Media Metadata Inspector — JavaScript source
Inspect audio, video and image files at the byte level — MP4 boxes, ID3 tags, WAV chunks, GIF headers — parsed by our own readers, entirely in your browser.
This is the JavaScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
/**
* media-metadata-inspector - read container metadata straight from the bytes.
*
* Language: JavaScript (ES module; runs in browsers, Node >= 16, Bun, Deno).
* CosmoDev polyglot showcase port of the `media-metadata-inspector` tool,
* ported from src/lib/media-meta.ts (the canonical TypeScript implementation).
*
* Why this file exists: container headers are designed to be self-describing,
* so duration, dimensions, and tags fall out of a few integer reads - no
* ffmpeg, no <video>, no decoder. Sniff the signature, then walk the
* container's boxes / chunks / frames. Deterministic; probe() returns null
* only for inputs too short to even carry a signature.
*
* display source - part of CosmoDev's polyglot tool pages.
*/
// The one platform primitive the port needs: TextDecoder is a global in every
// modern browser and in Node >= 11, and decodes UTF-8 ID3 frames for us.
const utf8Decoder = new TextDecoder();
const ascii = (bytes, start, len) =>
String.fromCharCode(...bytes.subarray(start, start + len));
const u16 = (b, o) => b[o] | (b[o + 1] << 8);
const u32 = (b, o) =>
(b[o] * 0x1000000 + ((b[o + 1] << 16) | (b[o + 2] << 8) | b[o + 3])) >>> 0;
/** MP4 box walk: payload offsets [start, end) for every box of a wanted type. */
function findBoxes(bytes, start, end, wanted) {
const found = [];
let pos = start;
while (pos + 8 <= end) {
let size = u32(bytes, pos);
if (size === 1) return found; // 64-bit sizes unsupported - stop safely.
if (size === 0) size = end - pos; // box extends to the end
if (size < 8 || pos + size > end) return found;
const type = ascii(bytes, pos + 4, 4);
if (wanted.includes(type)) found.push({ type, start: pos + 8, end: pos + size });
pos += size;
}
return found;
}
/** MP4: moov -> mvhd gives duration; the first trak's tkhd gives dimensions. */
function probeMp4(bytes) {
const meta = { kind: 'mp4', durationSec: null, width: null, height: null };
for (const moov of findBoxes(bytes, 0, bytes.length, ['moov'])) {
for (const mvhd of findBoxes(bytes, moov.start, moov.end, ['mvhd'])) {
const version = bytes[mvhd.start];
if (version === 0 && mvhd.end - mvhd.start >= 20) {
const timescale = u32(bytes, mvhd.start + 12);
if (timescale > 0) meta.durationSec = u32(bytes, mvhd.start + 16) / timescale;
} else if (version === 1 && mvhd.end - mvhd.start >= 32) {
const timescale = u32(bytes, mvhd.start + 20);
// 64-bit duration: high and low u32 halves of the u64.
const duration = u32(bytes, mvhd.start + 24) * 0x100000000 + u32(bytes, mvhd.start + 28);
if (timescale > 0) meta.durationSec = duration / timescale;
}
}
for (const trak of findBoxes(bytes, moov.start, moov.end, ['trak'])) {
const tkhd = findBoxes(bytes, trak.start, trak.end, ['tkhd'])[0];
// tkhd stores width/height as 16.16 fixed point - divide by 65536.
if (tkhd && bytes[tkhd.start] === 0 && tkhd.end - tkhd.start >= 84) {
meta.width = u32(bytes, tkhd.start + 76) / 65536;
meta.height = u32(bytes, tkhd.start + 80) / 65536;
break; // first track is enough
}
}
}
return meta;
}
/** ID3v2 syncsafe integer: four 7-bit groups so no byte sets the high bit. */
const syncsafe = (b, o) => (b[o] << 21) | (b[o + 1] << 14) | (b[o + 2] << 7) | b[o + 3];
/** Decode one ID3 text frame (first byte selects the encoding). */
export function decodeId3Text(bytes, start, len) {
if (len <= 0) return '';
const encoding = bytes[start];
const text = bytes.subarray(start + 1, start + len); // clamps, like the lib
if (encoding === 3) {
// UTF-8 (ID3v2.4); stop at the null terminator.
const end = text.indexOf(0);
return utf8Decoder.decode(end === -1 ? text : text.subarray(0, end));
}
if (encoding === 1 || encoding === 2) {
// UTF-16: encoding 1 carries a BOM (sniff endianness); 2 is BOM-less BE.
const bom = encoding === 1 && (text[0] === 0xff || text[0] === 0xfe);
const littleEndian = bom ? text[0] === 0xff : false;
let out = '';
for (let i = bom ? 2 : 0; i + 1 < text.length; i += 2) {
const code = littleEndian ? text[i] | (text[i + 1] << 8) : (text[i] << 8) | text[i + 1];
if (code === 0) break;
out += String.fromCharCode(code);
}
return out;
}
let out = '';
for (const byte of text) {
if (byte === 0) break;
out += String.fromCharCode(byte); // ISO-8859-1 / ASCII range
}
return out;
}
/** ID3v2: walk frames, keep TIT2 (title), TPE1 (artist), TALB (album). */
function probeId3(bytes) {
const meta = { kind: 'mp3', durationSec: null, width: null, height: null };
const version = bytes[3];
const tagSize = syncsafe(bytes, 6);
let pos = 10;
const end = Math.min(10 + tagSize, bytes.length);
while (pos + 10 <= end) {
const id = ascii(bytes, pos, 4);
if (!/^[A-Z0-9]{4}$/.test(id)) break;
const size = version === 4 ? syncsafe(bytes, pos + 4) : u32(bytes, pos + 4);
if (size === 0) break;
// Clamp declared size to the tag end: a corrupt frame stays inside the tag.
const avail = end - (pos + 10);
const text = decodeId3Text(bytes, pos + 10, Math.min(size, Math.max(avail, 0)));
if (id === 'TIT2') meta.title = text;
else if (id === 'TPE1') meta.artist = text;
else if (id === 'TALB') meta.album = text;
pos += 10 + size;
}
return meta;
}
/** WAV: fmt chunk gives byteRate; data chunk size / byteRate = duration. */
function probeWav(bytes) {
const meta = { kind: 'wav', durationSec: null, width: null, height: null };
let pos = 12; // past RIFF size + WAVE
let byteRate = 0;
while (pos + 8 <= bytes.length) {
const id = ascii(bytes, pos, 4);
const size = u32le(bytes, pos + 4); // RIFF fields are little-endian
if (id === 'fmt ' && pos + 8 + 16 <= bytes.length) {
byteRate = u32le(bytes, pos + 16); // byteRate sits at fmt payload offset 8
} else if (id === 'data' && byteRate > 0) {
meta.durationSec = size / byteRate;
break;
}
pos += 8 + size + (size % 2); // chunks are word-aligned
}
return meta;
}
/** RIFF/WAV fields are little-endian, unlike MP4/ID3. */
const u32le = (b, o) =>
(b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | b[o + 3] * 0x1000000) >>> 0;
/** Sniff the container and parse its metadata. Null when input is under 12 bytes. */
export function probe(bytes) {
if (bytes.length < 12) return null;
if (ascii(bytes, 4, 4) === 'ftyp') return probeMp4(bytes); // MP4 / MOV
if (ascii(bytes, 0, 3) === 'ID3') return probeId3(bytes);
if (ascii(bytes, 0, 4) === 'RIFF' && ascii(bytes, 8, 4) === 'WAVE') return probeWav(bytes);
if (ascii(bytes, 0, 4) === 'GIF8') {
return { kind: 'gif', durationSec: null, width: u16(bytes, 6), height: u16(bytes, 8) };
}
if (bytes[0] === 0x1a && bytes[1] === 0x45 && bytes[2] === 0xdf && bytes[3] === 0xa3) {
return { kind: 'webm', durationSec: null, width: null, height: null }; // EBML magic
}
return { kind: 'unknown', durationSec: null, width: null, height: null };
}
// Usage:
// const meta = probe(new Uint8Array(await file.arrayBuffer()));
// console.log(meta?.kind, meta?.durationSec, meta?.width, meta?.height);
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →