ffmpeg::avcodec_encode_video setting PTS h264

前端 未结 4 1078
执念已碎
执念已碎 2020-12-28 20:43

I\'m trying to encode video as H264 using libavcodec

ffmpeg::avcodec_encode_video(codec,output,size,avframe);

returns an error that I don\'t hav

4条回答
  •  清酒与你
    2020-12-28 21:29

    I had this problem too. I sloved the problem in this way:

    Before you invoke

    ffmpeg::avcodec_encode_video(codec,output,size,avframe);
    

    you set the pts value of avframe an integer value which has an initial value 0 and increments by one every time, just like this:

    avframe->pts = nextPTS();
    

    The implementation of nextPTS() is:

    int nextPTS()
    {
        static int static_pts = 0;
        return static_pts ++;
    }
    

    After giving the pts of avframe a value, then encoded it. If encoding successfully. Add the following code:

        if (packet.pts != AV_NOPTS_VALUE)
            packet.pts = av_rescale_q(packet.pts, mOutputCodecCtxPtr->time_base, mOutputStreamPtr->time_base);
        if (packet.dts != AV_NOPTS_VALUE)
             packet.dts = av_rescale_q(packet.dts, mOutputCodecCtxPtr->time_base, mOutputStreamPtr->time_base);
    

    It'll add correct dts value for the encoded AVFrame. Among the code, packe of type AVPacket, mOutputCodeCtxPtr of type AVCodecContext* and mOutputStreamPtr of type AVStream.

    avcodec_encode_video returns 0 indicates the current frame is buffered, you have to flush all buffered frames after all frames have been encoded. The code flushs all buffered frame somewhat like:

    int ret;
    while((ret = ffmpeg::avcodec_encode_video(codec,output,size,NULL)) >0)
        ;// place your code here.
    

提交回复
热议问题