FFmpeg Scale Video to Width Maintaining Aspect Ratio
Scaling a video to a specific width while preserving its original aspect ratio is a common task in video processing. This article provides a straightforward guide on how to use FFmpeg to resize your videos by defining a target width and letting FFmpeg automatically calculate the proportional height, ensuring the video does not look stretched or distorted.
The Basic Command
To scale a video to a specific width while maintaining its aspect
ratio, you use FFmpeg’s video filter (-vf) with the
scale parameter.
The basic syntax is:
ffmpeg -i input.mp4 -vf scale=1280:-1 output.mp4In this command: * -i input.mp4 specifies the input
video file. * -vf scale=1280:-1 applies the scale filter.
The first number (1280) is the desired width in pixels. The
second number (-1) tells FFmpeg to automatically calculate
the height based on the input video’s original aspect ratio. *
output.mp4 is the name of the exported file.
Ensuring Compatibility with Even Dimensions
While using -1 works for many formats, many common video
codecs (such as H.264 / AVC) require the video dimensions to be
divisible by 2. If the calculated height results in an odd number,
FFmpeg will return an error and fail to export.
To prevent this error, use -2 instead of
-1:
ffmpeg -i input.mp4 -vf scale=1280:-2 output.mp4Using -2 tells FFmpeg to calculate the height to
maintain the aspect ratio, but automatically round it to the nearest
even number. This ensures compatibility with almost all video
encoders.
Example: Scaling to 720p Width
If you want to resize a video to a width of 1280 pixels (commonly used for 720p HD) while keeping the aspect ratio perfect, run:
ffmpeg -i holiday.mov -vf scale=1280:-2 holiday_720p.mp4This command takes the input file holiday.mov, scales
the width to exactly 1280 pixels, adjusts the height to the
nearest even number that maintains the aspect ratio, and outputs the
result as holiday_720p.mp4.