FFmpeg: bitrate change dynamically

2019-08-01 09:29发布

问题:

I read the previous thread and this is the response from NISHAnT, FFMPEG: Dynamic change of bit_rate for Video

 avcodec_init();

 avcodec_register_all();

 codec = avcodec_find_encoder(CODEC_ID_H263);

 c = avcodec_alloc_context();

 picture= avcodec_alloc_frame();

    c->bit_rate = bitrate;
    c->width = w;
    c->height = h;
    c->time_base= (AVRational){1,framerate};
    c->pix_fmt = PIX_FMT_YUV420P;

 avcodec_close(c);

 av_free(c);

And this is my code:

    if(previous_BR != cur_BR){
        previous_BR = cur_BR;

        AVCodecContext* new_c = av_mallocz(sizeof(AVCodecContext));;

        avcodec_copy_context(new_c, ost_table[0]->st->codec);


        avcodec_close(ost_table[0]->st->codec);
        av_free(ost_table[0]->st->codec);

        avcodec_init();
        avcodec_register_all();

        ost_table[0]->enc = avcodec_find_encoder(CODEC_ID_H264);
        new_c = avcodec_alloc_context3(ost_table[0]->enc);
        ost_table[0]->st->codec = new_c;

        AVFrame *picture= avcodec_alloc_frame();

        new_c->bit_rate = cur_BR;
        new_c->width = 352;
        new_c->height = 288;
        int framerate = 30;
        new_c->time_base= (AVRational){1,framerate};
        new_c->pix_fmt = PIX_FMT_YUV420P;
        new_c->codec_type = AVMEDIA_TYPE_VIDEO;
        new_c->codec_id = CODEC_ID_H264;}

I tried to add my code to transcode(), but ffmpeg exits after it goes through my codes. is there something wrong with my codes? or what else I should add?

I put the code after "redo:", so that it will recursively loop back. please help !!

Thank you.

回答1:

c is AVCodecContext Structure. You must configure ffmpeg first for the type of file you are playing.Build it by conifguing first build.sh file in ffmpeg root directory. for the type of file you have to configure the codec9coder-decoder) and muxer/demuxer. for example to play avi file , you have to configure the muxer/demuxer and codec for avi which is MPEG "AVI" amd "MPEG4" respectively.