The FLV file format is Annex E of Adobe’s Video File Format Specification, version
10.1. FLV uses the simple data types of the SWF specification (UI8, UB[n], STRING and so
on), stored big-endian rather than little-endian, and adds one, UI24: an unsigned 24-bit
number. So the number 300, stored as 2C 01 in a SWF file, is
01 2C in an FLV file. UB[n] fields are bit fields, taken from the most
significant bit of each byte down.
Each tag type forms one stream. Adobe’s FLV has at most one audio stream and one video stream, kept in step by the tags’ times; Enhanced FLV adds multitrack packets that carry more.
File header
| Offset | Type | Field | Meaning |
|---|---|---|---|
| 0 | UI8 | Signature | F (0x46). |
| 1 | UI8 | Signature | L (0x4C). |
| 2 | UI8 | Signature | V (0x56). |
| 3 | UI8 | Version | The file version: 1 for FLV version 1, the only version defined. |
| 4 | UB[5] | TypeFlagsReserved | Bits 7 to 3. Shall be 0. |
| 4 | UB[1] | TypeFlagsAudio | Bit 2 (0x04): 1 when audio tags are present. |
| 4 | UB[1] | TypeFlagsReserved | Bit 1. Shall be 0. |
| 4 | UB[1] | TypeFlagsVideo | Bit 0 (0x01): 1 when video tags are present. |
| 5 | UI32 | DataOffset | The length of this header in bytes: 9 for version 1. It is there to make room for larger headers in later versions, so a reader starts the body at this offset. |
File body
After the header the file alternates back-pointers and tags:
| Field | Type | Meaning |
|---|---|---|
| PreviousTagSize0 | UI32 | Always 0. |
| Tag1 | FLVTAG | The first tag. |
| PreviousTagSize1 | UI32 | The size of the tag before it, header included: for version 1, 11 plus the DataSize of that tag. |
| Tag2 … TagN | FLVTAG | The next tags, each followed by its PreviousTagSize. |
| PreviousTagSizeN | UI32 | The size of the last tag, header included. |
The back-pointers let a reader walk the file from the end, and check that a tag ends where its header says.
Tag header
Every tag starts with an 11-byte header. The fields after it depend on the tag type and on the Filter bit.
| Offset | Type | Field | Meaning |
|---|---|---|---|
| 0 | UB[2] | Reserved | Bits 7 and 6. Reserved for Flash Media Server; should be 0. |
| 0 | UB[1] | Filter | Bit 5. 0: no pre-processing is needed. 1: the packet must be pre-processed (decrypted) before it can be played. 0 in unencrypted files and 1 for encrypted tags; see Encryption. |
| 0 | UB[5] | TagType | Bits 4 to 0: what the tag holds (below). |
| 1 | UI24 | DataSize | The number of bytes after StreamID to the end of the tag: the tag’s length less 11. |
| 4 | UI24 | Timestamp | The time in milliseconds at which the tag’s data applies, relative to the first tag, whose time is 0. These are the lower 24 bits. |
| 7 | UI8 | TimestampExtended | The upper 8 bits of the time, making it an SI32 number of milliseconds. |
| 8 | UI24 | StreamID | Always 0. |
| 11 | AudioTagHeader | AudioTagHeader | When TagType is 8: see audio tags. |
| VideoTagHeader | VideoTagHeader | When TagType is 9: see video tags. | |
| EncryptionTagHeader | EncryptionHeader | When Filter is 1: the filter to apply (Encryption). | |
| FilterParams | FilterParams | When Filter is 1: the filter’s parameters. | |
| AUDIODATA, VIDEODATA or SCRIPTDATA | Data | The tag’s contents, by type. |
| TagType | Contents |
|---|---|
| 8 | Audio |
| 9 | Video |
| 18 | Script data: a method name and its arguments in AMF, such as onMetaData |
| other values | Not defined for FLV files. RTMP numbers its messages the same way (8 audio, 9 video, 18 AMF0 data, and also 15 AMF3 data, 20 AMF0 command and so on), and a recorder that writes RTMP messages to a file can leave such types in it; FLV readers skip them. |
When a file is played, only the tags’ times decide when each tag’s data is used: any timing inside the audio or video data is ignored.
Worked example
The first 13 bytes of a file with audio and video:
| Bytes | Meaning |
|---|---|
46 4C 56 | FLV |
01 | Version 1 |
05 | Audio (04) and video (01) |
00 00 00 09 | Header size 9 |
00 00 00 00 | PreviousTagSize0 |
Then the header of a script data tag of 292 bytes at time 0:
| Bytes | Meaning |
|---|---|
12 | Reserved 0, Filter 0, TagType 18 (script data) |
00 01 24 | DataSize 292 |
00 00 00 | Timestamp 0 |
00 | TimestampExtended 0 |
00 00 00 | StreamID 0 |
| 292 bytes | The data: onMetaData and its properties |
00 00 01 2F | PreviousTagSize 303 (11 + 292) |