Analyze FLV format with rtmp play

Source: Internet
Author: User
Tags time in milliseconds flv file

Recently made a RTMP transit service program, through the practice, familiar with rtmp play and push in various formats, summarized here.

Program GitHub Address: Https://github.com/runner365/rtmp_relay

RTMP Play Receive Message analysis

Message received at first frame:

1) 0x46 4c: Refer to the following article:

Character FLV Header

2) 0x01 05

Version typeflagsreserved Typeflagsaudio typeflagsreserved typeflagsvideo

When this is resolved, it is generally not necessary to control

3) 0x00 00 00 09

FLV Header offset: That is, from the beginning of 9 bytes, is the FLV true message header.

4) 0x00 00 00 00

This is the 1th frame of the PREVIOUSTAGSIZE0 (front frame length), because it is the first frame, so it must be 0;

5) 0x08 can refer to the following text: Reference two, reference three

First byte at the beginning of the frame: 0x08 for audio, 0x09 for video

6) 0x00 00 04

Frame payload Length: Because the first frame of the audio is ASC, it is only 4 bytes.

7) 0x 00 00 00 00

Timestamp, time stamp

8) 0x 00 00 00

Streamid, Stream ID

9) 0x AF 00 13 90

The audio PAYLOAD:0XAF00 begins with ASC flag, and the 0xaf01 is followed by the real audio data

0x13 90, i.e. 0b0001 0011 1001 0000,

ASC flag format: xxxx xyyy yzzz z000

X characters: AAC type, 2 for AAC-LC,5 for SBR, 29 for ps,5 and 29 for special Ascflag will become 4;

Y-Character: sample rate, sample rates, 7 = 22050 sample Rate

Z Character: Number of channels, 2 is dual channel

0x xx 0F

This is still PreviousTagSize1, last frame length 15bytes

One) 0x09 video type, new frame

0x00 00 22

Video Frame payload Length

0x00 xx 0a 00

Time stamp: This place has a big pit, the order is: A[3] a[0] a[1] a[2], the last one is the highest bit.

) 0x00 00 00

Streamid, Stream ID.

0x 17 00

Video frame begins with 2 bytes:

0x17 00: Indicates that the content is SPS and PPS

0x17 01: Indicates that the content is I-frame

0X27: Indicates that the content is P-frame

16)

0000002bh:17 xx C0 1F FF E1 0E C0; ...... B??. GB?
0000003bh:1f 8C 8D F0 0F (6A)----(CE) 刯 ... h?
0000004BH:3C 80; <€

12th, 13 bytes: 0x00 0E is Spslen, which is 14 byte length

After skipping 14 bytes, 0x01 is the identity of the PPS start, skipping it.

0x00 04 is Ppslen, which is 4 bytes, and finally 0x68 CE 3c 80 is pps.

Reference:

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 is present
Typeflagsreserved UB [1] shall be 0
Typeflagsvideo UB [1] 1 = Video tags is present
Dataoffset UI32 The length of this header in bytes

Signature: The first 3 bytes of the FLV file are fixed 'F'L'V', which identifies the file as an FLV format. When doing a format probe,

If the first 3 bytes are found to be "flv", it is considered to be an FLV file.

version: The 4th byte represents the FLV version number.

Flags: the No. 0 and 2nd digits of the 5th byte, respectively, indicate the presence of video and audio. (1 means present, 0 means not present)

Dataoffset : The last 4 bytes represent the length of the FLV header.

2,FLV Body Overall

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 one 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 it header, in bytes

After the FLV header , it is the flv File Body.

The FLV File Body is composed of a series of back-pointers + tags. The back-pointers is 4 bytes of data representing the size of the previous tag.

3,flv Body Details

Field Type Comment
Reserved UB [2] Reserved for FMS, should is 0
Filter UB [1] Indicates if packets is filtered.
0 = No pre-processing required.
1 = pre-processing (such as decryption) of the packet is
Required before it can be rendered.
Shall is 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 the this tag. The following types is
Defined
8 = Audio
9 = Video
= 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 "the data" this tag applies.
This value was relative to the first tag in the FLV file, which
Always have 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 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

Analyze FLV format with rtmp play

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.