ffmpeg::avcodec_encode_video setting PTS h264

ffmpeg::avcodec_encode_video setting PTS h264

本文关键字:PTS h264 setting encode avcodec ffmpeg video      更新时间:2023-10-16

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

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

返回一个错误,我没有正确设置avframe->pts值。
我已经尝试将其设置为0,1,AV_NOPTS_VALUE和90khz *帧数,但仍然得到错误non-strictly-monotonic PTS

ffmpeg.c示例使用ffmpeg::av_rescale_q()设置packet.pts,但这只会在编码帧后调用!

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

我有同样的问题,通过在调用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);

从这篇有用的博客文章中偷来的解决方案

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

我也有这个问题。我用这种方法解决了这个问题:

在调用

之前
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, mOutputCodeCtxPtr类型为AVCodecContext*, mOutputStreamPtr类型为AVStream。

avcodec_encode_video返回0表示当前帧被缓冲,您必须在所有帧被编码后刷新所有缓冲帧。代码刷新所有缓冲帧,类似于:

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

我也有这个问题。据我所知,错误与dts

有关。

设置
out_video_packet.dts = AV_NOPTS_VALUE;

帮我

严格递增单调函数是f(x) <y。所以这意味着你不能用同样的PTS编码2帧。例如,用计数器检查,它应该不会再返回错误。>