Media Format analysis-Based on FFMPEG

Source: Internet
Author: User
Tags script tag flv file

We should have written a simple explanation of the media file format before writing it. Today, we will explain the popular media format flv according to the structure flv_demux of ffmpeg flv. c.

FLV is short for flash video. The FLV streaming media format is developed with the release of Flash MX. Because of its extremely small file size and extremely fast loading speed, it is possible to watch video files on the network. currently, mainstream media websites such as Youku and youtube in China use flv format.

Parsing the FLV file structure

FLV is a binary file in the format of FLV header and multiple tags. Tags can be divided into three types: audio, video, and script, which respectively represent audio streams, video streams, and script streams (such as keywords or file information ).

FLV Header

FLV Header information is generally relatively simple, including global information such as the file type. For example:

 

File Type 3 bytes is always FLV (0x46 0x4C 0x56), otherwise it is not in ffmpeg without specifying the file format, this field is also used to detect whether the file belongs to the FLV format.

Version 1 byte is generally 0x01, indicating FLV version 1

Stream information 1 byte the reciprocal first bit indicates that there is video, the last third bit is 1 indicates that there is audio, and other values should be 0 (some software such as flvtool2 may cause the last fourth bit to be 1, however, nothing is wrong)

The header length is 4 bytes. The length of the entire file header is usually 9 (3 + 1 + 1 + 4). Of course, the header field may also contain other information. The length is not 9.

FLV Body

A flv body consists of multiple tags. A tag includes the following information:

Previoustagsize 4 bytes the length of the previous tag. The first tag is 0.

Tag type 1 byte is divided into three types:

* 8 -- audio tag

* 9 -- video tag

* 18 -- script tag

The data zone length is 3 bytes. The timestamp is 3 bytes. Unit: milliseconds. The extended timestamp of 1 bytes is placed at the highest bit. Most timestamp is the data transmission information of the media. If the script tag is 0

StreamsID 3 bytes is always 0 (I Don't Know What To Use)

Data zone: different data zones are available for different tag types.

Script tag:

The script tag is generally expressed in text, such as the metadata information of flv:

It can be seen that it is marked by text, and its header information is:

It can be seen that its type is 18. Time stamp is 0. data size is 33638.

The metadata tag data information is parsed as follows:

There are some media information:

For example, the codec id of a video with height and width. Frame Rate. Audio Information such as sample rate, codec id, sample size, and stereo. And the size of the entire file.

Audio tag Information:

The audio tag information is as follows:

The time stamp is 0 because it is the first audio tag.

Video tag

This is the first tag in the file, so the time stamp is not 0. Because it is a video tag, its type is 9.

Implementation of parsing the flv file format in ffmpeg:

Among them, flv_read_header is mainly used to read some header information from the file and perform initialization work.

Static int flv_read_header (AVFormatContext * s, AVFormatParameters * ap)
{

......

Url_fskip (s-> pb, 4); // remove the flv header.
Flags = get_byte (s-> pb); // read flv video and audio flag information.

......
If (flags & FLV_HEADER_FLAG_HASVIDEO ){
If (! Create_stream (s, 0) // creates a video stream.
Return AVERROR (ENOMEM );
}
If (flags & FLV_HEADER_FLAG_HASAUDIO ){
If (! Create_stream (s, 1) // creates an audio stream
Return AVERROR (ENOMEM );
}

Offset = get_be32 (s-> pb); // get the file header length

......
}

Read Other tags:

Static int flv_read_packet (AVFormatContext * s, AVPacket * pkt)
{

......
For (; url_fskip (s-> pb, 4) {/* pkt size is repeated at end. skip it */
Pos = url_ftell (s-> pb );
Type = get_byte (s-> pb); // you can obtain the tag type. The following three types of flv tags are mentioned:FLV_TAG_TYPE_AUDIO = 0x08, FLV_TAG_TYPE_VIDEO = 0x09, FLV_TAG_TYPE_META = 0x12,
Size = get_be24 (s-> pb); // obtain the length of the tag
Dts = get_be24 (s-> pb );
Dts | = get_byte (s-> pb) <24; // calculate the tag's timestamp, that is, dts Information

......
If (type = FLV_TAG_TYPE_AUDIO) {// determine whether the tag is audio.

......
} Else if (type = FLV_TAG_TYPE_VIDEO) {// determine whether the video tag is used

......
If (flags & 0xf0) = 0x50)/* video info/command frame */
Goto skip;
} Else {
If (type = FLV_TAG_TYPE_META & size> 13 + 1 + 4) // determines whether the tag is meta. if the tag is meta information, the information is stored in a map table.
......

}

Copyright: boshui. Reprinted please indicate the source: http://www.cnblogs.com/qingquan/

Related Article

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.