Discussion:
[FFmpeg-user] FFMpeg encoding error: "height not divisible by 2" - Recommended dimesion 1680 x ??? with aspect 16:9
Ben
2018-10-22 07:32:57 UTC
Permalink
I wanted to encode (and resize) an existing MP4 file into a new MP4 video and a width of 1680 * ... and an aspect ratio of 16:9
The used codec is H.264

Since the calculated height therefore is (1680/16)*9=945 I entered this for the ffmpeg command and got an error:

"height not divisible by 2"

Hmm, can this really be?

What is the recommended height otherwise?

Is there no auto-adjustment in ffmpeg?

Ben
_______________________________________________
ffmpeg-user mailing list
ffmpeg-***@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
ffmpeg-user-***@ffmpeg.org
He Lei
2018-10-22 08:47:19 UTC
Permalink
The encoder needs multiples of 2. It better be a multiple of 16.
You can create a black background, center over the background use the source image.
This will not distort the image.

black = yuv(0, 128, 128);


//=========================
src_ratio = src_w / src_h; //ratio of source
dst_ratio = 16.0 / 9; //ratio of destination

if(src_ratio > dst_ratio){
bg_w = src_w; //width of background
bg_h = (int)(bg_w / dst_ratio); //height of background
bg_h += bg_h % 2; //make it a multiple of 2
}else{
bg_h = src_h;
bg_w = (int)(bg_h * dst_ratio);
bg_w += bg_w % 2; //make it a multiple of 2
}

overlay_x = (bg_w - src_w) / 2; //overlay_x and overlay_y always have one equal to 0
overlay_y = (bg_h - src_h) / 2;


then:
1,create background with size(bg_w, bg_h);
2,overlay to (overlay_x, overlay_y) with the source;
3,encode


LeiHe
***@hotmail.com<mailto:***@hotmail.com>



在 2018年10月22日,下午3:32,Ben <bxstover-at-***@ffmpeg.org<mailto:bxstover-at-***@ffmpeg.org>> 写道:

I wanted to encode (and resize) an existing MP4 file into a new MP4 video and a width of 1680 * ... and an aspect ratio of 16:9
The used codec is H.264

Since the calculated height therefore is (1680/16)*9=945 I entered this for the ffmpeg command and got an error:

"height not divisible by 2"

Hmm, can this really be?

What is the recommended height otherwise?

Is there no auto-adjustment in ffmpeg?

Ben
_______________________________________________
ffmpeg-user mailing list
ffmpeg-***@ffmpeg.org<mailto:ffmpeg-***@ffmpeg.org>
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
ffmpeg-user-***@ffmpeg.org with subject "unsubscribe".

_______________________________________________
ffmpeg-user mailing list
ffmpeg-***@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
ffmpeg-user-***@ffmp
Moritz Barsnick
2018-10-22 09:11:49 UTC
Permalink
Post by Ben
I wanted to encode (and resize) an existing MP4 file into a new MP4 video and a width of 1680 * ... and an aspect ratio of 16:9
The used codec is H.264
"height not divisible by 2"
Hmm, can this really be?
Yes, certain operations and codecs have such requirements. For example
the very common H.264 with yuv420p colorspace.
Post by Ben
What is the recommended height otherwise?
Some codecs even encode more efficiently when the resolution is
divisible by 4, 8 or possibly even 16. In your case, just choose a
height divisible by two.
Post by Ben
Is there no auto-adjustment in ffmpeg?
No, but if you use the scale filter (or others using the same size
syntax), you can let ffmpeg do the math for you:

Resize while keeping input aspect ratio, but divisible by 2:

$ ffmpeg [...] -vf scale=1680:-2 [...]

If you want to be explicit about 16:9 (and let ffmpeg calculate 16/9):

$ ffmpeg [...] -vf "scale=trunc(1680/2)*2:trunc(1680*9/16/2)*2"

There are some other nice suggestions in this StackOverflow topic:
https://stackoverflow.com/q/20847674/3974309

Cheers,
Moritz
_______________________________________________
ffmpeg-user mailing list
ffmpeg-***@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-user

To unsubscribe, visit link above, or email
ffmpe

Loading...