c++ - ffmpeg c/c++ 获取帧数或时间戳和 fps

标签 c++ c ffmpeg

我正在使用 ffmpeg 解码 C 中的视频文件。我正在努力获取我正在解码的当前帧的计数或帧的时间戳。我已经阅读了许多文章,这些文章展示了如何根据 fps 和帧时间戳计算估计的帧数,但是我无法获得其中任何一个。

我需要的:视频文件的fps,当前帧的时间戳或帧号(未计算)

我所拥有的:我可以使用

获取视频的时间
pFormatCtx->duration/AV_TIME_BASE

我在处理它们时计算当前的帧数,并获取当前的帧数,但这不会长期有效。我可以使用

获取文件的总帧数
pFormatCtx->streams[currentStream->videoStream]->nb_frames

我读过这可能不适用于所有流,尽管它对我尝试过的每个流都有效。

我已经尝试使用 time_base.num 和 time_base.den 值以及 packet.pts,但我无法理解从中获得的值,所以我可能只需要更好地了解这些值是。

有人知道显示有关如何获取此值的示例的资源吗?

最佳答案

此 url 讨论了为什么 pts 值可能没有意义以及如何获得合理的值: An ffmpeg and SDL Tutorial by Dranger

这是该链接的摘录,它根据帧号和时间戳提供了关于您正在寻找的内容的指导。如果这对您有用,那么您可能需要阅读更多文档以获得更全面的理解:

So let's say we had a movie, and the frames were displayed like: I B B P. Now, we need to know the information in P before we can display either B frame. Because of this, the frames might be stored like this: I P B B. This is why we have a decoding timestamp and a presentation timestamp on each frame. The decoding timestamp tells us when we need to decode something, and the presentation time stamp tells us when we need to display something. So, in this case, our stream might look like this:

PTS:    1 4 2 3
DTS:    1 2 3 4
Stream: I P B B

Generally the PTS and DTS will only differ when the stream we are playing has B frames in it.

When we get a packet from av_read_frame(), it will contain the PTS and DTS values for the information inside that packet. But what we really want is the PTS of our newly decoded raw frame, so we know when to display it.

Fortunately, FFMpeg supplies us with a "best effort" timestamp, which you can get via, av_frame_get_best_effort_timestamp()

关于c++ - ffmpeg c/c++ 获取帧数或时间戳和 fps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9014475/

相关文章:

c - 指针大小相同吗?

video - FFMPEG 在编码时是否支持用户数据段注入(inject)?

android - 使用 1 :1 aspect ratio on Android 捕获视频

c++ - 交叉编译 C++ 项目,通用 ELF 中的重定位 (EM : 3)

转换数据 block 以与结构保持一致

c++ - 查找最大值并删除二维数组中的该行。 C++

javascript - 如何在 webkit2gtk 中将 native 代码公开给 javascript

php - ffmpeg 没有给出正确的 mp4 持续时间

c++ - 如何重载 ~(按位不) 运算符?

c++ - 如何将指向 GLEW 函数的指针传递给模板?