FLV文件格式分析(转)

FLV是一个二进制文件,由文件头(FLV header)和很多tag组成。tag又可以分成三类:audio,video,script,分别代表音频流,视频流,脚本流(关键字或者文件信息之类)。

FLV Header

一般比较简单,包括文件类型之类的全局信息,如图:

文件类型

3bytes

总是FLV(0x46 0x4C 0x56),否则...

版本

1byte

一般是0x01,表示FLV version 1

流信息

1byte

倒数第一bit是1表示有视频,倒数第三bit是1表示有音频,其他都应该是0(有些软件如flvtool2可能造成倒数第四bit是1,不过也没发现有什么不对)

header长度

4bytes

整个文件头的长度,一般是9(3+1+1+4),有时候后面还有些别的信息,就不是9了

FLV Body

FLV body就是由很多tag组成的。

FLV文件里面帧的实体就是tag了。每个tag都可以分为两部分,第一部分包含是tag 类型信息,长度固定为15字节,如图:

第二部分为tag data,也就是flv的数据(有音频,视频,脚本等三类数据),根据不同的tag类型就有不同的数据区,数据区的长度由第一部分的数据区长度字段定义,如图:

previoustagsize

4bytes

前一个tag的长度,第一个tag就是0

tag类型

1byte

三类:

8 -- 音频tag

9 -- 视频tag

18 -- 脚本tag

数据区长度

3bytes

时间戳

3bytes

单位毫秒,如果是脚本tag就是0

扩展时间戳

1byte

作为时间戳的高位

streamsID

3bytes

总是0(不知道干啥用)

数据区

接下来就是下一个tag的内容,其开始的四个字节定义了上个tag的总长度,注意上个tag的总长度中不包括上个tag之前的4个描述再上一个tag的长度的4个字节,如图:

接下来说一下文件尾,在文件尾的最后有四个字节是定义最后一个tag的长度的,如图:

这里我门可以算一下,是00 00 00 DD是221,最后一个tag的长度是221,如图:

下面是不同类型的tag数据区的内容体

Audio tag 数据区

audio信息

1byte

前四位bits表示音频格式:

0 -- 未压缩

1 -- ADPCM

2 -- MP3

5 -- Nellymoser 8kHz momo

6 -- Nellymoser

下面两位bits表示samplerate:

0 -- 5.5kHz

1 -- 11kHz

2 -- 22kHz

3 -- 44kHz

下面一位bit表示每个采样的长度:

0 -- snd8Bit

1 -- snd16Bit

下面一位bit表示类型:

0 -- sndMomo

1 -- sndStereo

audio数据区

不定

video tag 数据区

video信息

1byte

前四位bits表示类型:

1 -- keyframe

2 -- inner frame

3 -- disposable inner frame (H.263 only)

后四位bits表示编码器id:

2 -- Seronson H.263

3 -- Screen video

4 -- On2 VP6

5 -- On2 VP6 without channel

6 -- Screen video version 2

video数据区

不定

script tag 数据区

略n字...

下面是自己写的一段根据上面对FLV文件结构的分析读取FLV播放时间的Delphi代码:


{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Execute;
Edit1.Text:=OpenDialog1.FileName;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
    iFileHandle:   Integer;
    iFileLength:   Integer;
    iBytesRead:   Integer;
    Buffer:   array   of   Byte;
    i:   Integer;
    str1,str2:String;
    tminute,tSecond,tMillisecond,tmptime:Integer;
begin
    if Edit1.Text = '' then
    begin
      ShowMessage('请选择文件!');
      exit;
    end;
    iFileHandle   :=   FileOpen( Edit1.Text ,   fmOpenRead);
    iFileLength   :=   FileSeek(iFileHandle,   0,   2);
    FileSeek(iFileHandle,   0,   0);
    SetLength(Buffer,   iFileLength);
    iBytesRead   :=   FileRead(iFileHandle,   Buffer[0],   iFileLength);
    FileClose(iFileHandle);
    str1   :=   '';
    for   i   := iBytesRead   -   4   to   iBytesRead   -   1   do
    begin
        str1   :=   str1   +   IntToHex(Buffer[i],2);
    end;
    str2   :=   '';
    for   i   := iBytesRead   -   StrToInt('$'+str1) to   iBytesRead   -   (StrToInt('$'+str1)-2)   do
    begin
        str2   :=   str2   +   IntToHex(Buffer[i],2);
    end;
    tMillisecond:=strtoint('$'+str2);
    tminute:=(tMillisecond div 1000) div 60;
    tSecond:=(tMillisecond div 1000) mod 60;
    tmptime:=tMillisecond mod 1000;
    Label2.Caption:=IntToStr(tminute)+'分'+IntToStr(tSecond)+'秒';
    Buffer   :=   nil;
end;

posted @ 2023-04-13 15:10  泽良_小涛  阅读(56)  评论(0编辑  收藏  举报