How to Resize Video with FFmpeg Scale Filter
This article provides a quick guide on how to use the FFmpeg
scale video filter to resize videos. You will learn the
basic syntax for changing video dimensions, how to automatically
maintain the original aspect ratio to prevent image stretching, and how
to use relative scaling variables for quick adjustments.
Basic Scaling Syntax
To resize a video to a specific width and height, use the video
filter flag (-vf) followed by the scale
filter. The basic command structure is:
ffmpeg -i input.mp4 -vf scale=640:480 output.mp4In this example, the video is resized to a width of 640 pixels and a height of 480 pixels.
Maintaining Aspect Ratio
If you force a specific width and height, the output video may appear
stretched. To maintain the original aspect ratio, set either the width
or height parameter to -1. FFmpeg will automatically
calculate the other dimension to keep the proportion correct.
To set the width to 320 pixels and scale the height proportionally:
ffmpeg -i input.mp4 -vf scale=320:-1 output.mp4To set the height to 720 pixels (720p) and scale the width proportionally:
ffmpeg -i input.mp4 -vf scale=-1:720 output.mp4Avoiding Encoder Errors (Using -2)
Many video codecs, such as H.264, require the width and height
dimensions to be divisible by 2. If using -1 results in an
odd number of pixels, the conversion will fail with an error. To prevent
this, use -2 instead of -1. This tells FFmpeg
to scale the video proportionally while ensuring the calculated
dimension is an even number.
ffmpeg -i input.mp4 -vf scale=320:-2 output.mp4Scaling Relative to the Input Size
You can use the variables iw (input width) and
ih (input height) to scale a video relative to its current
dimensions.
To scale a video to exactly half of its original size:
ffmpeg -i input.mp4 -vf scale=iw/2:ih/2 output.mp4To double the size of the input video:
ffmpeg -i input.mp4 -vf scale=iw*2:ih*2 output.mp4