ffmpeg::avcodec_encode_video setting PTS h264

前端 未结 4 962
执念已碎
执念已碎 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.
    
    0 讨论(0)
  • 2020-12-28 21:32

    I had this problem too. As far as I remember, the error is related to dts

    setting

    out_video_packet.dts = AV_NOPTS_VALUE;
    

    helped me

    0 讨论(0)
  • 2020-12-28 21:34

    I had the same problem, solved it by calculating pts before calling avcodec_encode_video as follows:

    //Calculate PTS: (1 / FPS) * sample rate * frame number
    //sample rate 90KHz is for h.264 at 30 fps
    picture->pts = (1.0 / 30) * 90 * frame_count;
    out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
    

    Solution stolen from this helpful blog post

    (Note: Changed sample rate to khz, expressed in hz was far too long between frames, may need to play with this value - not a video encoding expert here, just wanted something that worked and this did)

    0 讨论(0)
  • 2020-12-28 21:35

    A strictly increase monotonic function is a function where f(x) < f(y) if x < y. So it means you cannot encode 2 frames with the same PTS as you were doing... check for example with a counter and it should not return error anymore.

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