parsing upto avc1 and mp4a boxes

This commit is contained in:
Muaz Ahmad 2023-08-23 22:47:15 +05:00
parent 33a01c9552
commit 7447699735

View file

@ -17,8 +17,38 @@ class MP4Tree {
} }
parse_codecs() { parse_codecs() {
this.parse_until('moov'); this.parse_into('moov', 8);
console.log(this.idx); 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() { get_mime() {
@ -29,21 +59,25 @@ class MP4Tree {
return this.codecs.join(', '); return this.codecs.join(', ');
} }
parse_into(name, header_offset) {
this.parse_until(name);
this.idx += header_offset;
}
parse_until(name) { parse_until(name) {
let curr_head = null; let curr_head = null;
while (this.idx < this.data.byteLength) { while (this.idx + 8 < this.data.byteLength) {
curr_head = this.read_next_head(); curr_head = this.read_next_head();
console.log(curr_head);
if (curr_head.name == name) { if (curr_head.name == name) {
break; return true;
} else { } else {
this.idx += curr_head.len; this.idx += curr_head.len;
} }
} }
return false;
} }
read_next_head() { read_next_head() {
return {"len": this.data.getUint32(this.idx), "name": this.data.getString(this.idx + 4, 4)}; return {"len": this.data.getUint32(this.idx), "name": this.data.getString(this.idx + 4, 4)};
} }
} }