FFmpeg Scale Divisible by 2 Using -1
This article explains how to scale videos in FFmpeg while ensuring
the auto-calculated dimension is divisible by 2 to prevent encoding
errors. You will learn how to adapt the standard aspect-ratio-preserving
-1 syntax into the -2 parameter, which
automatically forces the calculated width or height to be an even
number.
The Problem with -1
When scaling a video in FFmpeg, you often want to set one dimension
(such as a width of 1080 pixels) and let FFmpeg automatically calculate
the other dimension to preserve the original aspect ratio. Typically,
you do this using the -1 syntax:
ffmpeg -i input.mp4 -vf scale=1080:-1 output.mp4However, many modern video codecs (most notably H.264 via
libx264) require the video dimensions to be even numbers
(divisible by 2). If the calculated dimension results in an odd number
(e.g., 1080x607), FFmpeg will fail with an error like:
"width or height not divisible by 2".
The Solution: Use
-2 Instead of -1
To solve this, FFmpeg allows you to replace -1 with
-2. The -2 option tells the scale filter to
calculate the dimension to maintain the aspect ratio, but round it to
the nearest value that is divisible by 2.
Example 1: Fixed Width, Auto-Even Height
To set the width to 1280 and force the auto-calculated height to be divisible by 2:
ffmpeg -i input.mp4 -vf scale=1280:-2 output.mp4Example 2: Fixed Height, Auto-Even Width
To set the height to 720 and force the auto-calculated width to be divisible by 2:
ffmpeg -i input.mp4 -vf scale=-2:720 output.mp4Advanced: Forcing Other Divisibility (e.g., Divisible by 4 or 8)
The same syntax logic applies if your target codec requires dimensions divisible by larger numbers (like 4, 8, or 16 for certain macroblock requirements):
- Use
-4to force the dimension to be divisible by 4. - Use
-8to force the dimension to be divisible by 8.