軟編碼Flv 到Mp4 容器(六) fmp4 moov>trak>tkhd box 和 moov>trak>mdia>mdhd box講解

https://github.com/332065255/flv2fmp4

代碼庫


軟編碼Flv 到Mp4 容器(一)
軟編碼Flv 到Mp4 容器(二) flv tag拆解
軟編碼Flv 到Mp4 容器(三) flv metadata tag解析
軟編碼Flv 到Mp4 容器(四) fmp4 總覽和基礎講解
軟編碼Flv 到Mp4 容器(五) fmp4 ftyp box 和moov>mvhd box詳解
軟編碼Flv 到Mp4 容器(六) fmp4 moov>trak>tkhd box 和 moov>trak>mdia>mdhd box講解
軟編碼Flv 到Mp4 容器(七) fmp4 mdia>hdlr box 和 mdia>minf> smhd 和dinf box講解
軟編碼Flv 到Mp4 容器(八) fmp4 mdia>stbl>stsd box 講解
軟編碼Flv 到Mp4 容器(九) fmp4 stts stsc stsz stco box 講解
軟編碼Flv 到Mp4 容器(十) fmp4 mvex box 講解
軟編碼Flv 到Mp4 容器(十一) fmp4 moof box詳解
軟編碼Flv 到Mp4 容器(十二) fmp4 mdat box詳解
軟編碼Flv 到Mp4 容器(十三) fmp4 生成ftyp和moov所必要的 flv數據


 - ftyp
 - moov
     - mvhd
     - trak
         - tkhd
         - mdia
             - mdhd
             - hdlr
             - minf
                 - smhd
                 - dinf
                     - dref
                         - url
                 - stbl
                     -  stsd
                         - mp4a(avc1)
                             - esds(avcC)
                     - stts
                     - stsc
                     - stsz
                     - stco


     - mvex
        -trex
 - moof
    - mfhd
    - traf
        -tfhd
        -tfdt
        -sdtp
        -trun
 - mdat

首先對應標題的box在 fmp4容器中的位置


tkhd box

Box Type: ‘tkhd’
Container: Track Box (‘trak’)
Mandatory: Yes
Quantity: Exactly one

先看官方定義

aligned(8) class TrackHeaderBox
   extends FullBox(‘tkhd’, version, flags){
   if (version==1) {
      unsigned int(64)  creation_time;
      unsigned int(64)  modification_time;
      unsigned int(32)  track_ID;
      const unsigned int(32)  reserved = 0;
      unsigned int(64)  duration;
   } else { // version==0     
      unsigned int(32)  creation_time; //創建時間(相對於UTC時間1904-01-01零點的秒數)
      unsigned int(32)  modification_time;//修改時間
      unsigned int(32)  track_ID;//id號,不能重複且不能爲0
      const unsigned int(32)  reserved = 0;//4字節保留位
      unsigned int(32)  duration;//時長
}
const unsigned int(32)[2] reserved = 0;// reserved: 2 * 4 bytes    保留位
template int(16) layer = 0;
template int(16) alternate_group = 0;// layer(2bytes) + alternate_group(2bytes)  視頻層,默認爲0,值小的在上層.track分組信息,默認爲0表示該track未與其他track有羣組關係
template int(16) volume = {if track_is_audio 0x0100 else 0}; //volume(2bytes)
const unsigned int(16) reserved = 0;//reserved(2bytes) 
template int(32)[9] matrix=//視頻變換矩陣
{ 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 };// unity matrix

unsigned int(32) width;//寬
unsigned int(32) height;//高
}

由上可見,這個box也是一個full box,它不包含任何子box

var tkhd = new Uint8Array([
            0x00, 0x00, 0x00, 0x07, // version(0) + flags 1位版本 box版本,0或1,一般爲0。(以下字節數均按version=0)按位或操作結果值,預定義如下:
            //0x000001 track_enabled,否則該track不被播放;
            //0x000002 track_in_movie,表示該track在播放中被引用;
            //0x000004 track_in_preview,表示該track在預覽時被引用。
            //一般該值爲7,1+2+4 如果一個媒體所有track均未設置track_in_movie和track_in_preview,將被理解爲所有track均設置了這兩項;對於hint track,該值爲0
            //hint track  這個特殊的track並不包含媒體數據,而是包含了一些將其他數據track打包成流媒體的指示信息。
            0x00, 0x00, 0x00, 0x00, // creation_time    創建時間(相對於UTC時間1904-01-01零點的秒數)
            0x00, 0x00, 0x00, 0x00, // modification_time    修改時間
            (trackId >>> 24) & 0xFF, // track_ID: 4 bytes   id號,不能重複且不能爲0
            (trackId >>> 16) & 0xFF,
            (trackId >>> 8) & 0xFF,
            (trackId) & 0xFF,
            0x00, 0x00, 0x00, 0x00, // reserved: 4 bytes    保留位
            (duration >>> 24) & 0xFF, // duration: 4 bytes      track的時間長度
            (duration >>> 16) & 0xFF,
            (duration >>> 8) & 0xFF,
            (duration) & 0xFF,
            0x00, 0x00, 0x00, 0x00, // reserved: 2 * 4 bytes    保留位
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, // layer(2bytes) + alternate_group(2bytes)  視頻層,默認爲0,值小的在上層.track分組信息,默認爲0表示該track未與其他track有羣組關係
            0x00, 0x00, 0x00, 0x00, // volume(2bytes) + reserved(2bytes)    [8.8] 格式,如果爲音頻track,1.0(0x0100)表示最大音量;否則爲0   +保留位
            0x00, 0x01, 0x00, 0x00, // ----begin composition matrix----
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x01, 0x00, 0x00, //視頻變換矩陣
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00,
            0x40, 0x00, 0x00, 0x00, // ----end composition matrix----
            (width >>> 8) & 0xFF, // //寬度
            (width) & 0xFF,
            0x00, 0x00,
            (height >>> 8) & 0xFF, //高度
            (height) & 0xFF,
            0x00, 0x00
        ]));

tkhd是一個full box,所以它沒有子box了,它最終的表現是 4字節長度+4字節’tkhd’+tkhd對象,就是一個完整的tkhd box


mdia box

Box Type: ‘mdia’
Container: Track Box (‘trak’)
Mandatory: Yes
Quantity: Exactly one

先看官方定義

aligned(8) class MediaBox extends Box(‘mdia’) { }

很明顯..這是一個container box


mdhd box

Box Type: ‘mdhd’
Container: Media Box (‘mdia’)
Mandatory: Yes
Quantity: Exactly one

官方定義

//繼承自fullbox 版本是0
aligned(8) class MediaHeaderBox extends FullBox(‘mdhd’, version, 0) { if (version==1) {
      unsigned int(64)  creation_time;
      unsigned int(64)  modification_time;
      unsigned int(32)  timescale;
      unsigned int(64)  duration;
   } else { // version==0
      unsigned int(32)  creation_time;//創建時間
      unsigned int(32)  modification_time;//修改時間
      unsigned int(32)  timescale;//文件媒體在1秒時間內的刻度值,可以理解爲1秒長度
      unsigned int(32)  duration;//track的時間長度
}
bit(1) pad=0;
unsigned int(5)[3] language; // // language: und (undetermined) 媒體語言碼。最高位爲0,後面15位爲3個字符(見ISO 639-2/T標準中定義)
pre_defined = 0;
}

這個mdhd box又是一個full box,所以它不不含任何子元素


var mdhd = new Uint8Array([
            0x00, 0x00, 0x00, 0x00, // version(0) + flags // version(0) + flags     box版本,0或1,一般爲0。
            0x00, 0x00, 0x00, 0x00, // creation_time    創建時間
            0x00, 0x00, 0x00, 0x00, // modification_time修改時間
            (timescale >>> 24) & 0xFF, // timescale: 4 bytes    文件媒體在1秒時間內的刻度值,可以理解爲1秒長度
            (timescale >>> 16) & 0xFF,
            (timescale >>> 8) & 0xFF,
            (timescale) & 0xFF,
            (duration >>> 24) & 0xFF, // duration: 4 bytes  track的時間長度
            (duration >>> 16) & 0xFF,
            (duration >>> 8) & 0xFF,
            (duration) & 0xFF,
            0x55, 0xC4, // language: und (undetermined) 媒體語言碼。最高位爲0,後面15位爲3個字符(見ISO 639-2/T標準中定義)
            0x00, 0x00 // pre_defined = 0
        ])

mdhd是一個full box,所以它沒有子box了,它最終的表現是 4字節長度+4字節’mdhd’+mdhd對象,就是一個完整的mdhd box

本章完

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章