Search code examples
ffmpeglibav

Error scaling a video using libav filters


I need to batch-process a bunch of videos to scale their height to 240 keeping the aspect ratio same. The command that gets the job almost done is:

$ avconv -threads 4 -ss 0.0 -i input.avi \
-map 0:0,0:0 -map 0:1,0:1 -vf "scale=-1:240" -y -f mpegts \
-async -1 -vcodec libx264 -vcodec libx264 -flags2 +fast \
-flags +loop -g 30 -bufsize 1024k \
-b 200k -bt 220k -qmax 48 -qmin 2 -r 20 -acodec libmp3lame \
-ab 44k -ar 44100 -ac 2 output.ts

The interesting part, as you can see, is -vf "scale=-1:240"

This works on videos where the scaled output width turns out to be an even number. Otherwise, I get the following error message:

[libx264 @ 0x7fc4f8821e00] width not divisible by 2 (341x240)

How do I overcome this?

Edit: As per this link, I tried using -vf "scale=trunc(oh/a/2)*2:240" which outputs a movie but the resulting video quality is really poor.

Edit #2: This is not a duplicate as it's wrongly marked. This question was posted much earlier than the other one.


Solution

  • Update; you can now use:

    -vf scale=-2:240
    

    Is there a good list of mp4 supported resolutions and a way to have ffmpeg calculate the width after modifying by set height ratio?


    The link actually says:

    scale="854:trunc(ow/a/2)*2"
    

    which correctly scales the height to an even number, if you wish to scale the width you must use this idiom:

    -vf scale="trunc(oh*a/2)*2:240"
    #                  ^
    #                 /
    #       notice ---
    

    Math behind it:

    iw   ow
    -- = --
    ih   oh
    
    ow = (iw * oh) / ih
    ow = oh * a