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-25 13:21:07 +05:00
|
|
|
// only doing h264 video since thats all I can check currently doing
|
|
|
|
// transcoding and running obs together with cpu usage
|
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;
|
|
|
|
}
|
2023-10-12 14:21:20 +05:00
|
|
|
let curr_track_end = this.idx + this.read_next_head().len;
|
2023-08-23 22:47:15 +05:00
|
|
|
this.idx += 8
|
|
|
|
this.parse_into('mdia', 8);
|
|
|
|
this.parse_into('minf', 8);
|
2023-10-12 14:21:20 +05:00
|
|
|
let track_type_head = this.read_next_head().name;
|
2023-08-23 22:47:15 +05:00
|
|
|
if (track_type_head == 'smhd' || track_type_head == 'vmhd') {
|
|
|
|
this.parse_into('stbl', 8);
|
|
|
|
this.parse_into('stsd', 16);
|
2023-10-12 14:21:20 +05:00
|
|
|
let media_sample = this.read_next_head().name;
|
2023-08-23 22:47:15 +05:00
|
|
|
if (media_sample == 'avc1') {
|
|
|
|
this.parse_avc1();
|
2023-09-14 12:17:51 +05:00
|
|
|
} else if (media_sample == 'vp09') {
|
|
|
|
this.parse_vp09();
|
2023-10-12 14:21:20 +05:00
|
|
|
} else if (media_sample == 'av01') {
|
|
|
|
this.parse_av01();
|
2023-08-23 22:47:15 +05:00
|
|
|
} else if (media_sample == 'mp4a') {
|
|
|
|
this.parse_mp4a();
|
2023-08-25 16:04:27 +05:00
|
|
|
} else if (media_sample == 'Opus') {
|
|
|
|
this.codecs.push('opus');
|
2023-08-25 13:21:07 +05:00
|
|
|
} else {
|
|
|
|
this.codecs.push('unsup codec');
|
2023-08-23 22:47:15 +05:00
|
|
|
}
|
|
|
|
}
|
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
|
2023-10-12 14:21:20 +05:00
|
|
|
let 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
|
|
|
}
|
|
|
|
|
2023-09-14 12:17:51 +05:00
|
|
|
parse_vp09() {
|
|
|
|
this.parse_into('vp09', 86);
|
|
|
|
this.parse_into('vpcC', 12);
|
2023-10-12 14:21:20 +05:00
|
|
|
let profile = this.data.getUint8(this.idx).toString().padStart(2, "0");
|
|
|
|
let level = this.data.getUint8(this.idx + 1).toString();
|
|
|
|
let depth = (this.data.getUint8(this.idx + 2) >> 4).toString().padStart(2, "0");
|
2023-09-14 12:17:51 +05:00
|
|
|
this.codecs.push(["vp09", profile, level, depth].join('.'));
|
|
|
|
}
|
|
|
|
|
2023-10-12 14:21:20 +05:00
|
|
|
parse_av01() {
|
2023-10-26 00:19:40 +05:00
|
|
|
this.parse_into('av01', 86);
|
2023-10-12 14:21:20 +05:00
|
|
|
this.parse_into('av1C', 8);
|
|
|
|
let tmp = this.data.getUint8(this.idx + 1);
|
|
|
|
let profile = tmp >> 5;
|
|
|
|
let level = tmp & 0x1f;
|
2023-10-26 00:19:40 +05:00
|
|
|
let level_profile, bit_depth;
|
|
|
|
tmp = this.data.getUint8(this.idx + 2);
|
2023-10-12 14:21:20 +05:00
|
|
|
switch (tmp >> 7) {
|
|
|
|
case 0:
|
2023-10-26 00:19:40 +05:00
|
|
|
level_profile = "M";
|
2023-10-12 14:21:20 +05:00
|
|
|
break;
|
|
|
|
case 1:
|
2023-10-26 00:19:40 +05:00
|
|
|
level_profile = "H";
|
2023-10-12 14:21:20 +05:00
|
|
|
break;
|
|
|
|
default:
|
2023-10-26 00:19:40 +05:00
|
|
|
level_profile = "";
|
2023-10-12 14:21:20 +05:00
|
|
|
}
|
|
|
|
let depth_bits = (tmp >> 5) & 0x3;
|
|
|
|
switch (depth_bits) {
|
|
|
|
case 0:
|
2023-10-26 00:19:40 +05:00
|
|
|
bit_depth = "08";
|
2023-10-12 14:21:20 +05:00
|
|
|
break;
|
|
|
|
case 1:
|
2023-10-26 00:19:40 +05:00
|
|
|
bit_depth = "12";
|
2023-10-12 14:21:20 +05:00
|
|
|
break;
|
|
|
|
case 2:
|
2023-10-26 00:19:40 +05:00
|
|
|
bit_depth = "10";
|
2023-10-12 14:21:20 +05:00
|
|
|
break;
|
|
|
|
default:
|
2023-10-26 00:19:40 +05:00
|
|
|
bit_depth = "0";
|
2023-10-12 14:21:20 +05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.codecs.push(["av01", profile, level.toString().padStart(2, "0") + level_profile, bit_depth].join('.'))
|
|
|
|
}
|
|
|
|
|
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
|
2023-10-12 14:21:20 +05:00
|
|
|
let oti = this.data.getUint8(this.idx + 13).toString(16);
|
|
|
|
let aot = (this.data.getUint8(this.idx + 31) >> 3).toString(10);
|
2023-08-23 23:13:07 +05:00
|
|
|
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)};
|
|
|
|
}
|
|
|
|
}
|