FFmpeg Dynamic Video Padding Based on Frame Size
This article explains how to use the FFmpeg pad filter
with dynamic expressions to adjust a video’s canvas size based on its
input frame dimensions. You will learn the core variables used for
dynamic sizing, how to format the expressions, and practical examples
for centering videos, creating square outputs, and adding
percentage-based borders.
The FFmpeg Pad Filter Syntax
To pad a video dynamically, you must use the pad filter.
The basic syntax of the filter is:
pad=width:height:x:y:colorTo make these parameters dynamic, FFmpeg allows you to use mathematical expressions and variables that reference the input frame’s dimensions in real-time. The primary variables you will use are:
iworin_w: The input frame width.ihorin_h: The input frame height.oworout_w: The resolved output width (can be used in theheight,x, andyexpressions).ohorout_h: The resolved output height (can be used in thexandyexpressions).
Because FFmpeg evaluates these variables for every frame, any change in the input frame size will dynamically scale the padding to match.
Practical Examples of Dynamic Padding
1. Dynamic Square Padding (Centering the Video)
If you want to pad a video into a perfect square based on whichever
dimension is larger (width or height) while keeping the video centered,
use the max() function:
ffmpeg -i input.mp4 -vf "pad='max(iw,ih):max(iw,ih):(ow-iw)/2:(oh-ih)/2:black'" output.mp4max(iw,ih): Sets both the output width and height to the larger of the two input dimensions.(ow-iw)/2: Calculates the horizontal offset to perfectly center the input video.(oh-ih)/2: Calculates the vertical offset to perfectly center the input video.
2. Adding a Percentage-Based Border
To add a dynamic border that scales relative to the size of the input video (for example, adding a 10% border around the video), multiply the input dimensions:
ffmpeg -i input.mp4 -vf "pad='iw*1.2:ih*1.2:(ow-iw)/2:(oh-ih)/2:blue'" output.mp4iw*1.2: Sets the output width to 120% of the input width.ih*1.2: Sets the output height to 120% of the input height.blue: Fills the padded border area with a blue background.
3. Dynamic 16:9 Aspect Ratio Padding
If you have videos of varying dimensions and want to force them into a 16:9 aspect ratio container without cropping or stretching, use this expression:
ffmpeg -i input.mp4 -vf "pad='max(iw,ih*16/9):max(ih,iw*9/16):(ow-iw)/2:(oh-ih)/2:black'" output.mp4max(iw,ih*16/9): Dynamically calculates the required width to satisfy the 16:9 ratio.max(ih,iw*9/16): Dynamically calculates the required height to satisfy the 16:9 ratio.
Important Rules for Dynamic Expressions
- Quotes are Required: Wrap the entire filter
argument in double quotes, and the mathematical expressions in single
quotes (e.g.,
"pad='expr:expr...'"). This prevents command line shells from misinterpreting characters like*or/. - Order of Evaluation: FFmpeg evaluates
widthfirst, thenheight, thenx, and finallyy. This means you can useowin your expression forheight, and bothowandohin your expressions forxandy. You cannot, however, useohin the expression forwidth.