FLV Package Principle---http://blog.csdn.net/leixiaohua1020/article/details/17934487The packaging principle of the FLV format, affixed to the use of auxiliary learning.
FLV (Flash video) is a popular streaming media format designed and developed by Adobe, making it suitable for use on the Internet due to its compact size and simple packaging. In addition, FLV can play with Flash Player, and the Flash Player plugin is already installed on most browsers around the world, making it easy to play FLV videos over the web. At present, the mainstream video sites such as Youku, Tudou, Lok-Net and other sites without exception, the use of the FLV format. The file suffix in the FLV encapsulation format is usually ". flv".
On the whole, FLV consists of the file header and the file body, where the file body consists of a series of tags. So an FLV file is as shown in Figure 1 structure.
Figure 1: File structure (schematic)
Each tag is preceded by the previous tag size field, which indicates the size of the previous tag. The type of tag can be video, audio, and script, and each tag can contain only one of the above three types of data. Figure 2 shows the detailed structure of the FLV file.
Figure 2. FLV file Structure (detail)
The following is a detailed description of the three tags of the tag data section of the structure.
(a) Audio tag data structure (video tag)
The 1th byte of the audio tag starts with the parameter information of the audio data, starting with the 2nd byte as the audio stream data. The structure is shown in Figure 3.
Figure 3. Audio TAG Data structure
The number of the first 4 digits of the 1th byte indicates the type of audio encoding. As shown in table 1.
Table 1. Audio Encoding Type
Value |
Meaning |
0 |
Linear Pcm,platform Endian |
1 |
ADPCM |
2 |
MP3 |
3 |
Linear Pcm,little Endian |
4 |
Nellymoser 16-khz Mono |
5 |
Nellymoser 8-khz Mono |
6 |
Nellymoser |
7 |
G.711 A-law Logarithmic PCM |
8 |
G.711 Mu-law Logarithmic PCM |
9 |
Reserved |
10 |
Aac |
14 |
MP3 8-khz |
15 |
Device-specific Sound |
The number of the 1th byte of 第5-6位 represents the audio sample rate. As shown in table 2.
Table 2. Audio sample Rate
Value |
Meaning |
0 |
5.5kHz |
1 |
11KHz |
2 |
KHz |
3 |
-KHz |
PS: From the table above, it can be found that the FLV package format does not support 48KHz sample rate.
The 7th bit of the 1th byte represents the audio sampling precision. As shown in table 3.
Table 3. Audio Sampling Accuracy
Value |
Meaning |
0 |
8bits |
1 |
16bits |
The 8th bit of the 1th byte represents the audio type.
Table 4. Audio type
Value |
Meaning |
0 |
Sndmono |
1 |
Sndstereo |
(b) Video tag data structure (videos tag)
The video tag also uses the beginning of the 1th byte to contain the video data of the parameter information, from the 2nd byte to the video stream data. The structure is shown in Figure 4.
Figure 4. Video TAG Data structure
The number of the first 4 digits of the 1th byte represents the frame type. As shown in table 5.
Table 5. Frame type
Value |
Meaning |
1 |
Keyframe (for avc,a seekable frame) |
2 |
Inter frame (for avc,a nonseekable frame) |
3 |
Disposable Inter frame (h.263 only) |
4 |
Generated keyframe (reserved for server use) |
5 |
Video Info/command Frame |
The second 4 digits of the 1th byte represent the video encoding type. As shown in table 6.
Table 6. Video Encoding Type
Value |
Meaning |
1 |
JPEG (currently unused) |
2 |
Sorenson H.263 |
3 |
Screen Video |
4 |
On2 VP6 |
5 |
ON2 VP6 with alpha channel |
6 |
Screen Video Version 2 |
7 |
Avc |
(c) Script TAG data structure (control frame)
This type of tag is often referred to as metadata tag, which puts some meta-data information about FLV video and audio such as duration, width, height, and so on. Typically, the type tag appears after the file header as the first tag, and there is only one. The structure is shown in Figure 5.
Figure 5. Script TAG Data structure
First AMF Package:
The 1th byte represents the AMF package type, which is generally always 0x02, representing the string. The 第2-3个 byte is the UI16 type value, which identifies the length of the string, generally always 0x000a ("onmetadata" length). The latter byte is a specific string, which is generally always "onmetadata" (6f,6e,4d,65,74,61,44,61,74,61).
A second AMF package:
The 1th byte represents the AMF package type, which is generally always 0x08, representing the array. The 第2-5个 byte is a UI32 type value that represents the number of array elements. The following is the encapsulation of each array element, and the array element is a pair of element names and values. Common array elements are shown in table 7.
Table 7. Common metadata
Value |
Meaning |
Duration |
& nbsp; duration |
Width |
Video width |
H Eight |
Video height |
videodatarate |
Video bitrate |
framerate |
Video frame rate |
Vide Ocodecid |
video encoding |
audiosamplerate |
&nb SP; audio sample rate |
audiosamplesize |
Audio sampling accuracy |
Stereo |
Whether it is stereo |
Audiocodecid |
Audio encoding method |
FileSize |
File size |