简介:
FLV(Flash Video)是现在非常流行的流媒体格式,由于其视频文件体积轻巧、封装播放简单等特点,使其很适合在网络上进行应用,目前主流的视频网站无一例外地使用了FLV格式。另外由于当前浏览器与Flash Player紧密的结合,使得网页播放FLV视频轻而易举,也是FLV流行的原因之一。可以通过Adobe公司的RTMP(Real Time Messaging Protocol)协议连接到Adobe Flash Media Server上面发布媒体信息。FLV视频格式是Adobe公司设计开发的,目前已经免费开放,现在的版本是v10。下面我们就了解一下FLV文件格式。
FLV是流媒体封装格式,我们可以将其数据看为二进制字节流。总体上看,FLV包括文件头(Flv Header)和文件体(Flv Body)两部分,其中文件体由一系列的Tag及Tag Size对组成。Tag又可以分成三类:audio,video,script,分别代表音频流,视频流,脚本流(关键字或者文件信息之类)。
一、FLV Header
一般比较简单,包括文件类型之类的全局信息,如图:
FLV Header 一共九个字节,含义如下:
第1-3字节为文件标识(Signature),总为“FLV”(0x46 0x4C 0x56),第4字节为版本,目前为1(0x01)。第5个字节的前5位保留必须为0,第6位表示是否存在音频Tag,第7位保留必须为0,第8位表示是否存在视频Tag。第6-9个字节为UI32类型的值,表示从File Header开始到File Body开始的字节数,版本1中总为9。
FLV Header包含9个字节,含义如下表所示:
FLV Header Field | 描述 |
文件类型(3byte) | 'F' (0x46) 'L' (0x4C) 'V' (0x56) |
版本(1byte) | File Version(For example 0x01 for FLV version 1) |
流信息(1byte) | UB[7]~UB[3]、UB[2]=1 Audio,UB[1]总为0,UB[0] = 1 Video |
Header长度(4bytes) | 整个文件头的长度,一般是9(3+1+1+4),有时候后面还有些别的信息,就不是9了 |
二 、The FLV File Body
After the FLV Header,the remainder of an FLV file consists of alternating back-pointers and tags. They interleave as shown in the following table:
FLV File Body Field | Type | Comment |
PreviousTagSize0 | UI32 | Always 0 |
Tag1 | FLVTAG | First tag |
PreviousTagSize1 | UI32 | Size of previous tag, including its header. For FLV version 1,this value is 11 plus the DataSize of the previous tag. |
Tag2 | FLVTAG | Second tag |
… | … | … |
PreviousTagSizeN-1 | UI32 | Size of second-to-last tag |
TagN | FLVTAG | Last tag |
PreviousTagSizeN | UI32 | Size of last tag |
FLV TagHeader have the following format:
FLVTAG Field | Type | Comment |
TagType | UI8 | Type of this tag. Values are: 8:audio 9:video 18:script data |
DataSize | UI24 | Length of the data in the Data field |
Timestamp | UI24 | Time in milliseconds at which the data in the this tag applies. The valve is relative to the first tag in the FLV file, which always has a timestamp of 0. |
TimestampExtended | UI8 | Extension of Timestamp field to form a UI32 value. This field represents the upper 8 bits, while the previous Timestamp filed represents the lower 24 bits of the time in milliseconds. |
StreamID | UI24 | Always 0 |
In playback, the time sequencing of FLV tags depends on the FLV timestamps only. Any timing mechanisms built into the playload data format are ignored.
FLV TagData的说明:
如果TagType是8, TagDate数据为音频;如果TagType是9, TagDate数据为视频;TagType是18, TagDate数据为SCRIPTDATAOBJECT.
FLV body就是由很多tag组成的。FLV文件里面帧的实体就是tag了。每个tag都可以分为两部分,第一部分包含是tag 类型信息,长度固定为15字节(PreviousTagSize + The Size of FLV TagHeader ),如图:
第二部分为tag data,也就是flv的数据(有音频,视频,脚本等三类数据),根据不同的tag类型就有不同的数据区,数据区的长度由第一部分的数据区长度字段定义,如图:
接下来就是下一个tag的内容,其开始的四个字节定义了上个tag的总长度,注意上个tag的总长度中不包括上个tag之前的4个描述再上一个tag的长度的4个字节,如图:
接下来说一下文件尾,在文件尾的最后有四个字节是定义最后一个tag的长度的,如图:
这里我门可以算一下,是00 00 00 DD是221,最后一个tag的长度是221,如图: