目录

走进音视频的世界视频封装格式

目录

走进音视频的世界——视频封装格式

音视频的时长怎么获取,音视频的封面怎么获取,音视频的格式怎么获取呢?这些信息都以特定格式存储在文件开头或者结尾,称为多媒体信息或者多媒体元数据。通用的封装格式由:文件标识头+多媒体信息+音视频(字幕)轨+视频帧索引块组成,如果是纯音频,后面可能还有歌词。音视频的封装格式就是通过解析文件标识头进行判断的,然后解析多媒体信息从而获取时长,再解析视频帧索引块,最后根据索引块去获取对应时间戳的视频帧。

音视频封装格式存储的字段包括:时长、码率、音视频编码器、分辨率(宽x高)、帧率、像素格式、旋转角度、采样率、声道数等等。其中视频专有的字段是分辨率、帧率、像素格式、旋转角度,而音频专有的字段是采样率、声道数。具体的字段解析可以参考: 。

常见的视频格式有:mp4、mov、3gp、mkv、webm、flv、avi、mpg、wmv、ts等等。其中mp4、mov、3gp同属一个协议簇,目前mp4最为流行,mp4全称为MPEG-4,由国际标准化组织和国际电工委员会下属的动态图像专家组(Moving Picture Experts Group)制定,具体协议可参考: ;mkv与webm公用封装格式: ,对于高清视频而言,mkv/webm最受欢迎;而avi是比较古老的格式,音视频流交错(Audio Video Interleave),可以封装各种编码格式的音视频流;mpg属于ps的一种封装格式;wmv(Windows Media Video)是微软推出的视频编解码格式统称,采用ASF(Advance System Format)作为容器,基于Object对象进行封装;而ts的全称为MPEG2-TS,即为Transport Stream的缩写,具体可参考 ,作用于传输层,主要用于实时传输的节目,HLS直播协议就是基于ts切片来传输视频流的,主要特点是从视频流任一片段都可独立解码播放;ps与ts类似,全称为MPEG-PS,即为Program Stream的缩写,用于存储固定时长的节目。视频格式如下图所示:

https://i-blog.csdnimg.cn/blog_migrate/aab5fd6b0d8b3de72c5914a14289c6dc.png

整个解封装流程:从读取文件头判断视频格式开始,然后选择对应的Extractor,解析多媒体信息,再解析视频帧的索引块,最后根据索引去定位并读取音视频数据。如下图所示:

https://i-blog.csdnimg.cn/blog_migrate/41e435001b106cdf8a535816ef58679e.png

图1—视频解封装流程图

mp4作为目前最流行的视频封装格式,也是本篇文章的男一号主角,下面将围绕mp4格式进行展开分析。mp4是由一系列的box组成(在quick time协议中,称为atom),box又由Header和Data组成,box的结构如图2所示:

https://i-blog.csdnimg.cn/blog_migrate/af6eace7eb219fc38eac42089d2bca62.png

图2—box的宏观结构

而Header由size、type、largeSize、extendType组成,其中size和type是必要字段,如表1所示:

sizetypelargeSizeextendType
4 bytes4 bytes8 bytes16 bytes

表1—通用Header结构

full box的Header多两个字段:version、flag,一般是track box采用full box形式,如表2所示:

sizetypelargeSizeextendTypeversionflag
4 bytes4 bytes8 bytes16 bytes1 byte3 bytes

表2—full box的Header结构

box分为normal box、full box、large box、extend box。如果size为1,那么表明该box为large box,使用largeSize来存储box的大小;如果size为0,那么表明该box是文件的最后一个box;如果box的类型为uuid,那么表明该box是扩展box。如下面代码段所示:

aligned(8) class Box (unsigned int(32) boxtype,
    optional unsigned int(8)[16] extended_type) {
    unsigned int(32) size;
    unsigned int(32) type = boxtype;
    if (size==1) {
        unsigned int(64) largesize;
    } else if (size==0) {
        // box extends to end of file
    }
    if (boxtype==uuid) {
        unsigned int(8)[16] usertype = extended_type;
    }
}

moov box作为mp4格式的重要组成部分,根据moov box与mdat box的相对位置,分为moov前置和moov后置。如下面图3、图4所示:

https://i-blog.csdnimg.cn/blog_migrate/e5294499203d8da8ec47fd378be361cc.jpeg

图3—mp4结构图(moov前置)

https://i-blog.csdnimg.cn/blog_migrate/c8da76b212c44c87c36971a28b03010d.jpeg

图4—mp4结构图(moov后置)

通常情况下,mp4的moov都是在mdat前面的;一般只有实时录制的mp4视频,moov才在mdat的后面。除了moov box,还有ftyp box、moof box、mdat box、free box、meta box等等,如下表所示,对各种box的介绍与描述:

ftypfile type and compatibility
pdinprogressive download information
moovcontainer for all the metadata
mvhdmovie header, overall declarations
trakcontainer for an individual track or stream
tkhdtrack header, overall information about the track
treftrack reference container
edtsedit list container
elstan edit list
mdiacontainer for the media information in a track
mdhdmedia header, overall information about the media
hdlrhandler, declares the media (handler) type
minfmedia information container
vmhdvideo media header, overall information (video track only)
smhdsound media header, overall information (sound track only)
hmhdhint media header, overall information (hint track only)
nmhdNull media header, overall information (some tracks only)
dinfdata information box, container
drefdata reference box, declares source(s) of media data in track
stblsample table box, container for the time/space map
stsdsample descriptions (codec types, initialization etc.)
stts(decoding) time-to-sample
ctts(composition) time to sample
stscsample-to-chunk, partial data-offset information
stszsample sizes (framing)
stz2compact sample sizes (framing)
stcochunk offset, partial data-offset information
co6464-bit chunk offset
stsssync sample table (random access points)
stshshadow sync sample table
padbsample padding bits
stdpsample degradation priority
sdtpindependent and disposable samples
sbgpsample-to-group
sgpdsample group description
subssub-sample information
mvexmovie extends box
mehdmovie extends header box
trextrack extends defaults
ipmcIPMP Control Box
moofmovie fragment
mfhdmovie fragment header
traftrack fragment
tfhdtrack fragment header
truntrack fragment run
sdtpindependent and disposable samples
sbgpsample-to-group
subssub-sample information
mframovie fragment random access
tfratrack fragment random access
mfromovie fragment random access offset
mdatmedia data container
freefree space
skipfree space
udtauser-data
cprtcopyright etc.
metametadata
hdlrhandler, declares the metadata (handler) type
dinfdata information box, container
drefdata reference box, declares source(s) of metadata items
ipmcIPMP Control Box
ilocitem location
iproitem protection
sinfprotection scheme information box
frmaoriginal format box
imifIPMP Information box
schmscheme type box
schischeme information box
iinfitem information
xmlXML container
bxmlbinary XML container
pitmprimary item reference
fiinfile delivery item information
paenpartition entry
fparfile partition
fecrFEC reservoir
segrfile delivery session group
gitngroup id to name
tseltrack selection
mecoadditional metadata container
meremetabox relation

表3—mp4的各种box描述

ftyp box:mp4视频标识头,包含major brand、minor version、compatible brands。其中major brand一般为isom,而compatible brands包括isom、iso2、avc、mp41、mp42等。

moov box:存储多媒体信息,嵌套着movie box(mvhd)、track box(trak box)、usedata box(udat);而track box分为视频轨、音频轨、字幕轨,如果有多语言,就会对应有多音轨;trak/mdia/minf/stbl/stsd存储的是音视频编码器信息,比如视频轨的是avc,音频轨是mp4a;trak/mdia/minf/stbl/stsz存储的是视频帧size;trak/mdia/minf/stbl/stco存储的是chunk offset。

mdat box:音视频数据,根据moov及其嵌套box解析出来的视频帧索引,去定位关键帧,然后根据帧类型读取音视频数据。

音视频学习和音视频处理项目可参考: