ffmpeg/avconv force scaled output to be divisible by 2

10

4

I record my desktop with avconv (aka ffmpeg) and then scale it to 720p.

I recently added to my script so they can select a window with xwininfo and it will record that one specifically.

However, if the output format isn't divisible by 2 the encoder freaks out and I don't get my video.

Is there a way to fix this inside ffmpeg so I don't have to do a bunch of manual math in bash to set the scale values?

Is it possible to have the scale filter subtract/add one to width/height if they're uneven? (round?)

Is it possible to apply another filter after the original scale that could accomplish this? (This would round videos that have an uneven input size and no scaling)

#!/bin/bash
avconv \
-f x11grab -r $fps -s $capturesize -i :0.0$offset \
-vcodec libx264 \
-vf scale=-1:720 \
-pre:v lossless_ultrafast \
-threads 4 \
-y $@

Some old mailing lists suggest a round() function you can use in the filter but I can't seem to get the syntax right. If it works a scale=round(iw,2):round(ih,2) filter applied at the end would solve all my problems at once. iw and ih don't seem to work.

J V

Posted 2013-03-25T10:12:02.157

Reputation: 446

Answers

7

After a lot of experimenting it looks like the following filter applied after other scale filters will round the width and height to 2.

scale=trunc(in_w/2)*2:trunc(in_h/2)*2

It's basically a divide, round, multiply thing, I just didn't have the syntax right.

J V

Posted 2013-03-25T10:12:02.157

Reputation: 446

9

Since division by 2 incurs in odd numbers sometimes, it should be:

-vf scale="trunc(oh*a/2)*2:720"

This performs what one would want with -1:720 syntax (keep original aspect ratio)

Fernando Piancastelli

Posted 2013-03-25T10:12:02.157

Reputation: 190

I use ceil instead trunc to get a higher pair number as the case of 16:9 480p – Felipe Alcacibar – 2016-11-02T15:05:53.803

8

Actually the filter can handle it directly. You need only to pass -2 instead of -1 to the filter: e.g.

-vf scale="-2:720"

I was surprised to find this out in a bug report from 3 years ago.

erik

Posted 2013-03-25T10:12:02.157

Reputation: 81

Didn't work for me, what is the minimum version supporting this? – letmaik – 2014-10-14T12:57:43.720

3-2 works for me in ffmpeg version 2.3.3 – Lance Nanek – 2015-03-16T14:44:50.053