日本語

Fixing FFmpeg “width not divisible by 2” When Resizing Video

​​FFmpeg​command line,H.264,video

When you use -1 for one side of scale, as in scale=-1:300, the calculated width or height can be odd. In that case libx264 stops with "width not divisible by 2". Changing -1 to -2 makes the calculated side even, and encoding succeeds. This form is useful when you want to lower the resolution while keeping the aspect ratio, for example when making a small video for the web or a preview.

Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), and libx264 0.164.3108. The only source video was a 5-second, 1920×1080, yuv420p H.264 video generated with FFmpeg's testsrc2. I did not test videos shot with a camera or smartphone, encoders other than libx264 (such as libx265 or GPU encoders), or how the output looks in players.

Example of an error when scaling down

I scaled a 1920×1080 video down to a height of 300. I set the width to -1 so that it would be calculated from the aspect ratio.

bash
ffmpeg -i src.mp4 -vf scale=-1:300 -c:v libx264 out.mp4

Encoding did not start. It stopped with the following output (the 0x... values change on each run):

text
[libx264 @ 0x564a79b49d00] width not divisible by 2 (533x300)
[vost#0:0/libx264 @ 0x564a79b49a40] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height.
Error while filtering: Generic error in an external library
Conversion failed!

The exit code was 187, and out.mp4 was left behind as a 0-byte file.

If the height is odd instead of the width, the message says height. When I set the width to 500 and the height to -1, the output was:

text
[libx264 @ 0x556ac36bad00] height not divisible by 2 (500x281)

Using -1 gives a width of 533

When you specify -1, the scale filter calculates that dimension from the other value and the original aspect ratio. Scaling 1920×1080 to a height of 300 gives a width of 1920 × 300 ÷ 1080 = 533.33….

Running with -v verbose shows the size that the scale filter chose.

bash
ffmpeg -v verbose -i src.mp4 -vf scale=-1:300 -frames:v 1 -f null -
text
[Parsed_scale_0 @ 0x56514995f300] w:1920 h:1080 fmt:yuv420p sar:1/1 -> w:533 h:300 fmt:yuv420p sar:1600/1599 flags:0x00000004

The width came out as 533. With the same 1920×1080 source, scale=-1:360 or scale=640:-1 gives 640×360 and no error. Whether the calculation divides evenly depends on the values you specify and the size of the source video.

Even numbers are required because the color format is yuv420p

The source video's color format is yuv420p. In yuv420p, brightness is stored for every pixel, but color is stored once per 2×2 block of pixels. If the width or height is odd, some pixels are left over that don't fit into these blocks.

Diagram of pixels arranged from the top-left. With a width of 6 and a height of 4, light blue 2×2 boxes divide evenly into six blocks. With a width of 5 and a height of 3, the rightmost column and the bottom row are enclosed by pink dotted lines and do not fit into 2×2 boxes
In yuv420p, the 2×2 pixel unit that holds color information, and the leftover pixels when the width or height is odd

The error comes not from FFmpeg but from x264, the H.264 encoder. In the x264 source code (encoder/encoder.c) at the same commit as the libx264 used in the test environment, the encoder checks that the width is divisible by 2 for 4:2:0 and 4:2:2 color formats, and that the height is also divisible by 2 for 4:2:0. If the check fails, it stops with "not divisible by 2".

I changed the color format and re-encoded at the same sizes:

Color format 533×300 (odd width) 500×281 (odd height)
yuv420p Error Error
yuv422p Error Encoded successfully
yuv444p Encoded successfully Encoded successfully

yuv422p halves the color information only horizontally, so it encoded successfully even with an odd height. yuv444p stores color for every pixel, so both sizes encoded. In this article I kept the color format at yuv420p and made the dimensions even instead.

Making the dimensions even with scale=-2

Change -1 to -2.

bash
ffmpeg -i src.mp4 -vf scale=-2:300 -c:v libx264 out.mp4

This time encoding succeeded, and the output was 534×300. With scale=500:-2, which sets the height to -2, the output was 500×282.

The FFmpeg filter documentation for scale says that when only one side is -n (where n is 1 or greater), the filter calculates a length that keeps the aspect ratio and then adjusts it so that it is divisible by n. -1 means n is 1, so the adjustment has no effect and the width stays at 533.

The adjustment rounds to the nearest multiple of n rather than truncating. In libavfilter/scale_eval.c in FFmpeg 6.1.1, the calculated length is divided by n, converted to an integer with av_rescale (which rounds to nearest), and then multiplied by n. I kept the height at 300 and changed n to check the width (the unrounded value is 533.33…):

Setting Width
scale=-1:300 533
scale=-2:300 534
scale=-4:300 532
scale=-8:300 536
scale=-16:300 528

With -2, 533.33… became 534, which is larger than the original value. -8 gave 536, also rounded up, while -4 and -16 rounded down. Which way it rounds depends on which multiple of n the original length is closest to.

Point

Only the side that is calculated becomes even with -2. If the side you specify is odd, as in scale=501:-2, the height becomes 282, but the width stays at 501. Encoding then stopped with "width not divisible by 2 (501×282)". Make the number you specify even yourself.

SAR is no longer 1:1 after scaling

When I checked the 534×300 video made with scale=-2:300 using ffprobe, the SAR (the aspect ratio of a single pixel) was 800:801 instead of 1:1.

bash
ffprobe -v error -select_streams v:0 -show_entries stream=width,height,pix_fmt,sample_aspect_ratio,display_aspect_ratio -of compact=p=0 out.mp4
text
width=534|height=300|sample_aspect_ratio=800:801|display_aspect_ratio=16:9|pix_fmt=yuv420p

534÷300 is not exactly 16:9. When the size changes, the scale filter recalculates the SAR and writes it to the output so that the source video's DAR (the aspect ratio when displayed) is preserved (libavfilter/vf_scale.c in FFmpeg 6.1.1). As a result, the DAR stayed at 16:9, the same as the source, and the SAR became 800:801.

If you want the SAR to be 1:1, add setsar=1 after scale.

bash
ffmpeg -i src.mp4 -vf scale=-2:300,setsar=1 -c:v libx264 out.mp4
text
width=534|height=300|sample_aspect_ratio=1:1|display_aspect_ratio=89:50|pix_fmt=yuv420p

The SAR became 1:1, and the DAR became 89:50, which is 534:300 reduced to lowest terms. As the setsar section of the documentation describes, changing the SAR also changes the DAR to match.

Fitting into a fixed box with force_divisible_by

If you want to set maximum width and height and fit the video inside them while keeping the aspect ratio, use force_original_aspect_ratio=decrease. However, this alone can still produce odd dimensions. Fitting into a 500×500 box gave 500×281, which stopped with "height not divisible by 2 (500×281)".

Adding force_divisible_by=2 gave 500×282, and encoding succeeded.

bash
ffmpeg -i src.mp4 -vf "scale=500:500:force_original_aspect_ratio=decrease:force_divisible_by=2" -c:v libx264 out.mp4

According to the documentation, force_divisible_by works with force_original_aspect_ratio to make both the width and the height divisible by the specified number. It behaves the same way as -n. The documentation also notes that the aspect ratio may change slightly.

​Posted by 86