Configure FFmpeg swaprect Filter Coordinates
This guide explains how to configure the dimensions and coordinates
of the two rectangular regions used in the FFmpeg swaprect
video filter. You will learn the exact syntax, the parameter
definitions, and how to apply practical examples to swap specific areas
within a video frame.
The swaprect Syntax
The swaprect filter swaps two rectangular areas of the
same size within a video. The basic syntax for the filter is:
swaprect=w:h:x1:y1:x2:y2
Each parameter defines the size or position of the rectangles:
w: The width of both rectangular areas.h: The height of both rectangular areas.x1: The horizontal (X) coordinate of the top-left corner of the first rectangle.y1: The vertical (Y) coordinate of the top-left corner of the first rectangle.x2: The horizontal (X) coordinate of the top-left corner of the second rectangle.y2: The vertical (Y) coordinate of the top-left corner of the second rectangle.
Available Constants
Instead of using strictly static pixel values, you can use built-in FFmpeg constants to make your coordinates dynamic:
woriw: Input video width.horih: Input video height.a: Aspect ratio (input width divided by input height).sar: Sample aspect ratio.dar: Display aspect ratio.
Practical Examples
Example 1: Swapping Top-Left and Top-Right Corners
To swap a 200x150 pixel area from the top-left corner
(0,0) with a 200x150 pixel area in the top-right corner of
a video, use the following command:
ffmpeg -i input.mp4 -vf "swaprect=200:150:0:0:iw-200:0" -c:a copy output.mp4In this command: * w is 200 and
h is 150. * The first rectangle is positioned
at x1=0, y1=0. * The second rectangle is
positioned at x2=iw-200 (input width minus the rectangle
width) and y2=0.
Example 2: Swapping Left and Right Halves of a Video
To swap the left half of a video with the right half, set the width
of the rectangles to half of the input width (iw/2) and the
height to the full input height (ih):
ffmpeg -i input.mp4 -vf "swaprect=iw/2:ih:0:0:iw/2:0" -c:a copy output.mp4In this command: * w is iw/2 (half the
width) and h is ih (full height). * The first
rectangle starts at 0:0. * The second rectangle starts at
iw/2:0.