From 7447699735fec86bc90682d02507ce0a18a47fda Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Wed, 23 Aug 2023 22:47:15 +0500 Subject: [PATCH] parsing upto avc1 and mp4a boxes --- mp4-tree.js | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/mp4-tree.js b/mp4-tree.js index 61b3089..aec4acf 100644 --- a/mp4-tree.js +++ b/mp4-tree.js @@ -17,8 +17,38 @@ class MP4Tree { } parse_codecs() { - this.parse_until('moov'); - console.log(this.idx); + this.parse_into('moov', 8); + while (this.idx < this.data.byteLength) { + if (!this.parse_until('trak')) { + 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(); + } + } + this.idx = curr_track_end; + } + } + + parse_avc1() { + this.parse_into('avc1', 86); + console.log(this.read_next_head()); + } + + parse_mp4a() { + this.parse_into('mp4a', 36); + console.log(this.read_next_head()); } get_mime() { @@ -29,21 +59,25 @@ class MP4Tree { return this.codecs.join(', '); } + parse_into(name, header_offset) { + this.parse_until(name); + this.idx += header_offset; + } + parse_until(name) { let curr_head = null; - while (this.idx < this.data.byteLength) { + while (this.idx + 8 < this.data.byteLength) { curr_head = this.read_next_head(); - console.log(curr_head); if (curr_head.name == name) { - break; + return true; } else { this.idx += curr_head.len; } } + return false; } read_next_head() { return {"len": this.data.getUint32(this.idx), "name": this.data.getString(this.idx + 4, 4)}; } } -