詳細なFLVファイル形式


特定の形式については、flv仕様を参照してください。


上の図のタグのフィールドを主に紹介します各タグは、タグヘッダーとタグデータの2つの部分で構成されています。

1.タグヘッダー

名前 長さ はじめに
タグタイプ 1バイト 8:オーディオ
9:ビデオ
18:メタ
その他:予約済み
データ域の長さ 3バイト データ域の長さ
タイムスタンプ 3バイト 整数はミリ秒単位です。スクリプトタグの場合は常に0
タイムスタンプ拡張 1バイト タイムスタンプを上位8ビットを表す4バイトに拡張します。めったに使用されない
StreamsID 3バイト 常に0
データ領域(データ) データ域の長さによって決定 データエンティティ

2日目のデータ

タグのデータ領域は、タグタイプに応じて、オーディオデータ、ビデオデータ、メタデータの3つのタイプに分類できます。

2.1音声データ

最初のバイトはオーディオ情報で、形式は次のとおりです。
名前 長さ はじめに
オーディオフォーマット 4ビット 0 =線形PCM、プラットフォームエンディアン
1 = ADPCM
2 = MP3
3 =線形PCM、リトルエンディアン
4 = Nellymoser 16 kHzモノ
5 = Nellymoser 8 kHzモノ
6 = Nellymoser
7 = G.711 A-law対数PCM
8 = G .711 mu-law対数PCM
9 =予約済み
10 = AAC
11 = Speex
14 = MP3 8-Khz
15 =デバイス固有のサウンド
サンプルレート 2ビット 0 = 5.5-kHz
1 = 11-kHz
2 = 22-kHz
3 = 44-kHz
はAACの場合は常に3
サンプルの長さ 1ビット 0 = snd8Bit
1 = snd16Bit
圧縮オーディオは16ビット
音声タイプ 1ビット 0 = sndMono
1 = sndStereo
はAACの場合は常に1です
音声データ         8ビット[n]  
     

AAC形式の場合、音声データの形式は次のとおりです。


2.2ビデオデータ

最初のバイトはビデオ情報で、形式は次のとおりです。
名前 長さ はじめに
フレームタイプ 4ビット 1:キーフレーム(AVC、シーク可能なフレーム)
2:インターフレーム(AVC、シーク不可能なフレーム)
3:使い捨てインターフレーム(H.263のみ)
4:生成されたキーフレーム(サーバーでの使用のみに予約)
5:ビデオ情報/コマンドフレーム
コードID 4ビット 1:JPEG(現在未使用)
2:Sorenson H.263
3:画面ビデオ
4:On2 VP6
5:On2 VP6(アルファチャネル付き)
6:画面ビデオバージョン2
7:AVC

フレームタイプが5の場合、タグデータのコンテンツはビデオデータではなく、データは8ビットデータです。意味は次のとおりです。
  • 0、クライアント側シークビデオフレームシーケンスの開始 
  • 1、クライアント側シークビデオフレームシーケンスの終わり 
AVC形式の場合、タグデータ後のデータ形式は以下のとおりです。


2.3スクリプトデータ

通常、flvの最初のタグであるスクリプトタグは1つだけあり、duration、audiodatarate、creator、widthなどのflv情報を格納するために使用されます。

最初にスクリプトのデータ型を紹介します。すべてのデータは、データ型+(データ長)+データの形式で表示されます。データ型は1バイトを占有します。データ長は、データ型が存在するかどうかに依存し、その後にデータが続きます。

形式は次のとおりです。



タイプがStringの場合、次の2バイトはストリングの長さ(Long Stringは4バイト)、次にストリング・データです。それがNumberタイプの場合、次の8バイトはDoubleタイプのデータ、ブール・タイプ、次の1バイトはBoolタイプです。 。

これを理解したら、flvのスクリプトを見てみましょう。通常、先頭は0x02で、これはString型を意味し、次の2バイトは文字列の長さ、通常は0x000a( "onMetaData"の長さ)、次に文字列 "onMetaData。flv形式のファイルにはonMetaDataタグがあり、ActionScriptの実行時に使用されます。その後に0x08が続きます。これは、マップに似たECMAアレイタイプを示し、キーの後に値が続きます。キーはすべてString型であるため、最初の0x02は省略され、その後に文字列の長さ、文字列、値の型、つまり上記で紹介したものが続きます。

onMetaDataタグにはストリーム情報が含まれています。一般的な情報は次のとおりです。

  • duration: a DOUBLE indicating the total duration of the file in seconds
  • width: a DOUBLE indicating the width of the video in pixels
  • height: a DOUBLE indicating the height of the video in pixels
  • videodatarate: a DOUBLE indicating the video bit rate in kilobits per second 
  • framerate: a DOUBLE indicating the number of frames per second
  • videocodecid: a DOUBLE indicating the video codec ID used in the file (see “Video tags” on page 8 for available CodecID values)
  • audiosamplerate: a DOUBLE indicating the frequency at which the audio stream is replayed
  • audiosamplesize: a DOUBLE indicating the resolution of a single audio sample
  • stereo: a BOOL indicating whether the data is stereo
  • audiocodecid: a DOUBLE indicating the audio codec ID used in the file (see “Audio tags” on page 6 for available SoundFormat values)
  • filesize: a DOUBLE indicating the total size of the file in bytes 

2.4 keyframes索引信息

官方的文档中并没有对 keyframes index 做描述,但是,flv 的这种结构每个 tag 又不像 TS 有同步头,如果没有 keyframes index 的话,需要按顺序读取每一个tag, seek 及快进快退的效果会非常差。后来在做 flv 文件合成的时候,发现网上有的 flv 文件将 keyframes 信息隐藏在 Script Tag 中。

keyframes 几乎是一个非官方的标准, 也就是民间标准。两个常用的操作 metadata 的工具是 flvtool2 和 FLVMDI,都是把 keyframes 作为一个默认的元信息项目。在 FLVMDI 的主页上有描述:

keyframes: (Object) This object is added only if you specify the /k switch. 'keyframes' is known to FLVMDI and if /k switch is not specified, 'keyframes' object will be deleted. 'keyframes' object has 2 arrays: 'filepositions' and 'times'. Both arrays have the same number of elements, which is equal to the number of key frames in the FLV. Values in times array are in 'seconds'. Each correspond to the timestamp of the n'th key frame. Values in filepositions array are in 'bytes'. Each correspond to the fileposition of the nth key frame video tag (which starts with byte tag type 9).

也就是说 keyframes 中包含着 2 个内容 “filepositions” 和 “times”分别指的是关键帧的文件位置和关键帧的 PTS。通过 keyframes 可以建立起自己的 Index,然后在 seek 和快进快退的操作中,快速有效地跳转到你想要找的关键帧位置进行处理。

3. FLV分析工具


发布了60 篇原创文章 · 获赞 44 · 访问量 34万+

おすすめ

転載: blog.csdn.net/beyond702/article/details/78929334