-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
2927 lines (2658 loc) · 117 KB
/
Copy pathscript.js
File metadata and controls
2927 lines (2658 loc) · 117 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ============================================================
CMAF / fMP4 Inspector — ISO BMFF Box Parser
Pure JavaScript, zero dependencies
============================================================ */
(() => {
'use strict';
// ── DOM Refs ──
const $ = id => document.getElementById(id);
const dropZone = $('dropZone');
const fileInput = $('fileInput');
const fileUrlInput = $('fileUrl');
const loadUrlBtn = $('loadUrlBtn');
const loadingEl = $('loading');
const errorBox = $('errorBox');
const resultsEl = document.querySelector('.results');
const fileSummaryEl = $('fileSummary');
const boxTreeContent = $('boxTreeContent');
const expandAllBtn = $('expandAllBtn');
const collapseAllBtn = $('collapseAllBtn');
const copyResultsBtn = $('copyResultsBtn');
const exportJsonBtn = $('exportJsonBtn');
const themeToggle = $('themeToggle');
let parsedData = null; // last parse result
// ── URL History ──
const URL_HISTORY_KEY = 'cmaf-url-history';
const MAX_URL_HISTORY = 15;
function getUrlHistory() {
try { return JSON.parse(localStorage.getItem(URL_HISTORY_KEY) || '[]'); }
catch { return []; }
}
function addUrlToHistory(url) {
try {
let history = getUrlHistory();
history = history.filter(h => h !== url);
history.unshift(url);
if (history.length > MAX_URL_HISTORY) history = history.slice(0, MAX_URL_HISTORY);
localStorage.setItem(URL_HISTORY_KEY, JSON.stringify(history));
refreshUrlDatalist();
} catch {}
}
function refreshUrlDatalist() {
const dl = $('urlHistory');
if (!dl) return;
dl.innerHTML = '';
getUrlHistory().forEach(url => {
const opt = document.createElement('option');
opt.value = url;
opt.label = url.length > 60 ? '...' + url.slice(-57) : url;
dl.appendChild(opt);
});
}
// Show datalist dropdown on focus (workaround for some browsers)
fileUrlInput.addEventListener('focus', () => {
if (!fileUrlInput.value && getUrlHistory().length) {
fileUrlInput.value = ' ';
fileUrlInput.value = '';
}
});
$('manifestUrl').addEventListener('focus', () => {
const el = $('manifestUrl');
if (!el.value && getUrlHistory().length) {
el.value = ' ';
el.value = '';
}
});
refreshUrlDatalist();
// ── Theme ──
const savedTheme = localStorage.getItem('cmaf-theme') || 'dark';
document.documentElement.setAttribute('data-theme', savedTheme);
themeToggle.textContent = savedTheme === 'dark' ? '🌙' : '☀️';
themeToggle.addEventListener('click', () => {
const current = document.documentElement.getAttribute('data-theme');
const next = current === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', next);
themeToggle.textContent = next === 'dark' ? '🌙' : '☀️';
localStorage.setItem('cmaf-theme', next);
});
// ── Helpers ──
function formatBytes(n) {
if (n === 0) return '0 B';
const k = 1024, units = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(n) / Math.log(k));
return parseFloat((n / Math.pow(k, i)).toFixed(2)) + ' ' + units[i];
}
function showError(msg) {
errorBox.textContent = msg;
errorBox.style.display = 'block';
loadingEl.style.display = 'none';
}
function clearError() {
errorBox.style.display = 'none';
errorBox.textContent = '';
}
function showLoading() {
loadingEl.style.display = 'flex';
resultsEl.style.display = 'none';
clearError();
}
function hideLoading() {
loadingEl.style.display = 'none';
}
// ── Drag & Drop ──
dropZone.addEventListener('dragover', e => { e.preventDefault(); dropZone.classList.add('drag-over'); });
dropZone.addEventListener('dragleave', () => dropZone.classList.remove('drag-over'));
dropZone.addEventListener('drop', e => {
e.preventDefault();
dropZone.classList.remove('drag-over');
const file = e.dataTransfer.files[0];
if (file) handleFile(file);
});
fileInput.addEventListener('change', e => {
const file = e.target.files[0];
if (file) handleFile(file);
});
// ── URL Load ──
loadUrlBtn.addEventListener('click', () => {
const url = fileUrlInput.value.trim();
if (!url) return;
handleUrl(url);
});
fileUrlInput.addEventListener('keydown', e => {
if (e.key === 'Enter') {
const url = fileUrlInput.value.trim();
if (url) handleUrl(url);
}
});
async function handleUrl(url) {
// Clear manifest scanner state
$('manifestUrl').value = '';
$('manifestResults').innerHTML = '';
$('manifestError').style.display = 'none';
$('manifestLoading').style.display = 'none';
showLoading();
try {
// Validate URL format
let parsedUrl;
try {
parsedUrl = new URL(url);
} catch {
throw new Error('Invalid URL format. Please enter a valid HTTP/HTTPS URL.');
}
if (!['http:', 'https:'].includes(parsedUrl.protocol)) {
throw new Error('Only HTTP and HTTPS URLs are supported.');
}
const resp = await fetch(url, { mode: 'cors' });
if (!resp.ok) {
throw new Error(`HTTP ${resp.status} ${resp.statusText}\nURL: ${url}\n\nPossible causes:\n• The URL may be incorrect or expired\n• The server may be down\n• Network connectivity issues`);
}
const contentType = resp.headers.get('content-type') || '';
const buffer = await resp.arrayBuffer();
if (buffer.byteLength === 0) {
throw new Error('Empty response received from server.');
}
const fileName = url.split('/').pop().split('?')[0] || 'remote-file';
addUrlToHistory(url);
processBuffer(buffer, fileName, buffer.byteLength);
} catch (err) {
hideLoading();
if (err.name === 'TypeError' && err.message === 'Failed to fetch') {
showError(
`Failed to fetch the URL.\n\n` +
`URL: ${url}\n\n` +
`Possible causes:\n` +
`• CORS: The server doesn't allow cross-origin requests\n` +
`• The server is not reachable (internal/VPN-only network)\n` +
`• SSL/TLS certificate issues\n` +
`• Network connectivity problems\n\n` +
`Workaround: Download the file manually and drag it into the drop zone.`
);
} else {
showError(err.message);
}
}
}
// ── File Handler ──
function handleFile(file) {
// Clear manifest scanner state
$('manifestUrl').value = '';
$('manifestResults').innerHTML = '';
$('manifestError').style.display = 'none';
$('manifestLoading').style.display = 'none';
showLoading();
const reader = new FileReader();
reader.onload = () => {
processBuffer(reader.result, file.name, file.size);
};
reader.onerror = () => {
hideLoading();
showError('Failed to read the file.');
};
reader.readAsArrayBuffer(file);
}
// ── Core Processing ──
function processBuffer(buffer, fileName, fileSize, sourceUrl) {
try {
const data = new DataView(buffer);
const boxes = parseBoxes(data, 0, buffer.byteLength, buffer);
if (boxes.length === 0) {
throw new Error(
'No valid ISO BMFF boxes found in this file.\n\n' +
'This tool supports ISO BMFF / CMAF / fMP4 container formats.\n' +
'Supported extensions: .mp4, .m4s, .m4a, .m4v, .cmaf, .fmp4\n\n' +
'Common non-ISO-BMFF formats: .mp3, .ts, .avi, .mkv, .webm'
);
}
parsedData = { fileName, fileSize, boxes, buffer, sourceUrl };
renderResults(parsedData);
hideLoading();
resultsEl.style.display = '';
copyResultsBtn.style.display = '';
exportJsonBtn.style.display = '';
} catch (err) {
hideLoading();
showError(err.message);
}
}
// ======================================================================
// ISO BMFF BOX PARSER
// ======================================================================
const BOX_DESCRIPTIONS = {
ftyp: 'File Type Box',
styp: 'Segment Type Box',
moov: 'Movie Box',
mvhd: 'Movie Header Box',
trak: 'Track Box',
tkhd: 'Track Header Box',
edts: 'Edit List Container Box',
elst: 'Edit List Box',
mdia: 'Media Box',
mdhd: 'Media Header Box',
hdlr: 'Handler Reference Box',
minf: 'Media Information Box',
vmhd: 'Video Media Header Box',
smhd: 'Sound Media Header Box',
hmhd: 'Hint Media Header Box',
nmhd: 'Null Media Header Box',
sthd: 'Subtitle Media Header Box',
dinf: 'Data Information Box',
dref: 'Data Reference Box',
stbl: 'Sample Table Box',
stsd: 'Sample Description Box',
stts: 'Time-to-Sample Box',
ctts: 'Composition Offset Box',
stsc: 'Sample-to-Chunk Box',
stsz: 'Sample Size Box',
stz2: 'Compact Sample Size Box',
stco: 'Chunk Offset Box',
co64: 'Chunk Offset Box (64-bit)',
stss: 'Sync Sample Box',
sdtp: 'Sample Dependency Box',
sgpd: 'Sample Group Description Box',
sbgp: 'Sample-to-Group Box',
cslg: 'Composition Shift Least Greatest Box',
mvex: 'Movie Extends Box',
trex: 'Track Extends Box',
mehd: 'Movie Extends Header Box',
moof: 'Movie Fragment Box',
mfhd: 'Movie Fragment Header Box',
traf: 'Track Fragment Box',
tfhd: 'Track Fragment Header Box',
tfdt: 'Track Fragment Decode Time Box',
trun: 'Track Fragment Run Box',
mdat: 'Media Data Box',
free: 'Free Space Box',
skip: 'Free Space Box (skip)',
udta: 'User Data Box',
meta: 'Metadata Box',
ilst: 'Item List Box',
sidx: 'Segment Index Box',
ssix: 'Subsegment Index Box',
mfra: 'Movie Fragment Random Access Box',
mfro: 'Movie Fragment Random Access Offset Box',
tfra: 'Track Fragment Random Access Box',
pssh: 'Protection System Specific Header Box',
sinf: 'Protection Scheme Information Box',
frma: 'Original Format Box',
schm: 'Scheme Type Box',
schi: 'Scheme Information Box',
tenc: 'Track Encryption Box',
saiz: 'Sample Auxiliary Information Sizes Box',
saio: 'Sample Auxiliary Information Offsets Box',
senc: 'Sample Encryption Box',
emsg: 'Event Message Box',
prft: 'Producer Reference Time Box',
leva: 'Level Assignment Box',
pdin: 'Progressive Download Information Box',
bloc: 'Base Location Box',
avc1: 'AVC Video Sample Entry Box',
avc3: 'AVC Video Sample Entry Box',
hev1: 'HEVC Video Sample Entry Box',
hvc1: 'HEVC Video Sample Entry Box',
vp08: 'VP8 Video Sample Entry Box',
vp09: 'VP9 Video Sample Entry Box',
av01: 'AV1 Video Sample Entry Box',
mp4a: 'MPEG-4 Audio Sample Entry Box',
mp4v: 'MPEG-4 Video Sample Entry Box',
ec_3: 'E-AC-3 Audio Sample Entry Box',
'ec-3': 'E-AC-3 Audio Sample Entry Box',
ac_3: 'AC-3 Audio Sample Entry Box',
'ac-3': 'AC-3 Audio Sample Entry Box',
ac_4: 'AC-4 Audio Sample Entry Box',
'ac-4': 'AC-4 Audio Sample Entry Box',
Opus: 'Opus Audio Sample Entry Box',
fLaC: 'FLAC Audio Sample Entry Box',
dtsl: 'DTS Lossless Audio Box',
dtsh: 'DTS-HD Audio Box',
dtse: 'DTS Express Audio Box',
dtsx: 'DTS:X Audio Box',
avcC: 'AVC Decoder Configuration Record Box',
hvcC: 'HEVC Decoder Configuration Record Box',
av1C: 'AV1 Codec Configuration Record Box',
vpcC: 'VP Codec Configuration Box',
esds: 'ES Descriptor Box',
dac3: 'AC-3 Specific Box',
dec3: 'E-AC-3 Specific Box',
dac4: 'AC-4 Specific Box',
dOps: 'Opus Specific Box',
dfLa: 'FLAC Specific Box',
btrt: 'Bit Rate Box',
pasp: 'Pixel Aspect Ratio Box',
colr: 'Colour Information Box',
clap: 'Clean Aperture Box',
ccst: 'Coding Constraints Box',
stpp: 'Subtitle Sample Entry Box (TTML)',
wvtt: 'WebVTT Sample Entry Box',
tx3g: 'Timed Text Sample Entry Box',
vttC: 'WebVTT Configuration Box',
'vlab': 'WebVTT Label Box',
mime: 'MIME Type Box',
// Container boxes missing descriptions
ipro: 'Item Protection Box',
rinf: 'Restricted Scheme Information Box',
strk: 'Sub-Track Box',
meco: 'Additional Metadata Container Box',
mere: 'Metabox Relation Box',
// HEIF / AVIF (ISO 14496-12 + ISO 23008-12)
iloc: 'Item Location Box',
iinf: 'Item Information Box',
infe: 'Item Information Entry Box',
pitm: 'Primary Item Box',
ipma: 'Item Property Association Box',
iprp: 'Item Properties Box',
ispe: 'Image Spatial Extents Box',
pixi: 'Pixel Information Box',
auxC: 'Auxiliary Type Property Box',
grpl: 'Group List Box',
// Sub-sample / Degradation
subs: 'Sub-Sample Information Box',
stdp: 'Degradation Priority Box',
padb: 'Padding Bits Box',
csgp: 'Compact Sample-to-Group Box',
// User Metadata
cprt: 'Copyright Box',
kind: 'Kind Box',
// 3GPP Metadata
loci: 'Location Information Box',
titl: 'Title Box',
dscp: 'Description Box',
albm: 'Album Title Box',
yrrc: 'Recording Year Box',
gnre: 'Genre Box',
perf: 'Performer Box',
auth: 'Author Box',
};
// ISO/IEC specification reference for each box type
const BOX_SPECS = {
// ISO 14496-12:2026 (ISO Base Media File Format / ISOBMFF — 8th Edition)
ftyp: 'ISO 14496-12:2026 §4.3', styp: 'ISO 14496-12:2026 §8.16.2',
moov: 'ISO 14496-12:2026 §8.2.1', mvhd: 'ISO 14496-12:2026 §8.2.2',
trak: 'ISO 14496-12:2026 §8.3.1', tkhd: 'ISO 14496-12:2026 §8.3.2',
edts: 'ISO 14496-12:2026 §8.6.4', elst: 'ISO 14496-12:2026 §8.6.6',
mdia: 'ISO 14496-12:2026 §8.4.1', mdhd: 'ISO 14496-12:2026 §8.4.2',
hdlr: 'ISO 14496-12:2026 §8.4.3', minf: 'ISO 14496-12:2026 §8.4.4',
vmhd: 'ISO 14496-12:2026 §12.1.2', smhd: 'ISO 14496-12:2026 §12.2.2',
hmhd: 'ISO 14496-12:2026 §12.4.2', nmhd: 'ISO 14496-12:2026 §12.1.2',
sthd: 'ISO 14496-12:2026 §12.6.2',
dinf: 'ISO 14496-12:2026 §8.7.1', dref: 'ISO 14496-12:2026 §8.7.2',
stbl: 'ISO 14496-12:2026 §8.5.1', stsd: 'ISO 14496-12:2026 §8.5.2',
stts: 'ISO 14496-12:2026 §8.6.1.2', ctts: 'ISO 14496-12:2026 §8.6.1.3',
stsc: 'ISO 14496-12:2026 §8.7.4', stsz: 'ISO 14496-12:2026 §8.7.3',
stz2: 'ISO 14496-12:2026 §8.7.3.3', stco: 'ISO 14496-12:2026 §8.7.5',
co64: 'ISO 14496-12:2026 §8.7.5', stss: 'ISO 14496-12:2026 §8.6.2',
sdtp: 'ISO 14496-12:2026 §8.6.4.2', sgpd: 'ISO 14496-12:2026 §8.9.3',
sbgp: 'ISO 14496-12:2026 §8.9.2', csgp: 'ISO 14496-12:2026 §8.9.4',
cslg: 'ISO 14496-12:2026 §8.6.1.4',
subs: 'ISO 14496-12:2026 §8.7.7', stdp: 'ISO 14496-12:2026 §8.7.6',
padb: 'ISO 14496-12:2026 §8.7.6.2',
mvex: 'ISO 14496-12:2026 §8.8.1', trex: 'ISO 14496-12:2026 §8.8.3',
mehd: 'ISO 14496-12:2026 §8.8.2',
moof: 'ISO 14496-12:2026 §8.8.4', mfhd: 'ISO 14496-12:2026 §8.8.5',
traf: 'ISO 14496-12:2026 §8.8.6', tfhd: 'ISO 14496-12:2026 §8.8.7',
tfdt: 'ISO 14496-12:2026 §8.8.12', trun: 'ISO 14496-12:2026 §8.8.8',
mdat: 'ISO 14496-12:2026 §8.1.1', free: 'ISO 14496-12:2026 §8.1.2',
skip: 'ISO 14496-12:2026 §8.1.2',
udta: 'ISO 14496-12:2026 §8.10.1', cprt: 'ISO 14496-12:2026 §8.10.2',
kind: 'ISO 14496-12:2026 §8.10.3',
meta: 'ISO 14496-12:2026 §8.11.1', ilst: 'ISO 14496-12:2026 §8.11.1.2',
sidx: 'ISO 14496-12:2026 §8.16.3', ssix: 'ISO 14496-12:2026 §8.16.4',
mfra: 'ISO 14496-12:2026 §8.8.9', mfro: 'ISO 14496-12:2026 §8.8.11',
tfra: 'ISO 14496-12:2026 §8.8.10',
leva: 'ISO 14496-12:2026 §8.13.2', pdin: 'ISO 14496-12:2026 §8.1.3',
emsg: 'ISO 14496-12:2026 §8.16.5', prft: 'ISO 14496-12:2026 §8.16.6',
btrt: 'ISO 14496-12:2026 §8.5.2.2', pasp: 'ISO 14496-12:2026 §12.1.4',
colr: 'ISO 14496-12:2026 §12.1.5', clap: 'ISO 14496-12:2026 §12.1.4.2',
ccst: 'ISO 14496-12:2026 §12.1.3',
// Common Encryption (CENC) — ISO 23001-7:2023
pssh: 'ISO 23001-7:2023 §8.1', sinf: 'ISO 14496-12:2026 §8.12.1',
frma: 'ISO 14496-12:2026 §8.12.2', schm: 'ISO 14496-12:2026 §8.12.5',
schi: 'ISO 14496-12:2026 §8.12.6', tenc: 'ISO 23001-7:2023 §8.2',
saiz: 'ISO 14496-12:2026 §8.7.8', saio: 'ISO 14496-12:2026 §8.7.9',
senc: 'ISO 23001-7:2023 §7.2',
// ISO 14496-14:2020 (MP4 File Format)
esds: 'ISO 14496-14:2020 §5.6.1',
mp4a: 'ISO 14496-14:2020 §5.6.1', mp4v: 'ISO 14496-14:2020 §5.6.1',
// ISO 14496-15:2022 (AVC/HEVC/VVC in ISOBMFF)
avc1: 'ISO 14496-15:2022 §5.3.4', avc3: 'ISO 14496-15:2022 §5.3.4',
avcC: 'ISO 14496-15:2022 §5.3.3.1',
hev1: 'ISO 14496-15:2022 §8.4.1', hvc1: 'ISO 14496-15:2022 §8.4.1',
hvcC: 'ISO 14496-15:2022 §8.3.3.1',
// AV1 Codec ISO Media File Format Binding (AV1-ISOBMFF)
av01: 'AV1-ISOBMFF v1.2.0 §2.1', av1C: 'AV1-ISOBMFF v1.2.0 §2.3.1',
// VP Codec ISO Media File Format Binding
vp08: 'VP Codec ISO Media §4.1', vp09: 'VP Codec ISO Media §4.1',
vpcC: 'VP Codec ISO Media §4.2',
// ETSI TS 102 366 (AC-3, E-AC-3)
ac_3: 'ETSI TS 102 366 §F.3', 'ac-3': 'ETSI TS 102 366 §F.3',
dac3: 'ETSI TS 102 366 §F.4', ec_3: 'ETSI TS 102 366 §F.5',
'ec-3': 'ETSI TS 102 366 §F.5', dec3: 'ETSI TS 102 366 §F.6',
ac_4: 'ETSI TS 103 190-2 §E.5', 'ac-4': 'ETSI TS 103 190-2 §E.5',
dac4: 'ETSI TS 103 190-2 §E.6',
// RFC 7845 (Opus in ISOBMFF)
Opus: 'RFC 7845 / Opus-in-ISOBMFF §3.1', dOps: 'RFC 7845 / Opus-in-ISOBMFF §3.2',
// FLAC in ISOBMFF
fLaC: 'FLAC-in-ISOBMFF §3.3.1', dfLa: 'FLAC-in-ISOBMFF §3.3.2',
// DTS (ETSI TS 102 114)
dtsl: 'ETSI TS 102 114', dtsh: 'ETSI TS 102 114',
dtse: 'ETSI TS 102 114', dtsx: 'ETSI TS 102 114',
// ISO 14496-30:2018 (Timed Text / WebVTT / TTML)
stpp: 'ISO 14496-30:2018 §4.4', wvtt: 'ISO 14496-30:2018 §6.2',
tx3g: 'ISO 14496-17:2006 §4.1', vttC: 'ISO 14496-30:2018 §6.3',
'vlab': 'ISO 14496-30:2018 §6.4', mime: 'ISO 14496-12:2026 §8.4.1',
// HEIF / AVIF (ISO 23008-12:2022)
iloc: 'ISO 23008-12:2022 §8.3', iinf: 'ISO 23008-12:2022 §8.6',
infe: 'ISO 23008-12:2022 §8.6.2', pitm: 'ISO 23008-12:2022 §8.4',
ipma: 'ISO 23008-12:2022 §9.3.2', iprp: 'ISO 23008-12:2022 §9.3.1',
ispe: 'ISO 23008-12:2022 §6.5.3.2', pixi: 'ISO 23008-12:2022 §6.5.6',
auxC: 'ISO 23008-12:2022 §6.5.8', grpl: 'ISO 23008-12:2022 §10.1',
// CMAF (ISO 23000-19:2024)
bloc: 'ISO 23000-19:2024 §7.3.2',
// 3GPP TS 26.244 (3GPP file format)
loci: '3GPP TS 26.244 §5.5', titl: '3GPP TS 26.244 §5.5',
dscp: '3GPP TS 26.244 §5.5', albm: '3GPP TS 26.244 §5.5',
yrrc: '3GPP TS 26.244 §5.5', gnre: '3GPP TS 26.244 §5.5',
perf: '3GPP TS 26.244 §5.5', auth: '3GPP TS 26.244 §5.5',
// Containers (ISOBMFF base spec)
ipro: 'ISO 14496-12:2026 §8.12.4', rinf: 'ISO 14496-12:2026 §8.12.7',
strk: 'ISO 14496-12:2026 §8.14.3', meco: 'ISO 14496-12:2026 §8.11.2',
mere: 'ISO 14496-12:2026 §8.11.3',
};
const CONTAINER_BOXES = new Set([
'moov', 'trak', 'edts', 'mdia', 'minf', 'dinf', 'stbl', 'mvex',
'moof', 'traf', 'udta', 'sinf', 'schi', 'mfra', 'ilst', 'ipro',
'rinf', 'strk', 'meco', 'mere',
]);
// Boxes that are full boxes (version + flags) but also containers
const FULL_CONTAINER_BOXES = new Set(['meta', 'stsd']);
function getBoxCategory(type) {
// File type
if (['ftyp', 'styp'].includes(type)) return 'filetype';
// Fragment boxes (checked before container to avoid shadowing)
if (['tfhd', 'tfdt', 'trun', 'trex', 'mfhd'].includes(type)) return 'fragment';
// DRM / Protection (checked before container to avoid shadowing sinf/schi)
if (['pssh', 'sinf', 'frma', 'schm', 'schi', 'tenc', 'saiz', 'saio', 'senc',
'subs'].includes(type)) return 'drm';
// Index boxes (checked before container to avoid shadowing mfra)
if (['sidx', 'ssix', 'mfra', 'mfro', 'tfra', 'pdin', 'leva'].includes(type)) return 'index';
// Fragment containers
if (['moof', 'traf'].includes(type)) return 'fragment';
// Containers
if (['moov', 'trak', 'mdia', 'minf', 'dinf', 'stbl', 'mvex',
'edts', 'udta', 'ilst', 'meco', 'meta', 'iprp', 'grpl',
'ipro', 'rinf', 'strk', 'mere'].includes(type)) return 'container';
// Headers
if (['mvhd', 'tkhd', 'mdhd', 'hdlr', 'vmhd', 'smhd', 'hmhd', 'nmhd', 'sthd',
'mehd', 'elst'].includes(type)) return 'header';
// Sample / codec / media description
if (['stsd', 'stts', 'ctts', 'stsc', 'stsz', 'stz2', 'stco', 'co64', 'stss',
'sdtp', 'sgpd', 'sbgp', 'cslg', 'csgp', 'stdp', 'padb',
'avc1', 'avc3', 'hev1', 'hvc1', 'mp4a', 'mp4v', 'vp08', 'vp09', 'av01',
'ec_3', 'ec-3', 'ac_3', 'ac-3', 'ac_4', 'ac-4', 'Opus', 'fLaC',
'dtsl', 'dtsh', 'dtse', 'dtsx',
'avcC', 'hvcC', 'av1C', 'vpcC', 'esds', 'dac3', 'dec3', 'dac4', 'dOps', 'dfLa',
'btrt', 'pasp', 'colr', 'clap', 'ccst',
'stpp', 'wvtt', 'tx3g', 'vttC', 'vlab', 'mime',
'iloc', 'iinf', 'infe', 'pitm', 'ipma', 'ispe', 'pixi', 'auxC'
].includes(type)) return 'sample';
// Events
if (['emsg', 'prft'].includes(type)) return 'event';
// Data
if (['mdat', 'free', 'skip'].includes(type)) return 'data';
// Metadata
if (['cprt', 'kind', 'loci', 'titl', 'dscp', 'albm', 'yrrc', 'gnre', 'perf', 'auth',
'bloc'].includes(type)) return 'event';
return 'unknown';
}
// Read 4-byte ASCII type at offset
function readType(data, offset) {
let s = '';
for (let i = 0; i < 4; i++) {
const c = data.getUint8(offset + i);
if (c >= 0x20 && c <= 0x7e) {
s += String.fromCharCode(c);
} else {
s += '.';
}
}
return s;
}
// Read null-terminated string
function readString(data, offset, maxLen) {
let s = '';
for (let i = 0; i < maxLen; i++) {
const c = data.getUint8(offset + i);
if (c === 0) break;
s += String.fromCharCode(c);
}
return s;
}
// Read 64-bit unsigned
function readUint64(data, offset) {
const hi = data.getUint32(offset);
const lo = data.getUint32(offset + 4);
return hi * 0x100000000 + lo;
}
// Read fixed-point 16.16
function readFixed32(data, offset) {
const raw = data.getInt32(offset);
return raw / 65536;
}
// Read fixed-point 8.8
function readFixed16(data, offset) {
const raw = data.getInt16(offset);
return raw / 256;
}
// ── Main recursive box parser ──
function parseBoxes(data, start, end, buffer) {
const boxes = [];
let offset = start;
while (offset < end - 7) {
const boxStart = offset;
let size = data.getUint32(offset);
const type = readType(data, offset + 4);
let headerSize = 8;
let realSize = size;
// Extended size
if (size === 1) {
if (offset + 16 > end) break;
realSize = readUint64(data, offset + 8);
headerSize = 16;
} else if (size === 0) {
// Box extends to end of file
realSize = end - offset;
}
// Validate
if (realSize < headerSize || boxStart + realSize > end) break;
// Sanity: if type is all dots, probably garbage
if (type === '....') break;
const box = {
type,
offset: boxStart,
size: realSize,
headerSize,
description: BOX_DESCRIPTIONS[type] || '',
children: [],
details: {},
};
const payloadStart = boxStart + headerSize;
const payloadEnd = boxStart + realSize;
// Parse known boxes
try {
if (type === 'ftyp' || type === 'styp') {
parseFtyp(data, payloadStart, payloadEnd, box);
} else if (FULL_CONTAINER_BOXES.has(type)) {
// Read version+flags then parse children
if (payloadEnd - payloadStart >= 4) {
box.details.version = data.getUint8(payloadStart);
box.details.flags = (data.getUint8(payloadStart + 1) << 16) |
(data.getUint8(payloadStart + 2) << 8) |
data.getUint8(payloadStart + 3);
let childStart = payloadStart + 4;
if (type === 'stsd') {
if (payloadEnd - childStart >= 4) {
box.details.entryCount = data.getUint32(childStart);
childStart += 4;
}
}
box.children = parseBoxes(data, childStart, payloadEnd, buffer);
}
} else if (CONTAINER_BOXES.has(type)) {
box.children = parseBoxes(data, payloadStart, payloadEnd, buffer);
} else if (type === 'mvhd') {
parseMvhd(data, payloadStart, payloadEnd, box);
} else if (type === 'tkhd') {
parseTkhd(data, payloadStart, payloadEnd, box);
} else if (type === 'mdhd') {
parseMdhd(data, payloadStart, payloadEnd, box);
} else if (type === 'hdlr') {
parseHdlr(data, payloadStart, payloadEnd, box);
} else if (type === 'elst') {
parseElst(data, payloadStart, payloadEnd, box);
} else if (type === 'vmhd') {
parseVmhd(data, payloadStart, payloadEnd, box);
} else if (type === 'smhd') {
parseSmhd(data, payloadStart, payloadEnd, box);
} else if (type === 'dref') {
parseDref(data, payloadStart, payloadEnd, box, buffer);
} else if (type === 'stts') {
parseStts(data, payloadStart, payloadEnd, box);
} else if (type === 'ctts') {
parseCtts(data, payloadStart, payloadEnd, box);
} else if (type === 'stsc') {
parseStsc(data, payloadStart, payloadEnd, box);
} else if (type === 'stsz') {
parseStsz(data, payloadStart, payloadEnd, box);
} else if (type === 'stco' || type === 'co64') {
parseStco(data, payloadStart, payloadEnd, box, type === 'co64');
} else if (type === 'stss') {
parseStss(data, payloadStart, payloadEnd, box);
} else if (type === 'trex') {
parseTrex(data, payloadStart, payloadEnd, box);
} else if (type === 'mehd') {
parseMehd(data, payloadStart, payloadEnd, box);
} else if (type === 'mfhd') {
parseMfhd(data, payloadStart, payloadEnd, box);
} else if (type === 'tfhd') {
parseTfhd(data, payloadStart, payloadEnd, box);
} else if (type === 'tfdt') {
parseTfdt(data, payloadStart, payloadEnd, box);
} else if (type === 'trun') {
parseTrun(data, payloadStart, payloadEnd, box);
} else if (type === 'sidx') {
parseSidx(data, payloadStart, payloadEnd, box);
} else if (type === 'pssh') {
parsePssh(data, payloadStart, payloadEnd, box, buffer);
} else if (type === 'tenc') {
parseTenc(data, payloadStart, payloadEnd, box);
} else if (type === 'schm') {
parseSchm(data, payloadStart, payloadEnd, box);
} else if (type === 'frma') {
parseFrma(data, payloadStart, payloadEnd, box);
} else if (type === 'saiz') {
parseSaiz(data, payloadStart, payloadEnd, box);
} else if (type === 'saio') {
parseSaio(data, payloadStart, payloadEnd, box);
} else if (type === 'emsg') {
parseEmsg(data, payloadStart, payloadEnd, box);
} else if (type === 'prft') {
parsePrft(data, payloadStart, payloadEnd, box);
} else if (type === 'btrt') {
parseBtrt(data, payloadStart, payloadEnd, box);
} else if (type === 'pasp') {
parsePasp(data, payloadStart, payloadEnd, box);
} else if (type === 'colr') {
parseColr(data, payloadStart, payloadEnd, box);
} else if (type === 'esds') {
parseEsds(data, payloadStart, payloadEnd, box);
} else if (type === 'avcC') {
parseAvcC(data, payloadStart, payloadEnd, box);
} else if (type === 'hvcC') {
parseHvcC(data, payloadStart, payloadEnd, box);
} else if (type === 'mdat') {
box.details.dataSize = payloadEnd - payloadStart;
} else if (type === 'free' || type === 'skip') {
box.details.paddingSize = payloadEnd - payloadStart;
} else if (['avc1', 'avc3', 'hev1', 'hvc1', 'vp08', 'vp09', 'av01', 'mp4v'].includes(type)) {
parseVisualSampleEntry(data, payloadStart, payloadEnd, box, buffer);
} else if (['mp4a', 'ec_3', 'ec-3', 'ac_3', 'ac-3', 'ac_4', 'ac-4',
'Opus', 'fLaC', 'dtsl', 'dtsh', 'dtse', 'dtsx'].includes(type)) {
parseAudioSampleEntry(data, payloadStart, payloadEnd, box, buffer);
} else if (['stpp', 'wvtt', 'tx3g'].includes(type)) {
parseSubtitleSampleEntry(data, payloadStart, payloadEnd, box, buffer);
} else if (type === 'senc') {
parseSenc(data, payloadStart, payloadEnd, box);
} else if (type === 'subs') {
parseSubs(data, payloadStart, payloadEnd, box);
}
} catch (e) {
box.details._parseError = e.message;
}
// Store raw bytes reference for hex viewer
box._bufferOffset = boxStart;
box._bufferLength = realSize;
boxes.push(box);
offset = boxStart + realSize;
}
return boxes;
}
// ── Box Parsers ──
function parseFtyp(data, start, end, box) {
if (end - start < 8) return;
box.details.majorBrand = readType(data, start);
box.details.minorVersion = data.getUint32(start + 4);
const brands = [];
for (let i = start + 8; i + 4 <= end; i += 4) {
brands.push(readType(data, i));
}
box.details.compatibleBrands = brands;
}
function parseMvhd(data, start, end, box) {
if (end - start < 4) return;
const v = data.getUint8(start);
box.details.version = v;
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
let o = start + 4;
if (v === 1) {
box.details.creationTime = readUint64(data, o); o += 8;
box.details.modificationTime = readUint64(data, o); o += 8;
box.details.timescale = data.getUint32(o); o += 4;
box.details.duration = readUint64(data, o); o += 8;
} else {
box.details.creationTime = data.getUint32(o); o += 4;
box.details.modificationTime = data.getUint32(o); o += 4;
box.details.timescale = data.getUint32(o); o += 4;
box.details.duration = data.getUint32(o); o += 4;
}
if (box.details.timescale > 0) {
box.details.durationSec = (box.details.duration / box.details.timescale).toFixed(3) + 's';
}
box.details.rate = readFixed32(data, o); o += 4;
box.details.volume = readFixed16(data, o); o += 2;
o += 10; // reserved
o += 36; // matrix
o += 24; // pre-defined
if (o + 4 <= end) {
box.details.nextTrackID = data.getUint32(o);
}
}
function parseTkhd(data, start, end, box) {
if (end - start < 4) return;
const v = data.getUint8(start);
box.details.version = v;
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
const flagBits = box.details.flags;
const flagNames = [];
if (flagBits & 0x1) flagNames.push('enabled');
if (flagBits & 0x2) flagNames.push('in_movie');
if (flagBits & 0x4) flagNames.push('in_preview');
if (flagBits & 0x8) flagNames.push('size_is_aspect_ratio');
box.details.flagNames = flagNames.join(', ');
let o = start + 4;
if (v === 1) {
box.details.creationTime = readUint64(data, o); o += 8;
box.details.modificationTime = readUint64(data, o); o += 8;
box.details.trackID = data.getUint32(o); o += 4;
o += 4; // reserved
box.details.duration = readUint64(data, o); o += 8;
} else {
box.details.creationTime = data.getUint32(o); o += 4;
box.details.modificationTime = data.getUint32(o); o += 4;
box.details.trackID = data.getUint32(o); o += 4;
o += 4; // reserved
box.details.duration = data.getUint32(o); o += 4;
}
o += 8; // reserved
box.details.layer = data.getInt16(o); o += 2;
box.details.alternateGroup = data.getInt16(o); o += 2;
box.details.volume = readFixed16(data, o); o += 2;
o += 2; // reserved
o += 36; // matrix
if (o + 8 <= end) {
box.details.width = readFixed32(data, o); o += 4;
box.details.height = readFixed32(data, o); o += 4;
if (box.details.width > 0 && box.details.height > 0) {
box.details.resolution = `${Math.round(box.details.width)}x${Math.round(box.details.height)}`;
}
}
}
function parseMdhd(data, start, end, box) {
if (end - start < 4) return;
const v = data.getUint8(start);
box.details.version = v;
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
let o = start + 4;
if (v === 1) {
box.details.creationTime = readUint64(data, o); o += 8;
box.details.modificationTime = readUint64(data, o); o += 8;
box.details.timescale = data.getUint32(o); o += 4;
box.details.duration = readUint64(data, o); o += 8;
} else {
box.details.creationTime = data.getUint32(o); o += 4;
box.details.modificationTime = data.getUint32(o); o += 4;
box.details.timescale = data.getUint32(o); o += 4;
box.details.duration = data.getUint32(o); o += 4;
}
if (box.details.timescale > 0) {
box.details.durationSec = (box.details.duration / box.details.timescale).toFixed(3) + 's';
}
if (o + 2 <= end) {
const langBits = data.getUint16(o);
const c1 = ((langBits >> 10) & 0x1f) + 0x60;
const c2 = ((langBits >> 5) & 0x1f) + 0x60;
const c3 = (langBits & 0x1f) + 0x60;
box.details.language = String.fromCharCode(c1, c2, c3);
}
}
function parseHdlr(data, start, end, box) {
if (end - start < 12) return;
box.details.version = data.getUint8(start);
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
// pre-defined
box.details.handlerType = readType(data, start + 8);
const handlerMap = { vide: 'Video', soun: 'Audio', hint: 'Hint', text: 'Text', subt: 'Subtitle', meta: 'Metadata', clcp: 'Closed Caption' };
box.details.handlerName = handlerMap[box.details.handlerType] || box.details.handlerType;
// Name string
if (start + 24 < end) {
box.details.name = readString(data, start + 24, end - start - 24);
}
}
function parseElst(data, start, end, box) {
if (end - start < 8) return;
const v = data.getUint8(start);
box.details.version = v;
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
const count = data.getUint32(start + 4);
box.details.entryCount = count;
const entries = [];
let o = start + 8;
const maxEntries = Math.min(count, 100);
for (let i = 0; i < maxEntries && o < end; i++) {
const entry = {};
if (v === 1) {
entry.segmentDuration = readUint64(data, o); o += 8;
entry.mediaTime = readUint64(data, o); o += 8;
} else {
entry.segmentDuration = data.getUint32(o); o += 4;
entry.mediaTime = data.getInt32(o); o += 4;
}
entry.mediaRateInteger = data.getInt16(o); o += 2;
entry.mediaRateFraction = data.getInt16(o); o += 2;
entries.push(entry);
}
box.details.entries = entries;
}
function parseVmhd(data, start, end, box) {
if (end - start < 12) return;
box.details.version = data.getUint8(start);
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
box.details.graphicsMode = data.getUint16(start + 4);
box.details.opcolor = [data.getUint16(start + 6), data.getUint16(start + 8), data.getUint16(start + 10)];
}
function parseSmhd(data, start, end, box) {
if (end - start < 8) return;
box.details.version = data.getUint8(start);
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
box.details.balance = readFixed16(data, start + 4);
}
function parseDref(data, start, end, box, buffer) {
if (end - start < 8) return;
box.details.version = data.getUint8(start);
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
box.details.entryCount = data.getUint32(start + 4);
box.children = parseBoxes(data, start + 8, end, buffer);
}
function parseStts(data, start, end, box) {
if (end - start < 8) return;
box.details.version = data.getUint8(start);
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
const count = data.getUint32(start + 4);
box.details.entryCount = count;
const entries = [];
let o = start + 8;
const maxEntries = Math.min(count, 200);
for (let i = 0; i < maxEntries && o + 8 <= end; i++) {
entries.push({ sampleCount: data.getUint32(o), sampleDelta: data.getUint32(o + 4) });
o += 8;
}
box.details.entries = entries;
if (count > maxEntries) box.details._truncated = count;
}
function parseCtts(data, start, end, box) {
if (end - start < 8) return;
box.details.version = data.getUint8(start);
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
const count = data.getUint32(start + 4);
box.details.entryCount = count;
const entries = [];
let o = start + 8;
const maxEntries = Math.min(count, 200);
for (let i = 0; i < maxEntries && o + 8 <= end; i++) {
entries.push({ sampleCount: data.getUint32(o), sampleOffset: box.details.version === 0 ? data.getUint32(o + 4) : data.getInt32(o + 4) });
o += 8;
}
box.details.entries = entries;
if (count > maxEntries) box.details._truncated = count;
}
function parseStsc(data, start, end, box) {
if (end - start < 8) return;
box.details.version = data.getUint8(start);
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
const count = data.getUint32(start + 4);
box.details.entryCount = count;
const entries = [];
let o = start + 8;
const maxEntries = Math.min(count, 200);
for (let i = 0; i < maxEntries && o + 12 <= end; i++) {
entries.push({ firstChunk: data.getUint32(o), samplesPerChunk: data.getUint32(o + 4), sampleDescIndex: data.getUint32(o + 8) });
o += 12;
}
box.details.entries = entries;
if (count > maxEntries) box.details._truncated = count;
}
function parseStsz(data, start, end, box) {
if (end - start < 12) return;
box.details.version = data.getUint8(start);
box.details.flags = (data.getUint8(start + 1) << 16) | (data.getUint8(start + 2) << 8) | data.getUint8(start + 3);
box.details.sampleSize = data.getUint32(start + 4);
box.details.sampleCount = data.getUint32(start + 8);
if (box.details.sampleSize === 0) {
const sizes = [];
let o = start + 12;
const max = Math.min(box.details.sampleCount, 200);
for (let i = 0; i < max && o + 4 <= end; i++) {
sizes.push(data.getUint32(o));
o += 4;
}
box.details.entries = sizes;
if (box.details.sampleCount > max) box.details._truncated = box.details.sampleCount;
}
}
function parseStco(data, start, end, box, is64) {
if (end - start < 8) return;
box.details.version = data.getUint8(start);