How to Correct Perspective with FFmpeg

Correcting perspective distortion in videos or images is a common post-production task that can be easily achieved using the powerful perspective video filter in FFmpeg. This guide provides a straightforward walkthrough on how to use FFmpeg's perspective filter, explaining how to define coordinate offsets and apply the command to warp, tilt, and straighten your media.

Understanding the Perspective Filter

The perspective filter works by mapping four source points (representing the distorted area in your input) to four destination points (representing the corrected rectangular area in your output).

The corners are defined in a specific clockwise order starting from the top-left:

  • Point 0: Top-Left (x0, y0)
  • Point 1: Top-Right (x1, y1)
  • Point 2: Bottom-Right (x2, y2)
  • Point 3: Bottom-Left (x3, y3)

The Command Syntax

The basic syntax for the filter is:

perspective=x0:y0:x1:y1:x2:y2:x3:y3:x0_out:y0_out:x1_out:y1_out:x2_out:y2_out:x3_out:y3_out:sense:eval
  • x0 to y3: The coordinates of the four corners in the original, distorted input.
  • x0_out to y3_out: The desired coordinates of the four corners in the corrected output.
  • sense: Specifies the interpretation of coordinates. Set to 0 (source to destination/backward, default) or 1 (destination to source/forward).
  • eval: Set to init (evaluate once) or frame (evaluate per frame).

Step-by-Step Example

Suppose you have a 1920x1080 video where a screen or sign is shot at an angle. You want to stretch and straighten this skewed area so it fills the entire 1920x1080 output frame.

  1. Identify the source coordinates: Locate the pixels of the four skewed corners in your input video.
    • Top-Left: x=250, y=150
    • Top-Right: x=1680, y=100
    • Bottom-Right: x=1750, y=950
    • Bottom-Left: x=180, y=1000
  2. Identify the destination coordinates: Map these corners to the boundaries of your output frame.
    • Top-Left: x=0, y=0
    • Top-Right: x=1920, y=0
    • Bottom-Right: x=1920, y=1080
    • Bottom-Left: x=0, y=1080
  3. Construct the FFmpeg command:
ffmpeg -i input.mp4 -vf "perspective=x0=250:y0=150:x1=1680:y1=100:x2=1750:y2=950:x3=180:y3=1000:x0_out=0:y0_out=0:x1_out=1920:y1_out=0:x2_out=1920:y2_out=1080:x3_out=0:y3_out=1080" -c:a copy output.mp4

Advanced Options

  • Linear Interpolation: To enable bilinear interpolation for smoother pixel rendering (which prevents aliasing on skewed edges), add the linear parameter:

    -vf "perspective=...:linear=1"
  • Perspective Sense: If you are warping the entire frame outward rather than pulling an inner section out, switching the sense parameter to 1 (forward mapping) can yield cleaner results depending on your coordinate calculation method.