Correct Video Perspective Distortion with FFmpeg
This article demonstrates how to correct perspective distortion—often
caused by camera tilt, angled shooting, or keystoning—using the powerful
perspective video filter in FFmpeg. You will learn the
underlying coordinate mapping system, the syntax of the filter, and a
step-by-step practical example to flatten and correct your video’s
perspective.
Understanding the FFmpeg Perspective Filter
The perspective filter works by mapping four corner
points of a distorted quadrilateral in the input video to four new
corner points in the output video. By defining where the tilted corners
currently are and where they should be, FFmpeg warps the video
frame to correct the distortion.
The four corners are defined in a specific order: 1. Top-Left (0) 2. Top-Right (1) 3. Bottom-Left (2) 4. Bottom-Right (3)
The Filter Syntax
The basic syntax for the perspective filter is:
perspective=x0:y0:x1:y1:x2:y2:x3:y3:px0:py0:px1:py1:px2:py2:px3:py3:sensex0toy3: The \(X\) and \(Y\) coordinates of the four corners in the original, distorted input video.px0topy3: The target \(X\) and \(Y\) coordinates of the four corners in the corrected output video.sense(Optional): Specifies the conversion action. Use0(default) for mapping destination to source, or1for mapping source to destination.
Step-by-Step Correction Guide
Step 1: Identify the Distorted Coordinates
Open your video in a media player or editing software that shows
pixel coordinates (such as Avidemux, Photoshop, or even using FFmpeg’s
ffplay with the drawgrid filter).
Locate the four corners of the distorted object or frame boundary you want to correct: * Top-Left (x0, y0): e.g., (150, 80) * Top-Right (x1, y1): e.g., (1130, 120) * Bottom-Left (x2, y2): e.g., (80, 980) * Bottom-Right (x3, y3): e.g., (1200, 900)
Step 2: Define the Target Coordinates
Decide where these corners should map to in the final output to look perfectly flat. If you want the corrected area to fill a standard 1280x720 frame, your target coordinates will be the outer boundaries of the video: * Top-Left (px0, py0): (0, 0) * Top-Right (px1, py1): (1280, 0) * Bottom-Left (px2, py2): (0, 720) * Bottom-Right (px3, py3): (1280, 720)
Step 3: Run the FFmpeg Command
Combine these coordinates into the FFmpeg command. Below is an example command applying the perspective filter and copying the audio stream without re-encoding it:
ffmpeg -i input.mp4 -vf "perspective=150:80:1130:120:80:980:1200:900:0:0:1280:0:0:720:1280:720:sense=0" -c:a copy output.mp4Parameter Explanations for the Command
-i input.mp4: Specifies the source video file.-vf "perspective=...": Applies the video filter with your designated coordinate mapping.-c:a copy: Copies the audio stream directly without re-encoding to save time and preserve quality.output.mp4: The corrected output video file.