How to Add Borders in FFmpeg Using Pad Filter
Adding borders, letterboxes, or pillarboxes to a video is a common
post-production task easily accomplished using FFmpeg’s powerful
pad filter. This guide provides a straightforward
explanation of how the pad filter works, its basic syntax,
and practical command-line examples to help you quickly add colored
borders or adjust your video’s aspect ratio.
Understanding the Pad Filter Syntax
The basic syntax for the FFmpeg pad filter is:
-vf "pad=width:height:x:y:color"To understand how to configure these parameters, you can use FFmpeg’s
built-in constants: * iw (Input Width):
The width of the original video. * ih
(Input Height): The height of the original video. *
ow (Output Width): The final width of the
video including the borders. * oh (Output
Height): The final height of the video including the borders. *
x and y: The
horizontal and vertical coordinates where the top-left corner of the
input video will be placed within the new output frame. *
color: The color of the padding (defaults
to black). You can use standard color names (e.g., red,
blue, white) or hex codes (e.g.,
0x00FF00 or #FF0000).
Practical Examples
1. Add an Equal Border Around the Entire Video
To add a uniform 30-pixel black border around your entire video, you must increase the output width by 60 pixels (30 on each side) and the output height by 60 pixels. The original video is offset by 30 pixels from the top-left corner.
ffmpeg -i input.mp4 -vf "pad=iw+60:ih+60:30:30:color=black" output.mp42. Automatically Center the Video on a Specific Resolution
If you need to fit a video into a specific target size (for example, a 1920x1080 canvas) while keeping its original aspect ratio, you can use mathematical expressions to center the video automatically.
ffmpeg -i input.mp4 -vf "pad=1920:1080:(ow-iw)/2:(oh-ih)/2:color=black" output.mp4In this command: * 1920 and 1080 set the
exact final dimensions. * (ow-iw)/2 calculates the exact
horizontal center. * (oh-ih)/2 calculates the exact
vertical center.
3. Add Borders only to the Top and Bottom (Letterboxing)
If you want to add 100 pixels of padding specifically to the top and bottom of your video, keep the output width the same as the input width, increase the output height by 200, and shift the input video down by 100 pixels.
ffmpeg -i input.mp4 -vf "pad=iw:ih+200:0:100:color=red" output.mp44. Add Borders only to the Left and Right (Pillarboxing)
To add a 50-pixel white border to the left and right sides of your video:
ffmpeg -i input.mp4 -vf "pad=iw+100:ih:50:0:color=white" output.mp4