setting video bit rate through ffmpeg API is ignored for libx264 Codec

后端 未结 1 1383
隐瞒了意图╮
隐瞒了意图╮ 2021-02-11 06:25

I am transcoding a video using FFMPEG API in c code. I am trying to set the video bit rate using the ffmpeg API as shown below:

ovCodecCtx->bit_rate = 100 * 1         


        
1条回答
  •  终归单人心
    2021-02-11 07:24

    I have found the solution to my problem. In fact somebody who was facing the same problem has posted the solution in ffmpeg(libav) user forum. This seems to work in my case too. I am posting the answer to my own question so that other users facing similar issue might benefit from this post.

    Problem:

    Setting the Video Bit Rate programmatically for the H264 Video Codec was not honoured by the libx264 Codec. Even though it was working for MPEG1, 2 and MPEG4 video codecs, this setting was not recognised for H264 Video Codec. And the resulting video quality was very bad.

    Solution:

    We need to set the pts for the decoded/resized frames before they are fed to encoder. The person who found the solution has gone through ffmpeg.c source and was able to figure this out. We need to first rescale the AVFrame's pts from the stream's time_base to the codec time_base to get a simple frame number (e.g. 1, 2, 3).

    pic->pts = av_rescale_q(pic->pts, ost->time_base, ovCodecCtx->time_base);
    
    avcodec_encode_video2(ovCodecCtx, &newpkt, pic, &got_packet_ptr);
    

    And when we receive back the encoded packet from the libx264 codec, we need to rescale the pts and dts of the encoded video packet to the stream time base

    newpkt.pts = av_rescale_q(newpkt.pts, ovCodecCtx->time_base, ost->time_base);
    newpkt.dts = av_rescale_q(newpkt.dts, ovCodecCtx->time_base, ost->time_base);
    

    Thanks

    0 讨论(0)
提交回复
热议问题