FFMPEG - Width/ Height Not Divisible by 2 (Scaling to Generate MBR Output)

允我心安 提交于 2021-02-11 13:44:18

问题


I am trying to generate multilple variants of videos in my library (Mp4 formats) and have renditions planned ranging from 1080p to 240p and popular sizes in between. For that I am taking a video with a AxB resolution and then running through a code (on bash) which scales them to desired following sizes - 426x240 640x360 842x480 1280x720 1920x1080, with different bitrates of course, and then saves as Mp4 again.

Now, this works just fine if source video has height and width divisible by 2, but code breaks on the following line for the videos with odd width and height: -vf scale=w=${width}:h=${height}:force_original_aspect_ratio=decrease"

Where 'width' and 'height' are the desired (and hardcoded) for every iteration: E.g. "426x240", and "640x360"

The Error: [libx264 @ 00000187da2a1580] width not divisible by 2 (639x360) Error initializing output stream 1:0 -- Error while opening encoder for output stream #1:0 - maybe incorrect parameters such as bit_rate, rate, width or height

Now approaches those are explained in this one doesn't work for me since I am scaling - FFMPEG (libx264) "height not divisible by 2"

And, I tried this one too but it seems all qualities are getting the same size -ffmpeg : width not divisible by 2 (when keep proportions)

  • This is how I tried to use this one: scale='bitand(oh*dar,65534)':'min(${height},ih)'

Kindly suggest how to solve this, keeping in view that: 1. I have a very large library and I can't do manual change for every video 2. I need to scale the video and keep the aspect ratio

Thanks!

PS: [Edit] One way that I can see is padding all of the odd height/ weight videos using a second script in advance. This however doubles my work time and load. I would prefer to keep it in single script. This is the script I see that I can use for padding: ```ffmpeg -r 24 -i -vcodec libx264 -y -an -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2"`` (from: FFMPEG (libx264) "height not divisible by 2")


回答1:


You can insert either insert a pad or crop filter after the scale, depending on whether you prefer dimensions to increase or decrease respectively.

e.g. for pad,

scale=w=...,pad='iw+mod(iw\,2)':'ih+mod(ih\,2)'"

e.g. for crop,

scale=w=...,crop='iw-mod(iw\,2)':'ih-mod(ih\,2)'"


来源:https://stackoverflow.com/questions/61229313/ffmpeg-width-height-not-divisible-by-2-scaling-to-generate-mbr-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!