FFmpeg Pad Filter: How to Center and Pad Video
This guide explains how to use the FFmpeg pad filter to
resize a video to a specific target resolution while keeping the
original video content perfectly centered. You will learn the correct
command-line syntax, how the centering formulas work, and see practical
examples for padding your videos with solid background colors.
The FFmpeg Pad Filter Syntax
To pad a video, you use the video filter flag (-vf)
followed by the pad filter. The basic syntax for the pad
filter is:
-vf "pad=width:height:x:y:color"- width (or ow): The desired width of the output video.
- height (or oh): The desired height of the output video.
- x: The horizontal offset where the input video’s top-left corner will be placed.
- y: The vertical offset where the input video’s top-left corner will be placed.
- color: The color of the padded area (optional, defaults to black).
How to Center the Video
To perfectly center the input video within the new, larger
resolution, you must calculate the offsets (x and
y) dynamically. FFmpeg provides built-in variables that
make this easy:
iw: Input widthih: Input heightow: Output width (the target width you defined)oh: Output height (the target height you defined)
The mathematical formula to center the video is: * Horizontal
position (\(x\)):
(ow-iw)/2 * Vertical position (\(y\)): (oh-ih)/2
Code Example: Padding to 1920x1080 (1080p)
If you have an input video of any size and want to place it in the center of a black 1920x1080 canvas, use the following command:
ffmpeg -i input.mp4 -vf "pad=1920:1080:(ow-iw)/2:(oh-ih)/2:black" output.mp4Changing the Background Color
If you want to use a color other than black for the padded borders, change the color parameter at the end of the filter. For example, to use a white background:
ffmpeg -i input.mp4 -vf "pad=1920:1080:(ow-iw)/2:(oh-ih)/2:white" output.mp4You can also use hex color codes. For instance, to use a specific
gray background (#333333):
ffmpeg -i input.mp4 -vf "pad=1920:1080:(ow-iw)/2:(oh-ih)/2:color=333333" output.mp4