FFMPEG (libx264) "height not divisible by 2"
Solution 1
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:
- Divide the original height and width by 2
- Round it up to the nearest pixel
- Multiply it by 2 again, thus making it an even number
- 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.
Solution 2
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.
Solution 3
If you want to set some output width and have output with the same ratio as original
scale=720:-1
and not to fall with this problem then you can use
scale="720:trunc(ow/a/2)*2"
(Just for people searching how to do that with scaling)
Solution 4
The problem with the scale
solutions here is that they distort the source image/video which is almost never what you want.
Instead, I've found the best solution is to add a 1-pixel pad to the odd dimension. (By default, the pading is black and hard to notice.)
The problem with the other pad
solutions is that they do not generalize over arbitrary dimensions because they always pad.
This solution only adds a 1-pixel pad to height and/or width if they are odd:
-vf pad="width=ceil(iw/2)*2:height=ceil(ih/2)*2"
This is ideal because it always does the right thing even when no padding is necessary.
Solution 5
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.
Related videos on Youtube
Andy Hin
Updated on July 12, 2022Comments
-
Andy Hin almost 2 years
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?
-
varmashrivastava over 5 years@AleksandrDubinsky But LordNeckbeard's answer doesn't preserve original width and height.Here we need to manually specify either width or height..and if w use -vf scale=-2:ih or -vf scale=iw:-2 this will not work if both height and width are uneven..Please explain how that answer is more optimal?..thanks
-
Aleksandr Dubinsky over 5 years@varmashrivastava Well, the way SO works is that there may originally have been one question, and then Google sends over a bunch of people with a different question who then hijack the page. It is what it is, try not to fight it. The correct answer to the original question is
-vf pad="width=ceil(iw/2)*2:height=ceil(ih/2)*2"
, which isn't even one of the answers. The correct answer to everyone else's question is LordNeckbeard's. -
Aleksandr Dubinsky over 5 years@varmashrivastava I've gone ahead and fixed the first answer. Hopefully it doesn't get vandalized by the mods.
-
varmashrivastava over 5 years@AleksandrDubinsky thanks..and user can use
"scale="
instead of"pad="
if he/she doesn't want colured padding pixels?
-
-
llogan over 10 yearsIt'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.
-
Andy Hin over 10 years@LordNeckbeard right, but why does either dimension need to be divisible by 2?
-
slhck over 10 yearsBecause 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.
-
Adisak over 9 yearsAnother 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).
-
Tom about 9 yearsAnd for a fixed height it is
scale="trunc(oh*a/2)*2:720"
-
LucaM almost 9 yearsI think tihis should be marked as the right answer because of no "tricks" involved. Whish to upvote more than one time
-
Pascal almost 9 yearsWhy 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. -
llogan almost 9 years@tuner The resulting value of
-2
depends on the declared value of the other dimension. -
lahwran over 8 yearsFor 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.
-
Glenn Maynard over 8 yearsYou'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.
-
Julien over 7 yearsin my case this gave me the following error:
Size values less than -1 are not acceptable.
but the answer from @Zbyszek worked perfectly. -
llogan over 7 years@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.
-
llogan over 7 years@Julien That's not
ffmpeg
. You can download a static build. -
Nicke Manarin about 7 years@GlennMaynard Could you care to give an example of
pad
for this problem? -
vbence about 7 yearsIt will distort the video.
-
Mark Berry almost 7 years@NickeManarin, this filter should work to add 1 pixel of white padding to the vertical dimension, with the video positioned upper left:
-vf pad="width=iw:height=ih+1:x=0:y=0:color=white"
. The ffmpeg pad documentation is here: ffmpeg.org/ffmpeg-filters.html#pad-1. -
danneu over 6 yearsHere's a solution that only adds a pixel of padding to dimensions that are odd:
-vf pad="width=ceil(iw/2)*2:height=ceil(ih/2)*2"
. -
llogan over 6 yearsYou don't need to declare threads: that's dealt with automatically. I believe the Andriod slowness when encoding to H.264 is due to people using the popular "WritingMinds/ffmpeg-android" which uses
--disable-asm
in its x264 build script. This results in unecessary and significant slowness (you can check the ffmpeg log and if it showsusing cpu capabilties: none!
then that's bad). I'm not sure why they added that, but I'm not an Android developer. -
pobro123 over 6 yearsThere is an old ffmpeg bug report that says it is a limitation of libx264: trac.ffmpeg.org/ticket/309
-
BjornW about 6 years@danneu this is the right answer. scaling will make the video look bad, padding is much better..
-
Georgii Oleinikov almost 6 yearsUnfortunately doesn't answer the original question: how to preserve the original width and height without rescaling
-
danneu over 5 yearsAlright, I've added an answer and tried to add some commentary based on my memory of the problems I had here. It's been a while. :) I've been using my solution in a large production pipeline since then and it's always had great results.
-
Ahmet over 5 yearsInstead of padding, if you prefer to crop. This is the formula
crop=floor(iw/2)*2:floor(ih/2)*2
-
Gyan over 5 yearsThe scale solutions alter the pixel count by 1 at most. That hardly distorts the picture. If you're worried about filtering speed, use
scale=iw+mod(iw,2):ih+mod(ih,2):flags=neighbor
. This can only increase each dimension by 1, if needed, and will duplicate the last row/column. -
danneu over 5 years@Gyan It's been too long since I had the problem that this solved (my answer was extracted from a comment I made long ago), but I remember that scaling by a single pixel did introduce noticeable visual artifacts under some conditions which is why I bothered in the first place. I don't remember exactly, maybe disproportionate amount of blurring from a single pixel change? Maybe only on some vid/image formats? All I can say is that I processed thousands of vids with this fix and it was the favorable transform.
-
Shevach Riabtsev over 4 yearsthe command '-vf pad=..." should be placed before the output video.mp4, otherwise no padding is executed. My example: ffmpeg -i test.h264 -vcodec libx264 -vf pad="width=iw+16:height=ih+16:color=white" -frames 10 -y -an test.mp4
-
Ahmed El-Gabbas almost 4 yearsThis worked for me. I used oher suggestions but all failed. Thnks
-
zaTricky over 3 yearsCan confirm ; padding a pixel adds no artifacts. Stretching, even if by only a single pixel, adds small artifacts due to aliasing.
-
Rahul Vyas over 3 years@llogan what is the difference between passing -1 or -2 in w or h ?
-
llogan over 3 years@RahulVyas It's explained in the scale filter documentation under width, height sections.
-
Vladimir Vukanac about 3 yearsThis
-2:480
saved my day ;)ffmpeg -i input.mp4 -vf scale=-2:480 output.mp4
. Also-preset fast
gave a bit bigger file size, but-preset ultrafast
gave almost 2x bigger file size. -
Aalex Gabi over 2 yearsHeight being divisible by 2 is not a requirement of h264 but of the pixel format used: yuv420p. If you use yuv422p you won't have to deal with this limitation.
-
ffarhour over 2 yearsJust came across this question, and can confirm this answer works. Alternatively if you are using complex filters, a solution like this will do the trick
-filter_complex "[0:v] {some filters you have} [outv]; [outv]pad='width=ceil(iw/2)*2:height=ceil(ih/2)*2'[outvpad]" -map "[outvpad]"
-
nurettin about 2 yearsscale 720:-1 made the resolution bad.
-
nurettin about 2 years@Gyan I did scale 720:-1 and I can't even read the text anymore.
-
Zbyszek about 2 yearschange 720 to a value that fits your needs like 1280:-1 or different