通过RTMP play分析FLV格式详解

来源:互联网 发布:单片机定时器工作原理 编辑:程序博客网 时间:2024/06/06 14:02

最近做了一个rtmp中转服务程序,通过实践,熟悉rtmp play和push中各类格式,这里总结一下。

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

rtmp play接收报文分析

第一帧收到的报文:

 

1) 0x46 4c 56:可参考文后:参考一

字符FLV头

2) 0x01 05 

Version TypeFlagsReserved TypeFlagsAudio TypeFlagsReserved TypeFlagsVideo

这个解析的时候,一般不用管

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

 TypeCommentSignatureUI8Signature byte always 'F' (0x46)SignatureUI8Signature byte always 'L' (0x4C)SignatureUI8Signature byte always 'V' (0x56)VersionUI8File version (for example, 0x01 for FLV version 1)TypeFlagsReservedUB [5]Shall be 0TypeFlagsAudioUB [1]1 = Audio tags are presentTypeFlagsReservedUB [1]Shall be 0TypeFlagsVideoUB [1]1 = Video tags are presentDataOffsetUI32The 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整体

FieldTypeCommentPreviousTagSize0UI32Always 0Tag1FLVTAGFirst tagPreviousTagSize1UI32

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

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

Tag2FLVTAGSecond tag.........PreviousTagSizeN-1UI32Size of second-to-last tag, including its header, in bytes.TagNFLVTAGLast tagPreviousTagSizeNUI32Size 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细节

 

FieldTypeCommentReservedUB [2]Reserved for FMS, should be 0FilterUB [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.TagTypeUB [5]

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

DataSizeUI24Length of the message. Number of bytes after StreamID to
end of tag (Equal to length of the tag – 11)TimestampUI24Time 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.TimestampExtendedUI8Extension 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.StreamIDUI24Always 0.AudioTagHeaderIF TagType == 8
AudioTagHeader VideoTagHeaderIF TagType == 9
VideoTagHeader EncryptionHeaderIF Filter == 1
EncryptionTagHeader FilterParamsIF Filter == 1
FilterParams DataIF TagType == 8
AUDIODATA
IF TagType == 9
VIDEODATA
IF TagType == 18
SCRIPTDATAData specific for each media t
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩子眼睛红有眼屎怎么办 狗狗的肉垫粗糙怎么办 狗狗眼睛变蓝色怎么办 脸被太阳晒伤了怎么办 皮肤晒伤红肿痒怎么办 3岁儿童频繁眨眼怎么办 狗狗的眼睛红肿怎么办 脸过敏发红怎么办不痒 上眼皮红肿痒是怎么办 眼睛痒了几天了怎么办 眼睛肿了还痒怎么办 孩子脸上有红血丝怎么办 脸上长了红血丝怎么办 指甲受创出血了怎么办 手指被挤压紫了怎么办 眼睛撞了有淤血怎么办 下眼底有小白点怎么办 狗的白眼球充血怎么办 眼球有出血点是怎么办 吃阿胶上火了该怎么办 胎儿胼胝体发育不良怎么办 鸡眼看到硬芯了怎么办 小脚趾起茧子疼怎么办 脚起老茧很痛怎么办 化疗后骨髓抑制严重怎么办 胃炎引起的胃胀怎么办 胃病胀肚子很鼓怎么办 小孩淋巴结发炎肚子疼痛怎么办 顺产后子宫脱垂怎么办 顺产完子宫脱垂怎么办 额头长了个鱼鳞怎么办 脸上长了很多痣怎么办 做过狐臭的疤痕怎么办 痤疮留下的红印怎么办 脸上疤掉了黑印怎么办 脸上有黑色的疤怎么办 一只眼睛外斜视怎么办 残币银行不给换怎么办 手上有多套房的怎么办 长了两层脚指甲怎么办 指甲长了两层怎么办