Scale Video to Specific Height with FFmpeg

This article provides a quick and direct guide on how to scale a video to a specific height while automatically maintaining its original aspect ratio using FFmpeg. You will learn the exact command-line syntax, understand how the scaling parameters work, and discover how to prevent common video encoding errors related to odd-numbered pixel dimensions.

To scale a video to a specific height while keeping its aspect ratio, you use FFmpeg’s video filter (-vf) with the scale parameter. You set the height to your desired pixel value and set the width to -1, which tells FFmpeg to calculate the width automatically based on the input video’s aspect ratio.

Here is the basic command to scale a video to a height of 720 pixels:

ffmpeg -i input.mp4 -vf scale=-1:720 output.mp4

Avoiding Scaling Errors (The Divisible by 2 Rule)

Many modern video codecs, such as H.264, require the width and height of the video to be even numbers (divisible by 2). If the automatic calculation from -1 results in an odd number, FFmpeg will throw an error and stop the process.

To prevent this, use -2 instead of -1 for the width. This tells FFmpeg to calculate the width based on the aspect ratio and automatically round it to the nearest even number:

ffmpeg -i input.mp4 -vf scale=-2:720 output.mp4

Ensuring Web Compatibility

When re-encoding videos (especially for web playback), it is recommended to specify the pixel format to ensure maximum compatibility across browsers and devices. Adding -pix_fmt yuv420p ensures the output video plays correctly on almost all HTML5 players:

ffmpeg -i input.mp4 -vf scale=-2:720 -c:v libx264 -pix_fmt yuv420p -c:a copy output.mp4

In this command: * -vf scale=-2:720 sets the height to 720p and maintains an even-numbered aspect-ratio width. * -c:v libx264 encodes the video using the H.264 codec. * -pix_fmt yuv420p applies the widely compatible pixel format. * -c:a copy copies the original audio stream without re-encoding it, which saves processing time.