FLV(Flash Video)是现在非常流行的流媒体格式,由于其视频文件体积轻巧、封装播放简单等特点,使其很适合在网络上进行应用。目前流行的rtmp推流直播,格式都是flv,而且基于http-flv形式来拉流,直播的实时性也很高。

格式

FLV是流媒体封装格式,我们可以将其数据看为二进制字节流。总体上看,FLV包括文件头(Flv Header)和文件体(Flv Body)两部分,其中文件体由一系列的Tag及Tag Size对组成。Tag又可以分成三类:audio,video,script,分别代表音频流,视频流,脚本流(关键字或者文件信息之类)。

FLV File 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 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了

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 类型 描述
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 Tag Header

FLV TagHeader have the following format:

FLV TAG Field 类型 描述
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

FLV Tag Data

如果TagType是8, TagDate数据为音频;如果TagType是9, TagDate数据为视频;TagType是18, TagDate数据为SCRIPTDATAOBJECT.

视频的Tag格式

参考