简体   繁体   English

获取 MP4 文件的帧数

[英]Get a frame count of a MP4 file

I made my own MP4 parser (it recursively parses an atom tree and the content of some atoms).我制作了自己的 MP4 解析器(它递归解析原子树和一些原子的内容)。

I already see the duration of a video (in seconds) and I know how to find a start of a frame at a specific time (in seconds).我已经看到了视频的持续时间(以秒为单位),并且我知道如何在特定时间(以秒为单位)找到帧的开始。 However, I don't know how to find a number of frames (and compute the FPS) without decoding actual frames.但是,我不知道如何在不解码实际帧的情况下找到多个帧(并计算 FPS)。

I was using the moov - trak - 0 - mdia - minf - stbl - stsz array, which usually has one value per frame.我使用的是moov - trak - 0 - mdia - minf - stbl - stsz数组,它通常每帧有一个值。 But now, I found a MP4 video, which has 370 values in "stsz" (370 video samples), but only 184 frames.但是现在,我找到了一个 MP4 视频,它在“stsz”中有 370 个值(370 个视频样本),但只有 184 帧。

Regarding...关于...

"...How to find the number of frames (and thus compute the FPS ) without decoding actual frames?" “......如何在不解码实际帧的情况下找到帧数(从而计算 FPS )?”

"I found a MP4 video, which has 370 values in "stsz" (370 video samples), but only 184 frames." “我找到了一个 MP4 视频,它在"stsz"中有 370 个值(370 个视频样本),但只有 184 帧。”

short version:精简版:

FPS = ( stsz_sampleCount / (mdhd_duration / mdhd_timescale) );


details :详情
Your MP4 seems to have gone through some video trimmer/editor that modified the bytes.您的 MP4 似乎已经通过了一些修改字节的视频修剪器/编辑器。

  • The original duration is 6.195 seconds , equates into 370 samples of the stsz atom.原始持续时间为6.195 秒,相当于370stsz原子样本。
  • The edited duration , based on the edits in the elst atom, is now 2.820 seconds .编辑的持续时间,基于elst atom 中的编辑,现在是2.820 seconds

I did not check bytes to manually count frames, so I cannot confirm if there is really 370 samples in mdat or if such metadata details were kept unchanged for the sake of future decoders.我没有检查字节来手动计算帧数,所以我无法确认mdat中是否真的有 370 个样本,或者这些元数据细节是否为了未来的解码器而保持不变。 This means, for example because of how the FPS is calculated using that 370 number, it would be incorrect if the sample count was lower.这意味着,例如,由于如何使用370数字计算FPS ,如果样本计数较低,则将是不正确的。

FPS calculation for your MP4 file: MP4 文件的 FPS 计算:

  • From mdhd atom get the values of: timescale (=15360) and duration (=95154).mdhd atom 获取值: timescale (=15360) 和duration (=95154)。
  • Calculate: playTime = ( duration / timescale ) = 6.195 secs ( ie: Math.ceil from 6.194927...).计算: playTime = ( duration / timescale ) = 6.195 秒(即: Math.ceil from 6.194927...)。
  • From stsz atom, get the sampleCount (=370).stsz atom,获取sampleCount (=370)。

then:然后:

  • fps = ( sampleCount / playtime ) = 60 FPS ( ie: Math.ceil from 59.725...). fps = ( sampleCount / playtime ) = 60 FPS (即: Math.ceil from 59.725 ...)。
  • frameCount = sampleCount = ( fps * playTime ) = 370 total Frames. frameCount = sampleCount = ( fps * playTime ) = 370 总帧。


accounting for elst:其他会计:
You must read the elst (under edts atom) to find out how much of that frame count is "edited" (can be duration trimmed, can be longer frame duration, can be segment time-skipping, etc).您必须阅读elst (在edts atom 下)以了解有多少帧数被“编辑”(可以修剪持续时间,可以是更长的帧持续时间,可以是分段时间跳跃等)。 The edits affect the overall duration (except if just doing reverse playback).编辑会影响整个持续时间(除非只是进行反向播放)。

You can see your elst has only 1 entry (the one segment that will be played) and it says the segment is extracted from time 1.972 seconds and has a duration of 2.820 seconds .您可以看到您的elst只有1 个条目(将播放的一个片段),它说该片段是从时间1.972 seconds中提取的,并且持续时间为2.820 seconds

It is the only segment played:这是唯一播放的片段:
So the actual_playTime duration is 3 seconds ( ie: Math.ceil from 2.8 seconds).所以actual_playTime持续时间是3 秒即: Math.ceil 从 2.8 秒开始)。

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

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