使用 FFMPEG/libavformat 查找 WebM 的 duration/number 帧

Finding duration/number of frames of WebM using FFMPEG/libavformat

我正在尝试使用 FFMPEG 和 libavformat 从 WebM 文件中检索帧的持续时间和数量。我正在改编一些最初编写的用于 MP4 的代码,当通过 MP4 时它似乎工作正常。

我用 vp8 解析器和解码器以及 matroska 解复用器编译了 FFMPEG 2.1。

我有一个指向视频流 *video_stAVStream 指针。该代码在 video_st->nb_frames 中查找帧数,在 video_st->duration 中查找持续时间。这些似乎都是 0。

video_st->duration (https://ffmpeg.org/doxygen/trunk/structAVStream.html#a4e04af7a5a4d8298649850df798dd0b) 的 API 文档说 "If a source file does not specify a duration, but does specify a bitrate, this value will be estimated from bitrate and file size." 我可以确认这个文件确实指定了持续时间和比特率(因为我使用 ffmpeg -i 在我的桌面上对其进行了测试)。所以这是 returning 0 似乎很奇怪。也许我的 FFMPEG 构建配置不正确?

至于 nb_frames,文档说 "number of frames in this stream if known or 0"。如果这个 returns 0,我能做些什么吗?同样,ffmpeg -i 对 fps 进行了 return 估计;这已经足够好了,因为我可以用它来估计帧数和持续时间。

谢谢!

为什么 nb_frames 是 0?

Mp4文件可以有流中所有帧的完整索引,这可以让你准确地知道帧数。 AVI 也这样做。 WebM 只允许索引感兴趣的框架,例如关键帧,这意味着索引更小(即文件更小),但这也意味着除非您解析整个文件,否则您不知道帧数。

如何获取时长?

AVStream.duration is the per-stream duration. For many file types, including WebM, only the duration of the entire file is specified in the header, not the duration per-stream. Thus, use AVFormatContext.duration, which is in AV_TIME_BASE units (microseconds). To get an approximate FPS, use AVStream.avg_frame_rate,然后nb_frames可以从那里近似。