[Libav-user] Setting libx264 bitrate via API

jettoblack jettoblack at gmail.com
Sat Jul 21 00:25:47 CEST 2012

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.

More information about the Libav-user mailing list