經過RTMP play分析FLV格式詳解

最近作了一個rtmp中轉服務程序,經過實踐,熟悉rtmp play和push中各種格式,這裏總結一下。git

程序github地址: https://github.com/runner365/rtmp_relaygithub

rtmp play接收報文分析app

第一幀收到的報文:ide

 

1) 0x46 4c 56:可參考文後:參考一ui

字符FLV頭this

2) 0x01 05 spa

Version TypeFlagsReserved TypeFlagsAudio TypeFlagsReserved TypeFlagsVideo3d

這個解析的時候,通常不用管orm

3)0x00 00 00 09視頻

FLV header offset: 也就是從開頭9字節後,纔是FLV真正的報文頭。

4)0x00 00 00 00 

這個是第1幀的PreviousTagSize0(前幀長度),由於是第一幀,因此確定是0;

5)0x08 可參考文後:參考二,參考三

幀開頭第一字節:0x08表示音頻,0x09表示視頻

6)0x00 00 04

幀payload長度:由於音頻第一幀是ASC,因此只有4字節。

7) 0x 00 00 00 00

timestamp,時間戳

8) 0x 00 00 00

streamid,流ID

9) 0x AF 00 13 90

音頻payload: 0xaf00開頭的後面是asc flag, 0xaf01開頭的後面是真正的音頻數據

0x13 90,也就是0b0001 0011 1001 0000, 

ASC flag格式:xxxx xyyy yzzz z000

x字符: aac type,類型2表示AAC-LC,5是SBR, 29是ps,5和29比較特殊ascflag的長度會變成4;

y字符:  sample rate, 採樣率, 7表示22050採樣率

z字符:  通道數,2是雙通道

10) 0x 00 00 00 0F

這個仍是PreviousTagSize1,上一幀長度15bytes

11) 0x09 視頻類型,新的一幀

12)0x00 00 22

視頻幀payload長度

13) 0x00 00 0a 00 

時間戳:這個地方有個大坑,順序是:a[3] a[0] a[1] a[2],最後一位是最高位。

14) 0x00 00 00

streamid, 流id。

15) 0x 17 00 

視頻幀開頭2字節:

0x17 00: 表示內容是SPS和PPS

0x17 01: 表示內容是I-FRAME

0x27:      表示內容是P-FRAME

16) 

0000002bh: 17 00 00 00 00 01 42 C0 1F FF E1 00 0E 67 42 C0 ; ......B??.gB?
0000003bh: 1F 8C 8D 40 F0 28 90 0F 08 84 6A 01 00 04 68 CE ; .實@??.刯...h?
0000004bh: 3C 80 ; <€

第12, 13字節: 0x00 0E是spslen,也就是14字節長度

跳過14字節後,0x01是pps開始的標識,跳過它。

0x00 04是ppslen,也就是4個字節,最後0x68 ce 3c 80就是pps。

 

參考:

1, The FLV header

  Type Comment
Signature UI8 Signature byte always 'F' (0x46)
Signature UI8 Signature byte always 'L' (0x4C)
Signature UI8 Signature byte always 'V' (0x56)
Version UI8 File version (for example, 0x01 for FLV version 1)
TypeFlagsReserved UB [5] Shall be 0
TypeFlagsAudio UB [1] 1 = Audio tags are present
TypeFlagsReserved UB [1] Shall be 0
TypeFlagsVideo UB [1] 1 = Video tags are present
DataOffset UI32 The length of this header in bytes

Signature: FLV 文件的前3個字節爲固定的‘F’‘L’‘V’,用來標識這個文件是flv格式的.在作格式探測的時候,

若是發現前3個字節爲「FLV」,就認爲它是flv文件.

Version: 第4個字節表示flv版本號.

Flags: 第5個字節中的第0位和第2位,分別表示 video 與 audio 存在的狀況.(1表示存在,0表示不存在)

DataOffset : 最後4個字節表示FLV header 長度.

2,FLV body總體

Field Type Comment
PreviousTagSize0 UI32 Always 0
Tag1 FLVTAG First tag
PreviousTagSize1 UI32

Size of previous tag, including its header, in bytes. For FLV version1,

this value is 11 plus the DataSize of the previous tag.

Tag2 FLVTAG Second tag
... ... ...
PreviousTagSizeN-1 UI32 Size of second-to-last tag, including its header, in bytes.
TagN FLVTAG Last tag
PreviousTagSizeN UI32 Size of last tag, including its header, in bytes

FLV header以後,就是 FLV File Body.

FLV File Body是由一連串的back-pointers + tags構成.back-pointers就是4個字節數據,表示前一個tag的size.

3,FLV body細節

 

Field Type Comment
Reserved UB [2] Reserved for FMS, should be 0
Filter UB [1] Indicates if packets are filtered.
0 = No pre-processing required.
1 = Pre-processing (such as decryption) of the packet is
required before it can be rendered.
Shall be 0 in unencrypted files, and 1 for encrypted tags.
See Annex F. FLV Encryption for the use of filters.
TagType UB [5]

Type of contents in this tag. The following types are
defined:
8 = audio
9 = video
18 = script data

DataSize UI24 Length of the message. Number of bytes after StreamID to
end of tag (Equal to length of the tag – 11)
Timestamp UI24 Time in milliseconds at which the data in this tag applies.
This value is relative to the first tag in the FLV file, which
always has a timestamp of 0.
TimestampExtended UI8 Extension of the Timestamp field to form a SI32 value. This
field represents the upper 8 bits, while the previous
Timestamp field represents the lower 24 bits of the time in
milliseconds.
StreamID UI24 Always 0.
AudioTagHeader IF TagType == 8
AudioTagHeader
 
VideoTagHeader IF TagType == 9
VideoTagHeader
 
EncryptionHeader IF Filter == 1
EncryptionTagHeader
 
FilterParams IF Filter == 1
FilterParams
 
Data IF TagType == 8
AUDIODATA
IF TagType == 9
VIDEODATA
IF TagType == 18
SCRIPTDATA
Data specific for each media t
相關文章
相關標籤/搜索