mp4 mvhd 解析

mvhd的协议描述如下:

8.2.2.1 Definition
Box Type: ‘mvhd’
Container: Movie Box (‘moov’)
Mandatory: Yes
Quantity: Exactly one
This box defines overall information which is media‐independent, and relevant to the entire
presentation considered as a whole.

8.2.2.2 Syntax 


aligned(8) class FullBox(unsigned int(32) boxtype, unsigned int(8) v, bit(24) f) 
    extends Box(boxtype) { 
        unsigned int(8) version = v; 
        bit(24) flags = f; 
 } 

aligned(8) class MovieHeaderBox extends FullBox(‘mvhd’, version, 0) { 
    if (version==1) { 
        unsigned int(64) creation_time; 
        unsigned int(64) modification_time; 
        unsigned int(32) timescale; 
        unsigned int(64) duration; 
    } else { // version==0 
        unsigned int(32) creation_time; 
        unsigned int(32) modification_time; 
        unsigned int(32) timescale; 
        unsigned int(32) duration; 
    } 
    template int(32) rate = 0x00010000; // typically 1.0 
    template int(16) volume = 0x0100; // typically, full volume 
    const bit(16) reserved = 0; 
    const unsigned int(32)[2] reserved = 0; 
    template int(32)[9] matrix = 
    { 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 }; 
    // Unity matrix 
    bit(32)[6] pre_defined = 0; 
    unsigned int(32) next_track_ID; 
} 

我们主要从mvhd中获取文件的创建时间、时长等信息。

具体分析一个mvhd 如下:

ffffff_with_tow_audio.mp4.tar

依然采用上述文件:

00000540: 6a03 8000 0006 d46d 6f6f 7600 0000 6c6d j……moov…lm
00000550: 7668 6400 0000 0000 0000 0000 0000 0000 vhd………….
00000560: 0003 e800 0000 2800 0100 0001 0000 0000 ……(………
00000570: 0000 0000 0000 0000 0100 0000 0000 0000 …………….
00000580: 0000 0000 0000 0000 0100 0000 0000 0000 …………….
00000590: 0000 0000 0000 0040 0000 0000 0000 0000 …….@……..
000005a0: 0000 0000 0000 0000 0000 0000 0000 0000 …………….
000005b0: 0000 0000 0000 0400 0002 2874 7261 6b00 ……….(trak.
000005c0: 0000 5c74 6b68 6400 0000 0300 0000 0000 ..\tkhd………
000005d0: 0000 0000 0000 0100 0000 0000 0000 2800 …………..(.

00 0000 6c atom size 6c == 108

6d 76 68 64 mvhd

version  8 bit 00

flags 00 00 00

creation_time 00 00 00 00

modification_time 00 00 00 00

timescale 00 00  03 e8  == 1000 timescale 是1000

duration 00 0000 28 == 40ms 时长为40ms

rate == 00 01 00 00

volume 01 00

reserved 00 00

reserved 32*2 00 00 00 00  00 00 00 00

matrix 32*9

pre_defined  32*6

next_track_id 00 0000 04

MP4 FFmpeg movenc mvhd box 写入 解析