[Libav-user] Setting libx264 bitrate via API

Rakesh Shivayya Guttedar -ERS, HCL Tech rakesh-sg at hcl.com
Mon Jul 23 08:17:11 CEST 2012


I am also facing a similar issue. 
When I set the bit rate using 

	out_vcodec->bit_rate = 200000;

the quality of the video goes very bad. But when I set qmin and qmax, the bitrate setting is ignored.
Only when i set a high value (approx 100MB) for the bit rate, the quality of the video improves.

I think there is some bug in ffmpeg or I might be missing something.
Can anyone tell whats the problem?


-----Original Message-----
From: libav-user-bounces at ffmpeg.org [mailto:libav-user-bounces at ffmpeg.org] On Behalf Of jettoblack
Sent: Saturday, July 21, 2012 3:56 AM
To: libav-user at ffmpeg.org
Subject: [Libav-user] Setting libx264 bitrate via API

I'm trying to do the equivalent of this via the API:
ffmpeg -i test.m2t -c:v libx264 -b:v 500000 -s 640x360 -ac 2 test.mov

This gives a ABR file at 500kbits/sec.

I thought setting the video bitrate should be as simple as:
    out_vcodec->bit_rate = 500000;

When using CODEC_ID_MPEG2VIDEO or CODEC_ID_MPEG4, this works fine, and the
resulting file has the specified ABR.  However with libx264 (CODEC_ID_H264)
the bitrate parameter seems to have no effect, even very small values like
50,000 or very large like 50,000,000 (for a 640x360 video) give the same
output of around 200kbits/sec.  The input video is not a static picture and
it's not like 200kbit/sec is giving a lossless result and more bits are

I also tried:
   out_vcodec->bit_rate = 500000;
   out_vcodec->rc_min_rate = 500000;
   out_vcodec->rc_max_rate = 500000;
   out_vcodec->rc_buffer_size = 500000;

But still, there is no change in the output bitrate no matter what values I
put.  So I suspect these values are getting ignored or I'm not passing them
into the codec correctly.

The only parameter that actually affects the libx264 output bitrate is
setting out_vcodec->qmin and out_vcodec->qmax.  By playing with qmin and
qmax I can at least affect the bitrate, but it doesn't give a predictable
ABR or CBR result.

Any ideas?  Thanks!

I initialize the codec like this:

    AVCodec *vcodec = avcodec_find_encoder(CODEC_ID_H264);     // or
    AVCodecContext *out_vcodec = avcodec_alloc_context3(vcodec);
    avcodec_get_context_defaults3(out_vcodec, vcodec);
    if (vcodec->id == CODEC_ID_H264) {
        av_opt_set(out_vcodec->priv_data, "profile", "main", 0);
    out_vcodec->bit_rate = 500000;
    out_vcodec->width = 640;
    out_vcodec->height = 360;
    out_vcodec->pix_fmt = PIX_FMT_YUV420P;

    r = avcodec_open2(out_vcodec, vcodec, NULL);


View this message in context: http://libav-users.943685.n4.nabble.com/Setting-libx264-bitrate-via-API-tp4655453.html
Sent from the libav-users mailing list archive at Nabble.com.
Libav-user mailing list
Libav-user at ffmpeg.org


The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only.
E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted,
lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents
(with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates.
Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the
views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification,
distribution and / or publication of this message without the prior written consent of authorized representative of
HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately.
Before opening any email and/or attachments, please check them for viruses and other defects.


More information about the Libav-user mailing list