ffmpeg: Width or Height not divisible by 2

If you’re trying to shrink a video by half along the lines of this, you may encounter an error about “width not divisible by 2” during processing, or you just might get an empty mp4 file:

INPUT_VIDEO='some-large-4k-video.mp4'
echo "Generating a ?x360 video from $INPUT_VIDEO"
ffmpeg -hide_banner -loglevel panic  -i "$INPUT_VIDEO" -vf scale=-1:360 -r 60000/1001 -preset slow -an -crf 23 -y -loglevel quiet -stats "$OUTPUT_FILENAME-360.mp4"

The problem happens when you get interesting video dimensions, such as a vertical video that might be 1080 wide by 1920 high.

When you try to downscale a 1080x1920 video to 360 high using -vf scale=-1:360 you end up trying to make a video that is 202.5 pixels wide, and that’s no good!

The simple fix here is to change -1 to -2 to tell ffmpeg to choose a number that’s divisible by 2 (instead of 1) so the end script might look like this:

INPUT_VIDEO='some-large-4k-video.mp4'

echo "Generating a ?x360 video from $INPUT_VIDEO"
ffmpeg -hide_banner -loglevel panic  -i "$INPUT_VIDEO" -vf scale=-2:360 -r 60000/1001 -preset slow -an -crf 23 -y -loglevel quiet -stats "$OUTPUT_FILENAME-360.mp4"

echo "Generating a ?x720 video from $INPUT_VIDEO"
ffmpeg -hide_banner -loglevel panic  -i "$INPUT_VIDEO" -vf scale=-2:720 -r 60000/1001 -preset slow -an -crf 23 -y -loglevel quiet -stats "$OUTPUT_FILENAME-720.mp4"

echo "Generating a ?x1080 video from $INPUT_VIDEO"
ffmpeg -hide_banner -loglevel panic  -i "$INPUT_VIDEO" -vf scale=-2:1080 -r 60000/1001 -preset slow -an -crf 23 -y -loglevel quiet -stats "$OUTPUT_FILENAME-1080.mp4"

Leave a Reply

Your email address will not be published. Required fields are marked *