欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

FLV封装结构

程序员文章站 2022-03-21 08:01:44
...

FLV

结构

FLV封装结构

FLV结构

FLV封装结构

video tag - avc video packet
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201123110215421.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Vhc3loYW8wMDc=,size_16,color_FFFFFF,t_70#pic_center)
audio tag - aac specific data

FLV header

    /*
        An FLV file shall begin with the FLV header:
        FLV header
        ----------------------------------------------------------------------
        Field               Type            Comment
        ----------------------------------------------------------------------
        Signature           UI8         Signature byte always 'F' (0x46)
        Signature           UI8         Signature byte always 'L' (0x4C)
        Signature           UI8         Signature byte always 'V' (0x56)
        Version             UI8         File version (for example, 0x01 for FLV version 1)
        TypeFlagsReserved   UB [5]      Shall be 0
        TypeFlagsAudio      UB [1]      1 = Audio tags are present
        TypeFlagsReserved   UB [1]      Shall be 0
        TypeFlagsVideo      UB [1]      1 = Video tags are present
        DataOffset          UI32        The length of this header in bytes
        The DataOffset field usually has a value of 9 for FLV version 1. This field is present to accommodate larger headers 
        in future versions.
    */

FLV Tag

    /*
    FLV Tag
        The FLV tag contains metadata for audio, video, or scripts, optional encryption metadata, and the payload.
        ----------------------------------------------------------
        Field               Type                        Comment
        -----------------------------------------------------------
        Reserved            UB [2]                      Reserved for FMS, should be 0
        Filter              UB [1]                      Indicates if packets are filtered.
                                                        0 = No pre-processing required.
                                                        1 = Pre-processing (such as decryption) of the packet is 
                                                        required before it can be rendered.
                                                        Shall be 0 in unencrypted files, and 1 for encrypted tags. 
                                                        See Annex F. FLV Encryption for the use of filters.
        TagType             UB [5]                      Type of contents in this tag. The following types are 
                                                        defined: 
                                                            8 = audio 
                                                            9 = video 
                                                            18 = script data 
        DataSize            UI24                        Length of the message. Number of bytes after StreamID to 
                                                        end of tag (Equal to length of the tag – 11)
        Timestamp           UI24                        Time in milliseconds at which the data in this tag applies. 
                                                        This value is relative to the first tag in the FLV file, which 
                                                        always has a timestamp of 0. 
        TimestampExtended   UI8                         Extension of the Timestamp field to form a SI32 value. This 
                                                        field represents the upper 8 bits, while the previous 
                                                        Timestamp field represents the lower 24 bits of the time in 
                                                        milliseconds. 
        StreamID            UI24                        Always 0. 
        AudioTagHeader      IF TagType == 8             AudioTagHeader element as defined in Section E.4.2.1. 
                            AudioTagHeader

        VideoTagHeader      IF TagType == 9             VideoTagHeader element as defined in Section E.4.3.1.
                            VideoTagHeader
        EncryptionHeader    IF Filter == 1              Encryption header shall be included for each protected 
                            EncryptionTagHeader         sample, as defined in Section F.3.1.
        FilterParams        IF Filter == 1              FilterParams shall be included for each protected sample, as 
                            FilterParams                defined in Section F.3.2.
        Data                IF TagType == 8 AUDIO DATA  Data specific for each media type
                            IF TagType == 9 VIDEOD ATA
                            IF TagType == 18SCRIPTDATA
   */

Audio Tag Header

    /*
        Audio tags are similar to the DefineSound tag in the SWF file format. 
        For formats also supported in SWF, the payload data is identical in FLV and SWF.

        ----------------------------------------------------------------------------------------------------
        Field                   Type                Comment
        ----------------------------------------------------------------------------------------------------
        SoundFormat             UB [4]              Format of SoundData. The following values are defined:
                                                    0 = Linear PCM, platform endian     
        (See notes following                        1 = ADPCM
        table, for special                          2 = MP3
        encodings)                                  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
                                                    11 = Speex
                                                    14 = MP3 8 kHz
                                                    15 = Device-specific sound
                                                    Formats 7, 8, 14, and 15 are reserved.
                                                    AAC is supported in Flash Player 9,0,115,0 and higher.
                                                    Speex is supported in Flash Player 10 and higher.

        SoundRate               UB [2]              Sampling rate. The following values are defined:
                                                    0 = 5.5 kHz
                                                    1 = 11 kHz
                                                    2 = 22 kHz
                                                    3 = 44 kHz

        SoundSize               UB [1]              Size of each audio sample. This parameter only pertains to 
                                                    uncompressed formats. Compressed formats always decode 
                                                    to 16 bits internally. 
                                                    0 = 8-bit samples
                                                    1 = 16-bit samples

        SoundType               UB [1]              Mono or stereo sound
                                                    0 = Mono sound
                                                    1 = Stereo sound

        AACPacketType       IF SoundFormat == 10    The following values are defined:
                                UI8                 0 = AAC sequence header
                                                    1 = AAC raw
    */

Video Tag Header

    /*
        The VideoTagHeader contains video-specific metadata.
        ---------------------------------------------------------------------
        Field               Type                        Comment
        ---------------------------------------------------------------------
        Frame Type          UB [4]                  Type of video frame. The following values are defined:
                                                    1 = key frame (for AVC, a seekable frame)
                                                    2 = inter frame (for AVC, a non-seekable frame)
                                                    3 = disposable inter frame (H.263 only)
                                                    4 = generated key frame (reserved for server use only)
                                                    5 = video info/command frame

        CodecID             UB [4]                  Codec Identifier. The following values are defined:
                                                    2 = Sorenson H.263
                                                    3 = Screen video
                                                    4 = On2 VP6
                                                    5 = On2 VP6 with alpha channel
                                                    6 = Screen video version 2
                                                    7 = AVC
        
        AVCPacketType       IF CodecID == 7         The following values are defined:
                            UI8                     0 = AVC sequence header
                                                    1 = AVC NALU
                                                    2 = AVC end of sequence (lower level NALU sequence ender is 
                                                    not required or supported)

        CompositionTime     IF CodecID == 7         IF AVCPacketType == 1
                            SI24                        Composition time offset
                                                    ELSE
                                                        0

    */

FLV File Body

    /*
        The FLV File Body
        After the FLV header, the remainder of an FLV file shall consist of alternating back-pointers and tags. 
        They interleave as shown in the following table:
        ---------------------------------------------------------------
        Field               Type        Comment
        ---------------------------------------------------------------
        PreviousTagSize0    UI32        Always 0
        Tag1                FLVTAG      First tag
        PreviousTagSize1    UI32        Size of previous tag, including its header, in bytes. For FLV version 
                                        1, this value is 11 plus the DataSize of the previous tag.
        Tag2                FLVTAG      Second tag
        ...
        PreviousTagSizeN-1  UI32        Size of second-to-last tag, including its header, in bytes.
        TagN                FLVTAG      Last tag
        PreviousTagSizeN    UI32        Size of last tag, including its header, in bytes.
        ---------------------------------------------------------------
    */
相关标签: avformat flv adts