<div><div>I have a question. How can I modify the video-encode bitrate in real time during coding after the video encoder has been turned on? Here's what I did (take x264-encoder as an example) :</div><div><br></div><div>step1.</div><div>Modified the definition of structural AVCoder to add a function pointer.</div><div><span style="white-space:pre">       </span>int (*valid_context)(AVCodecContext*);</div><div><br></div><div>step2.</div><div>Added function definition to enable the codec rate of x264.</div><div>static int X264_config(AVCodecContext* avctx){</div><div>    X264Context* x4 = avctx->priv_data;</div><div>    if(avctx->bit_rate){</div><div>        x4->params.rc.i_bitrate = avctx->bit_rate / 1000;</div><div>    }</div><div>    avctx->bit_rate = x4->params.rc.i_bitrate*1000;</div><div>    return 0;</div><div>}</div><div><br></div><div>step3.</div><div>build AVCodec.</div><div>AVCodec ff_libx264_encoder = {</div><div>    .name             = "libx264",</div><div>    .long_name        = NULL_IF_CONFIG_SMALL("libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),</div><div>    .type             = AVMEDIA_TYPE_VIDEO,</div><div>    .id               = AV_CODEC_ID_H264,</div><div>    .priv_data_size   = sizeof(X264Context),</div><div>    .init             = X264_init,</div><div>    //</div><div>    .valid_context    = X264_config,</div><div>    //</div><div>    .encode2          = X264_frame,</div><div>    .close            = X264_close,</div><div>    .capabilities     = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,</div><div>    .priv_class       = &x264_class,</div><div>    .defaults         = x264_defaults,</div><div>    .init_static_data = X264_init_static,</div><div>    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE |</div><div>                        FF_CODEC_CAP_INIT_CLEANUP,</div><div>    .wrapper_name     = "libx264",</div><div>};</div><div><br></div><div>step4.</div><div>FFmpeg interface layer.</div><div>int attribute_align_arg avcodec_valid_codec_Context(AVCodecContext* avctx){</div><div>    int ret = 0;</div><div>    ret = avctx->codec->valid_context(avctx);</div><div>    if(ret){</div><div>        av_log(avctx, AV_LOG_ERROR, "valid context error, ERROR CODE:%d\n", </div><div><span style="white-space:pre">          </span>ret);</div><div>    }</div><div>    return ret;</div><div>}</div><div><br></div><div>step5.</div><div>Whenever the application layer needs to modify the bit rate, then App should modify the AVCodecContext-> bit_rate, and then call the above interface to make the Context take effect in the encoder.</div><div><br></div><div>Because of the limited understanding of ffmpeg. So if there is a better way, also hope to give instruction.</div></div>