FFmpeg Perspective Filter Coordinate Calculation

The FFmpeg perspective filter allows you to alter the perspective of a video by mapping four source coordinates to four destination coordinates. This article provides a step-by-step guide on how to calculate these mapping coordinates to tilt, distort, or correct the keystone effect in your video files.

Understanding the Coordinate Parameter Order

The perspective filter uses 16 parameters representing the X and Y coordinates for four corners, first for the source (input) frame, and then for the destination (output) frame.

The syntax is structured as follows:

perspective=x0:y0:x1:y1:x2:y2:x3:y3:px0:py0:px1:py1:px2:py2:px3:py3

The corners are mapped in a specific Z-shaped sequence: * 0: Top-Left (TL) * 1: Top-Right (TR) * 2: Bottom-Left (BL) * 3: Bottom-Right (BR)

Therefore: * x0:y0 to x3:y3 are the source coordinates. * px0:py0 to px3:py3 are the destination coordinates.


Step 1: Define Your Source Coordinates

If you want to process the entire input frame, your source coordinates will simply be the outermost corners of your input video.

Using the width (\(W\)) and height (\(H\)) of your input video, define the source coordinates as: * Top-Left (x0, y0): 0:0 * Top-Right (x1, y1): W:0 * Bottom-Left (x2, y2): 0:H * Bottom-Right (x3, y3): W:H


Step 2: Calculate Your Destination Coordinates

To distort or tilt the video, you must calculate where these four source corners should land in the output frame.

Example: Tilting a Video Backward (Keystone Correction)

To make a video look like it is leaning backward, you need to pull the top corners inward and slightly downward, while keeping the bottom corners fixed.

Let’s assume an offset value of \(d_x\) (horizontal shift) and \(d_y\) (vertical shift): * Destination Top-Left (px0, py0): 0 + dx : 0 + dy * Destination Top-Right (px1, py1): W - dx : 0 + dy * Destination Bottom-Left (px2, py2): 0 : H (No change) * Destination Bottom-Right (px3, py3): W : H (No change)


Step 3: Put It into Practice (1920x1080 Example)

If your input video is \(1920 \times 1080\) (\(W = 1920, H = 1080\)), your source coordinates are: * x0:y0 = 0:0 * x1:y1 = 1920:0 * x2:y2 = 0:1080 * x3:y3 = 1920:1080

If you want to tilt the top of the video inward by 150 pixels (\(d_x = 150\)) and down by 50 pixels (\(d_y = 50\)), calculate the destination coordinates: * px0:py0 = 150:50 * px1:py1 = 1770:50 (calculated as \(1920 - 150\)) * px2:py2 = 0:1080 * px3:py3 = 1920:1080

The Final FFmpeg Command

Combine the source and destination coordinates into the perspective filter command:

ffmpeg -i input.mp4 -vf "perspective=0:0:1920:0:0:1080:1920:1080:150:50:1770:50:0:1080:1920:1080" output.mp4