注册 | 登录

解决h.264 - FFMPEG (libx264) "height not divisible by 2"

itPublisher 分享于



推荐:ffmpeg ERROR: libx264 not found

出售【Windows平台GPU硬解码SDK解码速度提升4倍】 ffmpeg ERROR: libx264 not found 看config.log来分析错误的原因 1)x624.h要自己加上 #include <stdint.h> 2)

I am trying to encode a .mp4 video from a set of frames using FFMPEG using the libx264 codec.

This is the command I am running:

/usr/local/bin/ffmpeg -r 24 -i frame_%05d.jpg -vcodec libx264 -y -an video.mp4

I sometimes get the following error:

[libx264 @ 0xa3b85a0] height not divisible by 2 (520x369)

After searching around a bit it seems that the issue has something to do with the scaling algorithm and can be fixed by adding a -vf argument.

However, in my case I don't want to do any scaling. Ideally, I want to keep the dimensions exactly the same as the frames. Any advice? Is there some sort of aspect ratio that h264 enforces?

ffmpeg h.264 libx264
  this question
edited Apr 11 '15 at 19:48 LordNeckbeard 25.5k 6 70 82 asked Dec 30 '13 at 21:13 Andy Hin 7,374 27 73 117 1   Please choose LordNeckbeard's answer. –  Aleksandr Dubinsky Feb 22 '16 at 15:53


4 Answers


After playing around with this a bit, I think I've answered my own question. Here is the solution in case anyone else runs into a similar issue... I had to add the above argument to the command:

-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

  this answer
answered Dec 30 '13 at 21:56 Andy Hin 7,374 27 73 117 2   It's not a bug. It does not matter that you're not performing scaling since the output will inherit the frame size of the input. –  LordNeckbeard Dec 30 '13 at 23:12 1   @LordNeckbeard right, but why does either dimension need to be divisible by 2? –  Andy Hin Dec 31 '13 at 8:05 1   Because that's what libx264 needs. I don't think it's a limitation of H.264 itself. Other encoders might enforce 8. In H.264 macroblocks can be split to 4⨉4 at minimum, so I don't know why 2 is the limit. –  slhck Dec 31 '13 at 15:14 2   For the record, I was just doing something where I created a video out of an image, and it used yuvj444p as the pixel format; it didn't care about the video size. Then I needed to convert it to yuv420p, and then it cared about the video size. I looked up yuv420p on wikipedia, I think it's a multi-pixel color format, that needs the image to be a specific size. Not sure why it matters compressed, though. –  lahwran Oct 18 '15 at 23:20 2   You're probably better off using pad rather than scale, to add a black row/column. Scaling an image up by one pixel will blur it. –  Glenn Maynard Nov 14 '15 at 22:20  |  show more comment

Just use -2

From the scale filter documentation:

If one of the values is -n with n > 1, the scale filter will also use a value that maintains the aspect ratio of the input image, calculated from the other specified dimension. After that it will, however, make sure that the calculated dimension is divisible by n and adjust the value if necessary.


Set width to 1280, and height will automatically be calculated to preserve the aspect ratio, and the height will be divisible by 2:

-vf scale=1280:-2

Same as above, but with a declared height instead; leaving width to be dealt with by the filter:

推荐:集成ffmpeg/x264:ERROR: libx264 not found的问题

集成ffmpeg/x264:ERROR: libx264 not found的问题    --拔剑 集成ffmpeg/x264碰到如下问题:     ERROR: libx264 not found 察看config.log,详细信息如下:     

-vf scale=-2:720

"divisible by 2"

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.


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

  this answer
edited Jun 30 '15 at 17:15 answered Apr 11 '15 at 19:48 LordNeckbeard 25.5k 6 70 82 4   I think tihis should be marked as the right answer because of no "tricks" involved. Whish to upvote more than one time –  donlucacorleone Jun 19 '15 at 10:14      Why does -vf scale=-2:-2 not work? In my case I want to preserve the original file size as much as possible. What worked for me was -vf scale=-2:ih. But it doesn't work if both h/w are uneven. –  tuner Jun 30 '15 at 8:27      @tuner The resulting value of -2 depends on the declared value of the other dimension. –  LordNeckbeard Jun 30 '15 at 17:10      in my case this gave me the following error: Size values less than -1 are not acceptable. but the answer from @Zbyszek worked perfectly. –  Julien Sep 30 '16 at 17:31      @Julien Never seen that before. Can you show your actual command and the complete console output? You can use a pastebin site and provide the link here. –  LordNeckbeard Sep 30 '16 at 17:33  |  show more comments

If you want to set some output width and have output with the same ratio as original


and not to fall with this problem then you can use


(Just for people searching how to do that with scaling)

  this answer
answered Jun 17 '14 at 9:49 Zbyszek 903 9 16 2   Thank you. This is exactly what I was searching for. Saved me a lot of time. –  Fred McIntyre Jan 23 '15 at 2:07 10   And for a fixed height it is scale="trunc(oh*a/2)*2:720" –  Tom Apr 15 '15 at 15:03


It's likely due to the the fact that H264 video is usually converted from RGB to YUV space as 4:2:0 prior to applying compression (although the format conversion itself is a lossy compression algorithm resulting in 50% space savings).

YUV-420 starts with an RGB (Red Green Blue) picture and converts it into YUV (basically one intensity channel and two "hue" channels). The Hue channels are then subsampled by creating one hue sample for every 2X2 square of that hue.

If you have an odd number of RGB pixels either horizontally or vertically, you will have incomplete data for the last pixel column or row in the subsampled hue space of the YUV frame.

  this answer
answered May 12 '14 at 16:51 Adisak 4,619 26 38 1   Cool, thanks for the technical explanation! –  Andy Hin May 23 '14 at 15:13 1   Another interesting fact... when you decode with Microsoft Media Foundation stuff, you need to use multiples of 16 for H264. So 1080P video actually decodes into a buffer that is 1088 high (although you ignore the last 8 lines). –  Adisak Jan 14 '15 at 17:50


推荐:android ffmpeg libx264编译记录

1. 2.








您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱