FLV file (H264 + AAC) format Ultra-detailed analysis

Source: Internet
Author: User
Tags script tag flv file
The H264 video stream and AAC audio stream have been synthesized by hand recently, but there is no open source FFmpeg API to handle audio and video streaming. The method is to imitate ffmpeg in the libavformat/flvenc.c file write code to complete the audio and video of the FLV format encapsulation. In this package process, the FLV file format needs to be very clear. There is a lot of information on the FLV file format on the Web, but most of the information is reproduced or lacks sufficient detail (for example, Avdecoderconfigurationrecord and Audiospecificconfig generation). So I analyze the composition of the FLV file based on LIBAVFORMAT/FLVENC.C in FFmpeg, and analyze the meaning of each byte in the flv file in detail. Each of the following squares represents one byte. The numbers are 16 binary representations (omit 0x), and the characters in the lattice can be replaced with the corresponding 16 digits, which are sometimes more intuitive. Assume that the FLV file contains both video and audio.
The next step is metadata's specific data, consisting of two AMF packages.
The number of metadata elements is tentatively 12 = Audio 5 + video 5 + 2 (Duration and filesize). Additional elements may be added later, and therefore will be returned to modify this value. The order of the metadata elements is not fixed, and the order in FFmpeg is taken here. Each array element in the second AMF package is encapsulated in the form: The first two bytes are the length of the element name, followed by a string of length L, the l+3 byte represents the type of the element value, followed by the corresponding value, and the number of bytes occupied depends on the type of the value. The following (Tag->key,tag->value) do not necessarily appear in all FLV files, depending on the different versions.   Remark: Written to the script tag with metadata information on the official FLV document. However, when making FLV file synthesis, it is found that some FLV files on the web will hide the keyframes information in script tag. Later through the network to check some information, found that keyframes is almost an unofficial standard, that is, civil standards.   Two common tools for operating metadata are flvtool2 and flvmdi, all of which use keyframes as a default meta-information item. On the homepage of Flvmdi (http://www.buraks.com/flvmdi/) There is a description: keyframes: (object) This object is added only if you specify the/k switch. ' KeyFrames ' is known-flvmdi and if/k switch is not specified, ' keyframes ' object would be deleted. ' KeyFrames ' object has 2 arrays: ' Filepositions ' and ' Times '. Both arrays has the same number of elements, which is equal to the number of key frames in the FLV. Values in times array is in ' seconds '. Each correspond to the timestamp of the n ' th key frame. Values in filepositions array is in ' bytes'. Each correspond to the fileposition of the nth key frame video tag (which starts with byte tag type 9). That is, KeyFrames contains 2 content ' filepositions ' and ' Times ', respectively, referring to the file location of the keyframe and the PTS of the keyframe. By keyframes you can set up your own index, and then seek and fast-forward the operation, quickly and effectively jump to the location you want to find the key frame for processing.






So far, the FLV file format has been introduced, FLV format is relatively simple, the header part is very concise, the body part is composed of a tag, tag words are three kinds, script tag generally only one. Finally, a simple graph is used to summarize the FLV file format to end this document.

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.