What is the correct way to set maxrate on android ffmpeg library?

2.3k Views Asked by At

I'm trying to control the max bitrate output using ffmpeg library on android.

When I use the ffmpeg command line, the output bitrate is 166.78 kbit/s

ffmpeg -y -i input.mp4 -c:v libx264 -preset medium -crf 29 -profile:v baseline -level: 3.0 -s 640x480 -strict experimental -c:a aac -ac 1 -ar 22050 -ab 48k -movflags faststart -maxrate 200k -bufsize 2000k output.mp4

But when I try using the android ffmpeg library the output bitrate is 2.12Mbit/s.

These are the options I'm passing the avcodec_open2.

av_dict_set(&opts, "preset", "medium", 0);
av_dict_set(&opts, "crf", "29", 0);
av_dict_set(&opts, "profile", "baseline", 0);
av_dict_set(&opts, "level", "30", 0);
av_dict_set(&opts, "maxrate", "200k", 0);
av_dict_set(&opts, "minrate", "0", 0);
av_dict_set(&opts, "bufsize", "2000k", 0);

/* open the codec */
ret = avcodec_open2(ctx, codec, &opts);

These are the settings in the context codec:

ctx->bit_rate = 200000;
ctx->width    = 640;
ctx->height   = 480;

Is there another setting I have to set?

I'll appreciate any help.

Thanks!

EDIT 1:

I removed the k in maxrate and bufsize but the output bitrate is still different.

For example:

ffmpeg -i input.mp4 -preset slow -c:v libx264 -maxrate 100k -bufsize 2000k -profile:v baseline -level 30 -s 480x360 -strict -2 -an output.mp4

produces an output with bitrate of 147.24 kbit/s

When running from android ffmpeg library, it produces an output with a different bitrate (99.45 kbit/s), also the video quality is reduced.

Using 200000 and 350000 as maxrate and buffsize produced a video with 157.49 kbit/s but the quality is poor compared with the command line result.

av_dict_set(&opts, "maxrate", "200000", 0);
av_dict_set(&opts, "bufsize", "350000", 0);

I did also try setting vbv-maxrate and vbv-bufsize with no luck.

What am I missing to get the same bitrate and quality when using the library?

Thanks!

2

There are 2 best solutions below

0
On

ffmpeg lib doesn't like the "k" in numbers.

Changed values to:

av_dict_set(&opts, "maxrate", "200000", 0);
av_dict_set(&opts, "minrate", "0", 0);
av_dict_set(&opts, "bufsize", "2000000", 0);
0
On

crf option makes the generated file to have constant quality, this is mutually exclusive to keeping constraints on bitrate, hence remove crf option and try again.