Correct Perspective Distortion with FFmpeg

Perspective distortion occurs when a camera captures a flat surface or scene from an angle, resulting in a trapezoidal or skewed appearance commonly known as the keystone effect. This guide explains how to use the FFmpeg perspective video filter to remap angled frame coordinates back into a rectangular, front-facing view, restoring proper proportions and straight vertical and horizontal lines in your video footage.

Understanding the perspective Filter

The FFmpeg perspective filter recalculates pixel positions by mapping four defined points to the four corners of the output frame.

The filter uses the following coordinate order:

  • x0, y0: Top-left corner
  • x1, y1: Top-right corner
  • x2, y2: Bottom-left corner
  • x3, y3: Bottom-right corner

By default, the filter operates with sense=source (value 0), which takes the four designated points from the distorted source frame and stretches them out to fill the rectangular output boundaries: (0, 0), (W, 0), (0, H), and (W, H).

Step 1: Identify the Distortion Coordinates

To correct the distortion, find the exact pixel coordinates of the four corners of the tilted object or area you want to rectify.

Extract a single reference frame from your video using the following command:

ffmpeg -i input.mp4 -vframes 1 reference.png

Open reference.png in an image editor (such as GIMP, Photoshop, or an online image inspector) and note the (X, Y) positions of the four skewed corners:

  • Top-Left (x0, y0): e.g., 120, 80
  • Top-Right (x1, y1): e.g., 1800, 110
  • Bottom-Left (x2, y2): e.g., 40, 1020
  • Bottom-Right (x3, y3): e.g., 1880, 1000

Step 2: Apply the Filter

Insert the identified coordinates into the perspective filter string:

ffmpeg -i input.mp4 -vf "perspective=x0=120:y0=80:x1=1800:y1=110:x2=40:y2=1020:x3=1880:y3=1000" -c:a copy output.mp4

FFmpeg will stretch the quadrilateral defined by these coordinates into a full rectangular video frame, eliminating the angular perspective tilt.

Adjusting Interpolation Quality

Perspective transformation requires resampling pixels, which can introduce blurriness or aliasing. You can set the interpolation mode using the interpolation parameter:

  • linear: Faster processing, moderate sharpness.
  • cubic: Slower processing, sharper details and smoother diagonal edges.

Example using cubic interpolation:

ffmpeg -i input.mp4 -vf "perspective=x0=120:y0=80:x1=1800:y1=110:x2=40:y2=1020:x3=1880:y3=1000:interpolation=cubic" -c:v libx264 -crf 18 -c:a copy output.mp4

Inverting the Mapping (sense=destination)

If you want to distort a standard rectangular video to fit into an angled surface within a scene (such as a TV screen or billboard shot at an angle), change the mapping direction using sense=destination:

ffmpeg -i input.mp4 -vf "perspective=x0=150:y0=100:x1=800:y1=160:x2=120:y2=600:x3=820:y3=550:sense=destination" -c:a copy output.mp4

In this mode, the filter compresses the standard rectangular input frame into the specified trapezoidal area.