【问题标题】:ffmpeg::avcodec_encode_video setting PTS h264ffmpeg::avcodec_encode_video 设置 PTS h264
【发布时间】:2012-10-10 10:04:00
【问题描述】:

我正在尝试使用 libavcodec 将视频编码为 H264

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

返回我没有正确设置 avframe->pts 值的错误。
我尝试将其设置为 0,1、AV_NOPTS_VALUE 和 90khz * framenumber 但仍然收到错误 non-strictly-monotonic PTS

ffmpeg.c 示例使用 ffmpeg::av_rescale_q() 设置 packet.pts,但这仅在您对帧进行编码后调用!

与 MP4V 编解码器一起使用时,avcodec_encode_video() 会自行正确设置 pts 值。

【问题讨论】:

  • 使用新版本关闭 ffmpeg 简单设置“ppicture->pts=pCodecCtx->frame_number;”作品

标签: c++ h.264 video-encoding avcodec


【解决方案1】:

我遇到了同样的问题,通过在调用avcodec_encode_video之前计算pts解决了,如下:

//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);

this helpful blog post窃取的解决方案

(注意:将采样率更改为 khz,以 hz 表示,帧之间的时间太长,可能需要使用此值 - 不是视频编码专家,只是想要一些可行的东西,并且确实如此)

【讨论】:

    【解决方案2】:

    我也有这个问题。我以这种方式解决了这个问题:

    调用之前

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

    你将 avframe 的 pts 值设置为一个整数值,其初始值为 0,每次递增 1,就像这样:

    avframe->pts = nextPTS();
    

    nextPTS() 的实现是:

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

    在给avframe的pts一个值之后,然后对其进行编码。如果编码成功。添加以下代码:

        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);
    

    它将为编码的 AVFrame 添加正确的 dts 值。其中,AVPacket类型的packe、AVCodecContext*类型的mOutputCodeCtxPtr和AVStream类型的mOutputStreamPtr。

    avcodec_encode_video 返回 0 表示当前帧已缓冲,所有帧编码完成后,您必须刷新所有缓冲帧。代码刷新所有缓冲的帧有点像:

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

    【讨论】:

    • nextPTS 函数溢出时怎么办(即 static_pts > INT_MAX)?
    • 如果 static_pts 有溢出的危险,只需将其类型从 int 更改为 int64_t。
    【解决方案3】:

    我也有这个问题。据我记得,错误与dts有关

    设置

    out_video_packet.dts = AV_NOPTS_VALUE;
    

    帮了我

    【讨论】:

      【解决方案4】:

      如果 x

      【讨论】:

        猜你喜欢
        • 2021-10-20
        • 2012-04-13
        • 2011-11-24
        • 1970-01-01
        • 2011-09-06
        • 2020-08-12
        • 2015-09-02
        • 1970-01-01
        • 2012-10-06
        相关资源
        最近更新 更多