How to Add Borders to Video with FFmpeg Pad Filter

Adding borders to a video is a common task in video post-production, useful for letterboxing, creating specific aspect ratios, or adding aesthetic margins. This guide demonstrates how to use the versatile pad filter in FFmpeg to add custom borders of any size and color to your videos, complete with practical command examples.

Understanding the Pad Filter Syntax

The basic syntax for the pad filter in FFmpeg is as follows:

-vf "pad=width:height:x:y:color"

Here is what each parameter represents: * width (or w): The total width of the output video, including the borders. * height (or h): The total height of the output video, including the borders. * x: The horizontal offset where the original video will be placed inside the new frame. * y: The vertical offset where the original video will be placed inside the new frame. * color: The color of the border (defaults to black if not specified).

You can use mathematical expressions and predefined variables like iw (input width), ih (input height), ow (output width), and oh (output height) to calculate these values dynamically.

Example 1: Adding an Equal Border to All Sides

To add a uniform border around your entire video, you must increase both the output width and height, and then offset the input video to center it.

The following command adds a 40-pixel black border to all sides of a video (increasing the total width by 80 pixels and the height by 80 pixels):

ffmpeg -i input.mp4 -vf "pad=iw+80:ih+80:40:40:black" output.mp4

In this command: * iw+80 sets the output width to the input width plus 80 pixels. * ih+80 sets the output height to the input height plus 80 pixels. * 40:40 places the original video 40 pixels from the left and 40 pixels from the top, centering it perfectly.

Example 2: Centering Video in a Specific Resolution (Letterboxing/Pillarboxing)

If you need to fit a video into a specific container size, such as forcing a vertical video into a standard 1920x1080 widescreen format, you can define the exact target resolution and use formulas to center the video automatically.

ffmpeg -i input.mp4 -vf "pad=1920:1080:(ow-iw)/2:(oh-ih)/2:black" output.mp4

In this command: * 1920:1080 defines the final output resolution. * (ow-iw)/2 automatically calculates the horizontal offset to center the video. * (oh-ih)/2 automatically calculates the vertical offset to center the video.

Example 3: Changing the Border Color

FFmpeg supports a wide variety of color names (such as white, red, blue, violet) as well as hexadecimal color codes.

To add a 50-pixel white border to your video, use this command:

ffmpeg -i input.mp4 -vf "pad=iw+100:ih+100:50:50:white" output.mp4

To use a custom hex color (for example, a specific shade of grey like #333333), format the color parameter like this:

ffmpeg -i input.mp4 -vf "pad=iw+100:ih+100:50:50:0x333333" output.mp4

Example 4: Adding a Border to Only One Side

You can also use the pad filter to add a border to a single side of the video, which is useful for making space for subtitles or logos.

To add a 100-pixel black bar only to the bottom of the video:

ffmpeg -i input.mp4 -vf "pad=iw:ih+100:0:0:black" output.mp4

Because x and y are set to 0:0, the original video starts at the very top-left corner, leaving the extra 100 pixels of height to appear entirely at the bottom.