2023-08-24 13:28:30 +05:00
|
|
|
// add a getString to make reading box headers easier
|
2023-08-23 20:58:37 +05:00
|
|
|
DataView.prototype.getString = function(offset, length) {
|
|
|
|
text = '';
|
|
|
|
let end = offset + length;
|
|
|
|
for (let i = offset; i < end; i++) {
|
|
|
|
text += String.fromCharCode(this.getUint8(i));
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:28:30 +05:00
|
|
|
// mp4 tree traverser, as is only good for getting avc1 + mp4a codec info.
|
|
|
|
// relevant tree parts:
|
|
|
|
// root
|
|
|
|
// |-moov
|
|
|
|
// |-trak
|
|
|
|
// |-mdia
|
|
|
|
// |-minf
|
|
|
|
// |-vmhd
|
|
|
|
// |-stbl
|
|
|
|
// |-stsd
|
|
|
|
// |-avc1
|
|
|
|
// |-avcC // has video codec profile
|
|
|
|
// |-trak
|
|
|
|
// |-mdia
|
|
|
|
// |-minf
|
|
|
|
// |-smhd
|
|
|
|
// |-stbl
|
|
|
|
// |-stsd
|
|
|
|
// |-mp4a
|
|
|
|
// |-esds // has audio codec profile
|
|
|
|
//
|
|
|
|
// headers are 4byte len + 4byte name string
|
|
|
|
// most details taken from https://web.archive.org/web/20180219054429/http://l.web.umkc.edu/lizhu/teaching/2016sp.video-communication/ref/mp4.pdf
|
|
|
|
// avc1 and mp4a specific header offsets from https://xhelmboyx.tripod.com/formats/mp4-layout.txt
|
2023-08-23 20:58:37 +05:00
|
|
|
class MP4Tree {
|
|
|
|
constructor(data) {
|
|
|
|
this.data = new DataView(data);
|
|
|
|
this.codecs = [];
|
2023-08-24 13:28:30 +05:00
|
|
|
this.idx = 0; // current byte index in data
|
2023-08-23 20:58:37 +05:00
|
|
|
this.parse_codecs();
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_codecs() {
|
2023-08-23 22:47:15 +05:00
|
|
|
this.parse_into('moov', 8);
|
2023-08-24 13:28:30 +05:00
|
|
|
// loop over all traks (can handle pure audio, pure video, both, multiple)
|
2023-08-23 22:47:15 +05:00
|
|
|
while (this.idx < this.data.byteLength) {
|
2023-08-24 13:28:30 +05:00
|
|
|
if (!this.parse_until('trak')) { // if no more tracks in file
|
2023-08-23 22:47:15 +05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
const curr_track_end = this.idx + this.read_next_head().len;
|
|
|
|
this.idx += 8
|
|
|
|
this.parse_into('mdia', 8);
|
|
|
|
this.parse_into('minf', 8);
|
|
|
|
const track_type_head = this.read_next_head().name;
|
|
|
|
if (track_type_head == 'smhd' || track_type_head == 'vmhd') {
|
|
|
|
this.parse_into('stbl', 8);
|
|
|
|
this.parse_into('stsd', 16);
|
|
|
|
const media_sample = this.read_next_head().name;
|
|
|
|
if (media_sample == 'avc1') {
|
|
|
|
this.parse_avc1();
|
|
|
|
} else if (media_sample == 'mp4a') {
|
|
|
|
this.parse_mp4a();
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 13:28:30 +05:00
|
|
|
this.idx = curr_track_end; // always skip to end of current processed track
|
2023-08-23 22:47:15 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_avc1() {
|
2023-08-24 13:28:30 +05:00
|
|
|
this.parse_into('avc1', 86); // avc1 has 78 bytes of frame metadata to skip
|
2023-08-23 23:13:07 +05:00
|
|
|
this.parse_into('avcC', 8);
|
2023-08-24 13:28:30 +05:00
|
|
|
// ex, 01 64 00 1f
|
|
|
|
// 01 is avc version (always 1, bit-and it off)
|
|
|
|
// rest is video codec profile, rep as hex
|
|
|
|
const avc_profile = (this.data.getUint32(this.idx) & 0xffffff).toString(16);
|
2023-08-23 23:13:07 +05:00
|
|
|
this.codecs.push('avc1.' + avc_profile);
|
2023-08-23 22:47:15 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
parse_mp4a() {
|
2023-08-24 13:28:30 +05:00
|
|
|
this.parse_into('mp4a', 36); // 28 bytes of subboxes with unclear offsets
|
2023-08-23 23:13:07 +05:00
|
|
|
this.parse_into('esds', 12);
|
2023-08-24 13:28:30 +05:00
|
|
|
// mp4a is as mp4a.40.2
|
|
|
|
// oti has defined position, rep as hex
|
|
|
|
// aot as a subtype is defined as 5 bits of the fetched byte, rep as dec
|
|
|
|
const oti = this.data.getUint8(this.idx + 13).toString(16);
|
2023-08-23 23:13:07 +05:00
|
|
|
const aot = (this.data.getUint8(this.idx + 31) >> 3).toString(10);
|
|
|
|
this.codecs.push(["mp4a", oti, aot].join('.'));
|
2023-08-23 20:58:37 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
get_mime() {
|
|
|
|
return 'video/mp4; codecs="' + this.get_codec_string() + '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
get_codec_string() {
|
|
|
|
return this.codecs.join(', ');
|
|
|
|
}
|
2023-08-24 13:28:30 +05:00
|
|
|
|
|
|
|
// find the given box, also descend into its child box depth
|
|
|
|
// offset is almost always 8, sometimes 12 or 16, defined in spec not file
|
2023-08-23 22:47:15 +05:00
|
|
|
parse_into(name, header_offset) {
|
|
|
|
this.parse_until(name);
|
|
|
|
this.idx += header_offset;
|
|
|
|
}
|
|
|
|
|
2023-08-24 13:28:30 +05:00
|
|
|
// find the box with the given name at the current box depth
|
|
|
|
// acts as though the current box extends to EOF.
|
2023-08-23 20:58:37 +05:00
|
|
|
parse_until(name) {
|
|
|
|
let curr_head = null;
|
2023-08-23 22:47:15 +05:00
|
|
|
while (this.idx + 8 < this.data.byteLength) {
|
2023-08-23 20:58:37 +05:00
|
|
|
curr_head = this.read_next_head();
|
|
|
|
if (curr_head.name == name) {
|
2023-08-23 22:47:15 +05:00
|
|
|
return true;
|
2023-08-23 20:58:37 +05:00
|
|
|
} else {
|
|
|
|
this.idx += curr_head.len;
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 22:47:15 +05:00
|
|
|
return false;
|
2023-08-23 20:58:37 +05:00
|
|
|
}
|
|
|
|
|
2023-08-24 13:28:30 +05:00
|
|
|
// read the mp4 box header as if the idx is positioned there
|
2023-08-23 20:58:37 +05:00
|
|
|
read_next_head() {
|
|
|
|
return {"len": this.data.getUint32(this.idx), "name": this.data.getString(this.idx + 4, 4)};
|
|
|
|
}
|
|
|
|
}
|