[Libav-user] Changing bit rate while encoding produces a short skip in the video

Joel Ng blood_elves88 at yahoo.com.sg
Mon Apr 3 06:21:27 EEST 2017


> Hello,
>
> Following the encode_video.c example on github
> (https://github.com/FFmpeg/FFmpeg/blob/58f24adc05b0ea47c3f7ed5cb9e8d5774e2fbaaf/doc/examples/encode_video.c),
> I have managed to change the bit rate of the encoded video in the
> encoding loop.
>
> However, I have noticed that the video has a short lag or skip when the
> bit rate is changed. Is this behavior expected? I am doing real-time
> live streaming and having this short skip is undesirable.
>
> More details on how I changed bit rate on the fly:
> In the encoding loop, I basically called "avcodec_free_context(&c);",
> followed by "c = avcodec_alloc_context3(codec);", setting the other
> parameters (e.g. c->bit_rate, c->width, etc.), and then
> "avcodec_open2(c, codec, NULL)". In short, I called line 66 to 98 in
> encode_video.c once during the encoding loop (line 122 onwards) to
> change the bit rate.
>
> Did I do it correctly? Perhaps I did something unnecessary which
> introduced the skip in the video.
>
> Thanks for the help.
>
> Regards,
> Joel
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user

I just thought I should answer my own question, in case anyone comes 
across it.

The whole re-initialization of the AVCodecContext by using 
avcodec_free_context, av_codec_alloc_context3, and avcodec_open2 takes 
anywhere from 0.2 seconds to 0.8 seconds from my measurements. Doing it 
sequentially during the encoding loop causes the skip.

So, just do it in another thread, then swap the AVCodecContext in when 
done.


More information about the Libav-user mailing list