FFMPEG (libx264) “height not divisible by 2”

When your are trying to encode a .mp4 video from a set of frames using FFMPEG using the libx264 codec. And get this error response [libx264 @ 0x24c6640] or [libx264 @ 0xa3b85a0] height not divisible by 2 ($widthx$height) the simple solution is to do as follow
-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2"

Basically the issue stems from a bug(?) in libx264 where it complains if the width or height is not an even number. This is weird in the case where I don’t want to perform any scaling at all. So the command above will:

  1. Divide the original height and width by 2
  2. Round it down to the nearest pixel
  3. Multiply it by 2 again, thus making it an even number


As required by x264, the “divisible by 2 for width and height” is needed for YUV 4:2:0 chroma subsampled outputs. 4:2:2 would need “divisible by 2 for width”, and 4:4:4 does not have these restrictions. However, most non-FFmpeg based players can only properly decode 4:2:0, so that is why you often see ffmpeg commands with the -pix_fmt yuv420p option when outputting H.264 video.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.