[Standard]/etc

FLV File Format

하늘을닮은호수M 2007. 11. 7. 18:05
반응형

FLV File Format

A Flash Video file (.FLV) consists of a short header, and then interleaved audio, video, and metadata packets. The audio and video packets are stored very similarly to those in SWF, and the metadata packets consist of AMF data.

FLV Header

Field Data Type Example Description
Signature byte[3] “FLV” Always “FLV”
Version uint8 “\x01” (1) Currently 1 for known FLV files
Flags uint8 bitmask “\x05” (5, audio+video) Bitmask: 4 is audio, 1 is video
Offset uint32_be “\x00\x00\x00\x09” (9) Total size of header (always 9 for known FLV files)

FLV Stream

Field Data Type Example Description
PreviousTagSize uint32_be “\x00\x00\x00\x00” (0) Always 0

Then a sequence of tags followed by their size until EOF.

FLV Tag

Field Data Type Example Description
Type uint8 “\x12” (0×12, META) Determines the layout of Body, see below for tag types
BodyLength uint24_be “\x00\x00\xe0” (224) Size of Body (total tag size - 11)
Timestamp uint24_be “\x00\x00\x00” (0) Timestamp of tag (in milliseconds)
TimestampExtended uint8 “\x00” (0) Timestamp extension to form a uint32_be. This field has the upper 8 bits.
StreamId uint24_be “\x00\x00\x00” (0) Always 0
Body byte[BodyLength] ... Dependent on the value of Type

Previous tag size

Field Data Type Example Description
PreviousTagSize uint32_be “\x00\x00\x00\x00” (0) Total size of previous tag, or 0 for first tag

FLV Tag Types

Tag code Name Description
0×08 AUDIO Contains an audio packet similar to a SWF SoundStreamBlock plus codec information
0×09 VIDEO Contains a video packet similar to a SWF VideoFrame plus codec information
0×12 META Contains two AMF packets, the name of the event and the data to go with it

FLV Tag 0x08: AUDIO

The first byte of an audio packet contains bitflags that describe the codec used, with the following layout:

Name Expression Description
soundType (byte & 0×01) » 0 0: mono, 1: stereo
soundSize (byte & 0×02) » 1 0: 8-bit, 1: 16-bit
soundRate (byte & 0x0C) » 2 0: 5.5 kHz, 1: 11 kHz, 2: 22 kHz, 3: 44 kHz
soundFormat (byte & 0xf0) » 4 0: Uncompressed, 1: ADPCM, 2: MP3, 5: Nellymoser 8kHz mono, 6: Nellymoser

The rest of the audio packet is simply the relevant data for that format, as per a SWF SoundStreamBlock.

FLV Tag 0x09: VIDEO

The first byte of a video packet describes contains bitflags that describe the codec used, and the type of frame

Name Expression Description
codecID (byte & 0x0f) » 0 2: Sorensen H.263, 3: Screen video, 4: On2 VP6, 5: On2 VP6 Alpha, 6: ScreenVideo 2
frameType (byte & 0xf0) » 4 1: keyframe, 2: inter frame, 3: disposable inter frame

In some cases it is also useful to decode some of the body of the video packet, such as to acquire its resolution (if the initial onMetaData META tag is missing, for example).

TODO: Describe the techniques for acquiring this information. Until then, you can consult the flashticle sources.

FLV Tag 0x12: META

The contents of a meta packet are two AMF packets. The first is almost always a short uint16_be length-prefixed UTF-8 string (AMF type 0×02), and the second is typically a mixed array (AMF type 0×08). However, the second chunk typically contains a variety of types, so a full AMF parser should be used.

HTTP Streaming

It is possible to semi-stream flv over http using a trick which sends the normal headers then skips forward to a desired point in the file and moves the timestamps forward accordingly.

The FLV Metadata injector is a free closed-source tool which can inject this information into a FLV file. A sample php script and fla is available at FlashComGuru

Another tool that you can use to stream flv files using http is using Flv4PHP this tool is both a FLV Metadata injector and a stream tool, using php 4.x. this Project is GPL.

반응형

'[Standard] > etc' 카테고리의 다른 글

Digital TV의 양대 규격 ATSC & DVB  (0) 2007.11.07
Media Server/Service를 위한 Curriculum  (0) 2007.10.26