Https://github.com/332065255/flv2fmp4
code Base
Soft coded FLV to MP4 container (i)
Soft coded FLV to MP4 container (ii) FLV tag Dismantling
Soft coded FLV to MP4 container (iii) FLV METADATA tag parsing
Soft coded FLV to MP4 container (iv) Fmp4 Overview and basic explanation
Soft coded FLV to MP4 container (v) Fmp4 Ftyp box and Moov>mvhd Box detailed
Soft coded FLV to MP4 container (vi) Fmp4 MOOV>TRAK>TKHD box and MOOV>TRAK>MDIA>MDHD box explain
Soft coded FLV to MP4 container (vii) Fmp4 MDIA>HDLR box and mdia>minf> smhd and Dinf box explain
Soft coded FLV to MP4 container (eight) Fmp4 mdia>stbl>stsd box explain
Soft coded FLV to MP4 container (ix) fmp4 stts stsc stsz Stco Box explain
Soft coded FLV to MP4 container (10) Fmp4 Mvex Box explain
Soft coded FLV to MP4 container (11) Fmp4 moof Box detailed
Soft coded FLV to MP4 container (12) Fmp4 Mdat Box detailed
Soft coded FLV to MP4 container (13) Fmp4 The FLV data necessary to generate Ftyp and Moov
-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
First corresponds to the position of the title box in the Fmp4 container Ftyp box
Box type: ' Ftyp '//box type
Container:file//Parent box
Mandatory:yes//whether mandatory existence
quantity:exactly One//only has a
Aligned (8) class Filetypebox
extends Box (' Ftyp ') {
unsigned int () major_brand; 4-byte brand name
unsigned int (minor_version) //4 byte version number
unsigned int compatible_brands[]; Content 4-byte compatible brand array
}
The previous code is an official PDF of the Ftyp box definition
Ftyp = new Uint8array ([
0x69, 0x73, 0x6f, 0x6d,
//Major_brand:isom isom MP4 Base Media v1
[IS0 14496-12:2003] ISO YES video/mp4
0x0, 0x0, 0x0, 0x1,//minor_version:0x01 0x69, 0x73, 0x6f,
0x6d, Isom
0x61, 0x76, 0x63, 0x31//AVC1
]);
Fmp4 's Ftyp is written above, 4 byte length + 4 byte ' Ftyp ' +ftyp object, is the complete Ftyp box moov Box
Box Type: ' Moov '//box name
Container:file//box Parent, this is the top level
Mandatory:yes//whether forced to exist
quantity:exactly One//only has a
Aligned (8) class MovieBox extends Box (' Moov ') {}
visible from above moov box is a container box, so it itself has no description, 4 bytes moov box Length (the length here refers to the total length of all box inside the moov, rather than the ' Moov ' 4-byte length) + 4-byte ' Moov ' Is the full description of Moov.
moov>mvhd Box
Box Type: ' mvhd '//box name
Container:movie Box (' Moov ')//box parent, here is Moov
Mandatory:yes//whether forced to exist
quantity:exactly One//only has a
aligned (8) class Movieheaderbox extends (' Fullbox ', version, 0) {if (mvhd) {version==1
Ed Int (creation_time);
unsigned int () Modification_time;
unsigned int () timescale;
unsigned int () duration; else {//version==0//usually version 0 unsigned int () Creation_time; Media creation time relative to UTC time 1904-01-01 0 o'clock seconds unsigned int () Modification_time; Modified unsigned int () timescale;//scale value, can be understood as length of 1 seconds unsigned int (duration;//) template int () rate = 0x000 10000; typically 1.0//playback rate, high 16 and low 16 respectively indicate the integer and fractional parts before and after the decimal point template int () volume = 0x0100; Typically, full volume//voice, high 8-bit and low 8-bit respectively denote the integer and fractional parts before and after the decimal point (the const bit) reserved = 0; 2-byte reserved bit const unsigned int (a) [2] reserved = 0;
4+4 byte reserved bit template int (9) matrix ={0x00010000,0,0,0,0x00010000,0,0,0,0x40000000}; Unity Matrix Video transformation matrix bit (6) pre_defined = 0; 6*4 reserved bit unsigned int (a) next_track_id; 4-byte next track ID}
The code above is defined by the MP4 official document.
First we see that Mvhd box inherits the Full box (' Mvhd ', version,0).
Full box is box's enhanced box, followed by 4 bytes, the first byte is version, and the following 3 bytes are reserved bits
var mvhd= new Uint8array ([0x00, 0x00, 0x00, 0x00,//version (0) + Flags 1-bit box version + 3-bit Flags box version, 0 or 1, generally 0 。 (Each of the following bytes is version=0) 0x00, 0x00, 0x00, 0x00,//Creation_time creation time (relative to UTC time 1904-01-01 0 o'clock seconds) 0x0 0, 0x00, 0x00, 0x00,//Modification_time modification time (timescale >>>) & 0xFF,//timescale:4 bytes The scale value of the file media in 1 seconds can be understood to be 1 seconds in length (timescale >>>) & 0xFF, (Timescale >>> 8 ) & 0xFF, (timescale) & 0xFF, (Duration >>>) & 0xFF,//Duration:4 bytes The length of the track, duration and time scale values can be used to calculate the track time, such as audio track times scale = 8000, duration = 560128, the length is 70.016,video Time scale = track, duration = 42000, Length (Duration >>>) & 0xFF, (Duration &G T;>> 8 & 0xFF, (duration) & 0xFF, 0x00, 0x01, 0x00, 0x00,//Preferred rate:1.0 Recommended playback rate, high 16-bit and low 16-bit, respectively, decimal point integer and fractional parts,In the [16.16] format, the value is 1.0 (0x00010000) for normal forward playback 0x01, 0x00, 0x00, 0x00,//Preferredvolume (1.0, 2bytes) + reserved (2byt ES) similar to rate, [8.8] format, 1.0 (0X0100) represents maximum volume 0x00, 0x00, 0x00, 0x00,//Reserved:4 + 4 bytes reserved bit 0 X00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,//----begin composition Matrix----0x00, 0x00, 0x0 0, 0x00, 0x00, 0x00, 0x00, 0x00,//Video transformation matrix linear algebra 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0
X00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,//----End composition matrix----0x00, 0x00, 0x00, 0x00,//----begin Pre_de
Fined 6 * 4 bytes----0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//pre-defined reserved bit 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,//----End pre_defined 6
* 4 bytes---- 0xFF, 0xFF, 0xFF, 0xFF//next_track_id the next track use ID number]
Mvhd is a full box, so it has no child box, and it ultimately behaves as a 4-byte length + 4-byte ' Mvhd ' +mvhd object, which is a complete mvhd box