繁体   English   中英

ffmpeg::avcodec_encode_video 设置 PTS h264

[英]ffmpeg::avcodec_encode_video setting PTS h264

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

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

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

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

当与 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 值设置为 integer 值,其初始值为 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.

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

环境

out_video_packet.dts = AV_NOPTS_VALUE;

帮助过我

严格增加单调 function 是 function 其中 f(x) < f(y) 如果 x < y。 因此,这意味着您不能像以前那样使用相同的 PTS 对 2 帧进行编码……例如使用计数器进行检查,它不应再返回错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM