The conversion of FLV stream to standard H264 and AAC in rtmp

Source: Internet
Author: User
Tags flv file

This time, the work of the need, on the rtmp to do the FLV stream to the standard H264, AAC conversion, the server is open source project Crtmpserver, client flex writing, Video coding H264, Audio coding AAC, now some of the agreements related to the following things record.

I. Analysis of FLV data

Let's take an FLV file to briefly analyze the format of the FLV data

FLV standard document Download address http://www.cnblogs.com/chef/archive/2012/07/18/2597003.html

FLV file Analyzer flvprase download address http://www.cnblogs.com/chef/archive/2012/07/18/2596930.html

This is not the point of this article, this piece is skipped. I'm sure you can read the FLV data format against the FLV standard document, and I strongly recommend that the friend who wants to learn this knowledge get the job done first.

Second, analyze the rtmp uplink h264 video stream

Save the uplink video as a binary file on the server (note that you must save the data in binary form) the following figure (the binary data shown in the picture has 16 columns of data per row, that is, from 0 to F, if the display is incomplete, please open the picture separately)

The tool I use is notepad++ (and install binary view plugin)

If you have to do the first step of the work, it is not difficult to see rtmp in the FLV video stream is a next one of the videos tag--is the FLV tag to remove the header information, only the video tag content.

We analyze the FLV standard document one by one

17:1-keyframe 7-AVC

00:AVC Sequence Header--AVC packet type

00:composition TIME,AVC, all 0, meaningless

Because AVC packet TYPE=AVC sequence header, the next is Avcdecoderconfigurationrecord content

Configurationversion = 01

Avcprofileindication = 42

Profile_compatibility = 00

Avclevelindication = 1f

Lengthsizeminusone = FF--the number of bytes used in Nalu packet length data in flv, (Lengthsizeminusone & 3) +1, when the actual test was found to be FF, the result is 4, and this data is also mentioned below

Numofsequenceparametersets = E1-the number of SPS, Numofsequenceparametersets & 0x1F, the actual test found that the total is E1, the calculation results are 1

Sequenceparametersetlength =--The length of the SPS, 2 bytes, calculated 49

Sequenceparametersetnalunits = A8 (1f) (f) "B8" in which you are not in the same. 8c C3 B4 4d-SPS, for the 49 bytes just calculated, the SPS contains long, wide information about the video's length and width

Numofpictureparametersets = number of PPS, the actual test found that the total is E1, the result is 1

Pictureparametersetlength =-Length of PPS

Pictureparametersetnalunits = CE--PPS

Next up is a new packet of VIDEOTAG data.

17:1-keyframe 7-AVC

01:AVC Nalu

00:composition TIME,AVC, all 0, meaningless

Because the Avcpacket type = AVC Nalu, the next step is one or more Nalu

Each Nalu package is preceded by (Lengthsizeminusone & 3) + 1 bytes of nal packet length description (mentioned above, remember), preceded by a result of 4 bytes

02:2-Nalu length

10:nal Bag

Here a little bit of nalu knowledge is inserted, each Nalu the first 5 digits of each byte is marked with the type of the NAL package, that is nal Nal_unit_type

#define NALU_TYPE_SLICE 1
#define NALU_TYPE_DPA 2
#define NALU_TYPE_DPB 3
#define NALU_TYPE_DPC 4
# Define NALU_TYPE_IDR 5
#define NALU_TYPE_SEI 6
#define NALU_TYPE_SPS 7
#define NALU_TYPE_PPS 8
#define Nalu_type_aud 9//Access separator
#define NALU_TYPE_EOSEQ
#define Nalu_type_eostream
#define Nalu_type_fill 12

09&0x1f=9, accessing the cell separator

The SPS header byte we resolved earlier is 67,67&0x1f = 7,pps header byte is 68,68&0x1f=8, just can correspond to.

00 00 00 29: Description The next nal packet length is 41

: 06&0x1f=6-SEI

3c D0: Next nal packet length

65 88 80 ... : 65&0x1f=5--I-frame data

This is the end of the packet of video tag analysis, followed by some of the I frame corresponding to the P-frame data, see the following figure

Look at the 00003d80 line, the previous content has been to 4f 7f is the last video tag content, that is 65 88 80 of the previous I-frame data pull, 27 is a new video tag

27:2-inter frame is P frames, 7-CODECID=AVC

01:avcpacket type = AVC Nalu

00:composition TIME,AVC, all 0, meaningless

00 00 00 02 09 30: As the analysis above, pull, 2 bytes of NAL packet, access cell separator

00 00 00 11:17 bytes of NAL packet

(a) 80:06&0x1f=6-sei (a) (0c)

85:nal Packet Data length

9a 02 ... : 41&0x1f=1,p Frame data

Iii. Conversion

Roughly summarize the FLV H264 Stream, in order

1, a video tag, contains information: Sps,pps, Access unit separator, Sei,i frame Package

2, one or more video tag, contains information: Access unit separator, Sei,p frame package can be multiple

Cycle 1, 2

Here is the explanation, do the conversion this step, we only need to get all of the Videotag, one of the NAL package on it, as it is I frame, p frame and other types, in fact, we do not need to care, here is just for better analysis of the data.

The H264 Nalu and Nalu are separated by 00 00 01 (or 00 00 00 01), and the format after which we form H264 is

1 SPS 00 00 00 01 The Access unit delimiter for A/p..... ......... of the ........ (P frame number is indefinite)

Cycle 1

The Access unit separator and the SEI are not required, the H264 is written in binary form, the Elecard Streameye can be played, the download address

Http://www.cnblogs.com/chef/archive/2012/07/18/2597008.html

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.