FFMPEG (libx264) "height not divisible by 2"

The answer to the original question should not scale the video but instead fix the height not divisible by 2 error. This can be achieve using this filter:

-vf "pad=ceil(iw/2)*2:ceil(ih/2)*2"

Full command:

ffmpeg -i frame_%05d.jpg -vcodec libx264 \
 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -r 24 \
 -y -an video.mp4 

Basically, .h264 needs even dimensions so this filter will:

  1. Divide the original height and width by 2
  2. Round it up to the nearest pixel
  3. Multiply it by 2 again, thus making it an even number
  4. Add black padding pixels up to this number

You can change the color of the padding by adding filter parameter :color=white. See the documentation of pad.


For width and height

Make width and height divisible by 2 with the crop filter:

ffmpeg -i input.mp4 -vf "crop=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4

If you want to scale instead of crop change crop to scale.

For width or height

Using the scale filter. This will make width 1280. Height will be automatically calculated to preserve the aspect ratio, and the width will be divisible by 2:

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

Similar to above, but make height 720 and automatically calculate width:

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

You can't use -2 for both width and height, but if you already specified one dimension then using -2 is a simple solution.