现在的位置: 首页 > 综合 > 正文

计算MP4文件播放时长的方法

2013年09月30日 ⁄ 综合 ⁄ 共 1662字 ⁄ 字号 评论关闭

计算MP4文件的播放时长,可以通过在mvhd box中去取得timescale和duration后计算得到:

播放时长 = duration / timescale

单位为秒(s)。

下面先介绍mvhd的结构。

Movie Header Atoms - MVHD

Movie header atom定义了整个movie的特性,例如time scale和duration,它的atom类型是'mvhd'。

mvhd结构图如下:

字段描述如下:

下面是mvhd字段的文字描述:

字段

长度(字节)

描述

尺寸

4

这个movie header atom的字节数

类型

4

Mvhd

版本

1

这个movie header atom的版本

标志

3

扩展的movie header标志,这里为0

生成时间

4

Movie atom的起始时间。基准时间是1904-1-1 0:00 AM

修订时间

4

Movie atom的修订时间。基准时间是1904-1-1 0:00 AM

Time scale

4

A time value that indicates the time scale for this movie—that is, the number of time units that pass per second in its time coordinate system. A time coordinate system that measures time in sixtieths of a second, for example, has a time scale of 60.

Duration

4

A time value that indicates the duration of the movie in time scale units.Note that this property is derived from the movie’s tracks. The value of this field corresponds to the duration of the longest track in the movie.

播放速度

4

播放此movie的速度。1.0为正常播放速度

播放音量

2

播放此movie的音量。1.0为最大音量

保留

10

这里为0

矩阵结构

36

该矩阵定义了此movie中两个坐标空间的映射关系

预览时间

4

开始预览此movie的时间

预览duration

4

movietime scale为单位,预览的duration

Poster time

4

The time value of the time of the movie poster.

Selection time

4

The time value for the start time of the current selection.

Selection duration

4

The duration of the current selection in movie time scale units.

当前时间

4

当前时间

下一个track ID

4

下一个待添加track的ID值。0不是一个有效的ID值。

实例分析:

例如1Mt.mp4文件,时长为70000ms,即70秒(s):

通过mp4Info工具查看如下红色框内所示:

总时长70s,duration为42000,timescale为600

根据本文开头说明的计算方法,那么先看看mvhd box的结构。

用mp4info和ultraedit都可以查看,上图中就包含了mvhd的结构信息,但显示的位数不全,应该是108字节,只显示了100字节,原因未明,可能是工具的原因。

下面用ultraedit查看如下图所示:

阴影部分就是所有的mvhd box,长度为108字节,即6C。

红色框内为timescale的值,00000258,换算成十进制是600。

紧接着后面的绿色框内的值是duration,0000A410,换算成十进制为42000。

所以根据公式duration / timescale = 42000 / 600 = 70s。

这个时长是文件总时长,audio和video track中较长的那个值。

参考:

获取mp4文件信息1 - 计算电影长度

http://www.52rd.com/Blog/Detail_RD.Blog_wqyuwss_7928.html

抱歉!评论已关闭.