Pad any aspect ratio
Problem
Pad any aspect ratio into a larger image by correct pillar or letter boxing (without scaling).
Solution
ffmpeg \
-i input_file \
-filter:v "pad=output_width:output_height:(ow-iw)/2:(oh-ih)/2" \
-c:a copy \
output_file
Command syntax
ffmpeg
- starts the command
-i input_file
- path, name and extension of the input file
-filter:v "pad=output_width:output_height:(ow-iw)/2:(oh-ih)/2"
- video padding
-c:a copy
- re-encodes using the same audio codec
output_file
- path, name and extension of the output file
Discussion
The video filter is specified by -filter:v . We advise to avoid the alias -vfilter and its abbreviation -vf as well. This resolution independent formula is actually padding any aspect ratio into any larger aspect ratio by pillar or letter boxing, because the video filter uses relative values for input width (iw), input height (ih), output width (ow) and output height (oh). Of course, in this formula the brackets are compulsory. The quotation marks are not mandatory, but we strongly recommend their use. Also remember that without the quotation marks, the parentheses must be escaped:
-filter:v pad=output_width:output_height:\(ow-iw\)/2:\(oh-ih\)/2 .
The audio codec is specified by -codec:audio , which is usually abbreviated as -c:a (-codec:a or -c:audio are also possible). We advise to avoid the alias -acodec . For silent videos you can replace -c:a copy by -an ; for video with sound you may choose to re-encode with another audio codec which is allowed by the container.
2024-11-27
|