使用 ffmpeg命令生成flv文件
ffmpeg -ss 0 -t 30 -f lavfi -i color=c=0x0000ff:s=1280x720:r=25 -ss 0 -t 30 -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -map 0:v -map 1:a -c:v libx264 -r:v 25 -c:a:0 aac 0ff0ff_30s_1280x720_aac2.flv
0ff0ff_30s_1280x720_aac2
上述文件使用flvmeta dump 信息如下:
Magic: FLV
Version: 1
Has audio: yes
Has video: yes
Offset: 9
--- Tag #1 at 0xD (13) ---
Tag type: scriptData
Body length: 293
Timestamp: 0
* Metadata event name: onMetaData
* Metadata contents: {
'duration' => 30.08
'width' => 1280
'height' => 720
'videodatarate' => 0
'framerate' => 25
'videocodecid' => 7
'audiodatarate' => 125
'audiosamplerate' => 44100
'audiosamplesize' => 16
'stereo' => true
'audiocodecid' => 10
'encoder' => 'Lavf58.43.100'
'filesize' => 76289
}
Previous tag size: 304
--- Tag #2 at 0x141 (321) ---
Tag type: video
Body length: 52
Timestamp: 0
* Video codec: AVC
* Video frame type: seekable frame
* AVC packet type: AVC sequence header
Previous tag size: 63
上述文件转换为16进制如下:
00000000: 464c 5601 0500 0000 0900 0000 0012 0001 FLV………….
00000010: 2500 0000 0000 0000 0200 0a6f 6e4d 6574 %……….onMet
00000020: 6144 6174 6108 0000 000d 0008 6475 7261 aData…….dura
00000030: 7469 6f6e 0040 3e14 7ae1 47ae 1400 0577 tion.@>.z.G….w
00000040: 6964 7468 0040 9400 0000 0000 0000 0668 idth.@………h
00000050: 6569 6768 7400 4086 8000 0000 0000 000d eight.@………
00000060: 7669 6465 6f64 6174 6172 6174 6500 0000 videodatarate…
00000070: 0000 0000 0000 0009 6672 616d 6572 6174 ……..framerat
00000080: 6500 4039 0000 0000 0000 000c 7669 6465 e.@9……..vide
00000090: 6f63 6f64 6563 6964 0040 1c00 0000 0000 ocodecid.@……
000000a0: 0000 0d61 7564 696f 6461 7461 7261 7465 …audiodatarate
000000b0: 0040 5f40 0000 0000 0000 0f61 7564 696f .@_@…….audio
000000c0: 7361 6d70 6c65 7261 7465 0040 e588 8000 samplerate.@….
000000d0: 0000 0000 0f61 7564 696f 7361 6d70 6c65 …..audiosample
000000e0: 7369 7a65 0040 3000 0000 0000 0000 0673 size.@0……..s
000000f0: 7465 7265 6f01 0100 0c61 7564 696f 636f tereo….audioco
00000100: 6465 6369 6400 4024 0000 0000 0000 0007 decid.@$……..
00000110: 656e 636f 6465 7202 000d 4c61 7666 3538 encoder…Lavf58
00000120: 2e34 332e 3130 3000 0866 696c 6573 697a .43.100..filesiz
00000130: 6500 40f2 a010 0000 0000 0000 0900 0001 e.@………….
00000140: 3009 0000 3400 0000 0000 0000 1700 0000 0…4………..
00000150: 0001 6408 1fff e100 1a67 6408 1fac d940 ..d……gd….@
00000160: 5005 bb01 1000 0003 0010 0000 0303 20f1 P…………. .
00000170: 8319 6001 0006 68eb e3cb 22c0 fdf8 f800 ..`…h…”…..
下面根据协议分析几个tag:
video_file_format_spec_v10_1
E.2 The 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.
00000000: 464c 5601 0500 0000 0900 0000 0012 0001 FLV………….
464c 56 FLV 指明文件类型
01 文件版本
00000101 0x05 说明有Video 也有Audio数据。
00 0000 09 数据偏移,按照协议定义为9
00000000: 464c 5601 0500 0000 0900 0000 0012 0001 FLV………….
00000010: 2500 0000 0000 0000 0200 0a6f 6e4d 6574 %……….onMet
00000020: 6144 6174 6108 0000 000d 0008 6475 7261 aData…….dura
00000030: 7469 6f6e 0040 3e14 7ae1 47ae 1400 0577 tion.@>.z.G….w
E.3 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:
FLV File Body
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
00 0000 00 UI32 保留位
参考E.4.1 FLV Tag
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
0x12 第一tag 类型为 script data
00000000: 464c 5601 0500 0000 0900 0000 0012 0001 FLV………….
00000010: 2500 0000 0000 0000 0200 0a6f 6e4d 6574 %……….onMet
00000020: 6144 6174 6108 0000 000d 0008 6475 7261 aData…….dura
00000030: 7469 6f6e 0040 3e14 7ae1 47ae 1400 0577 tion.@>.z.G….w
DataSize UI24 Length of the message. Number of bytes after StreamID to
end of tag (Equal to length of the tag – 11)
000125 = 293 第一个tag的数据大小为293.
00000000: 464c 5601 0500 0000 0900 0000 0012 0001 FLV………….
00000010: 2500 0000 0000 0000 0200 0a6f 6e4d 6574 %……….onMet
00000020: 6144 6174 6108 0000 000d 0008 6475 7261 aData…….dura
00000030: 7469 6f6e 0040 3e14 7ae1 47ae 1400 0577 tion.@>.z.G….w
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.
00 0000 00 TimeStamp 为0
00000000: 464c 5601 0500 0000 0900 0000 0012 0001 FLV………….
00000010: 2500 0000 0000 0000 0200 0a6f 6e4d 6574 %……….onMet
00000020: 6144 6174 6108 0000 000d 0008 6475 7261 aData…….dura
00000030: 7469 6f6e 0040 3e14 7ae1 47ae 1400 0577 tion.@>.z.G….w
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.
00 时间戳 高8bit
StreamID UI24 Always 0.
00 0000
Type UI8 Type of the ScriptDataValue.
The following types are defined:
0 = Number
1 = Boolean
2 = String
3 = Object
4 = MovieClip (reserved, not supported)
5 = Null
6 = Undefined
7 = Reference
8 = ECMA array
9 = Object end marker
10 = Strict array
11 = Date
12 = Long string
00000000: 464c 5601 0500 0000 0900 0000 0012 0001 FLV………….
00000010: 2500 0000 0000 0000 0200 0a6f 6e4d 6574 %……….onMet
00000020: 6144 6174 6108 0000 000d 0008 6475 7261 aData…….dura
00000030: 7469 6f6e 0040 3e14 7ae1 47ae 1400 0577 tion.@>.z.G….w
02 String 类型。
00 0a 大小
string Value 6f 6e4d 6574 6144 6174 61
00000000: 464c 5601 0500 0000 0900 0000 0012 0001 FLV………….
00000010: 2500 0000 0000 0000 0200 0a6f 6e4d 6574 %……….onMet
00000020: 6144 6174 6108 0000 000d 0008 6475 7261 aData…….dura
00000030: 7469 6f6e 0040 3e14 7ae1 47ae 1400 0577 tion.@>.z.G….w
08
IF Type == 8
SCRIPTDATAECMAARRAY
SCRIPTDATAECMAARRAY
Field Type Comment
ECMAArrayLength UI32 Approximate number of items in ECMA array 0000 000d 13个item 。
Variables SCRIPTDATAOBJECTPROPERTY [ ] List of variable names and values
List Terminator SCRIPTDATAOBJECTEND List terminator
PropertyName SCRIPTDATASTRING Name of the object property or variable
PropertyData SCRIPTDATAVALUE Value and type of the object property or variable
0008 string size
6475 7261 7469 6f6e duration string value @PropertyName
00 (type number) 40 3e14 7ae1 47ae 14 64bit int2String
循环分析剩余数据对,获取如下 信息:
‘duration’ => 30.08
‘width’ => 1280
‘height’ => 720
‘videodatarate’ => 0
‘framerate’ => 25
‘videocodecid’ => 7
‘audiodatarate’ => 125
‘audiosamplerate’ => 44100
‘audiosamplesize’ => 16
‘stereo’ => true
‘audiocodecid’ => 10
‘encoder’ => ‘Lavf58.43.100’
‘filesize’ => 76289
分析第二个tag
00000130: 6500 40f2 a010 0000 0000 0000 0900 0001 e.@………….
00000140: 3009 0000 3400 0000 0000 0000 1700 0000 0…4………..
00000150: 0001 6408 1fff e100 1a67 6408 1fac d940 ..d……gd….@
00000160: 5005 bb01 1000 0003 0010 0000 0303 20f1 P…………. .
00000170: 8319 6001 0006 68eb e3cb 22c0 fdf8 f800 ..`…h…”…..
00000180: 0000 003f 0800 0007 0000 0000 0000 00af …?…………
00000190: 0012 1056 e500 0000 0012 0900 0395 0000 …V…………
000001a0: 0000 0000 0017 0100 0050 0000 02af 0605 ………P……
000001b0: ffff abdc 45e9 bde6 d948 b796 2cd8 20d9 ….E….H..,. .
000001c0: 23ee ef78 3236 3420 2d20 636f 7265 2031 #..x264 – core 1
000001d0: 3537 2072 3239 3639 2064 3430 3939 6464 57 r2969 d4099dd
000001e0: 202d 2048 2e32 3634 2f4d 5045 472d 3420 – H.264/MPEG-4
000001f0: 4156 4320 636f 6465 6320 2d20 436f 7079 AVC codec – Copy
00000200: 6c65 6674 2032 3030 332d 3230 3139 202d left 2003-2019 –
09 类型 video TagType
00 00 34 大小52
timestamp 00 0000 24bit
00 timestamp 高8bit
stream id 00 00 00
StreamID UI24 Always 0.
VideoTagHeader IF TagType == 9
17
VIDEODATA
Frame Type UB [4] 1 = key frame (for AVC, a seekable frame)
CodecID UB [4] 7 = AVC
00 0 = AVC sequence header
00 00 00 IF AVCPacketType == 1
Composition time offset
ELSE
0
PreviousTagSize 0000 003f
aa