计算MP4⽂件播放时长的⽅法计算MP4⽂件的播放时长,可以通过在mvhd box中去取得timescale和duration后计算得到:
播放时长 = duration / timescale
单位为秒(s)。
下⾯先介绍mvhd的结构
Movie Header Atoms - MVHD
Movie header atom定义了整个movie的特性,例如time scale和duration,它的atom类型是'mvhd'。mvhd结构图如下:
字段描述如下:
下⾯是mvhd字段的⽂字描述:
字段长度
杰克逊this is it(字
节)
描述
电台情歌 邓超尺⼨4这个movie header atom的字节数
类型4Mvhd
版本1这个movie header atom的版本
标志3扩展的movie header标志,这⾥为0
⽣成时间4Movie atom的起始时间。基准时间是1904-1-1 0:00 AM 修订时间4Movie 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 s
econd, for example, has a time scale of 60.
沈阳啊Duration4A 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以movie的time scale为单位,预览的duration
冯巩喜剧电影Poster
time
4The time value of the time of the movie poster.
Selection
time
4The time value for the start time of the current selection.
adamlambertSelection
duration
4The 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 - 计算电影长度