// The Play view's mp4 index (mp4Index.mjs) against ffprobe: every video // sample's byte offset, size, presentation and decode time and keyframe flag, // on an ffmpeg-made file with B-frames (an edit list), moov first and last. // // Run with: node --test lib/report/mp4Index.test.mjs import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { mkdtempSync, readFileSync, rmSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { seekPoint, topBoxes, videoTrackOf } from "./mp4Index.mjs"; const have = ["ffmpeg", "ffprobe"].every((b) => spawnSync(b, ["-version"]).status === 0); const dir = mkdtempSync(path.join(os.tmpdir(), "mp4index-")); test.after(() => rmSync(dir, { recursive: true, force: true })); function make(name, extra) { const f = path.join(dir, name); const r = spawnSync("ffmpeg", [ "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=size=320x180:rate=30:duration=4", "-f", "lavfi", "-i", "sine=frequency=440:duration=4", "-c:v", "libx264", "-preset", "ultrafast", "-bf", "2", "-g", "30", "-c:a", "aac", ...extra, f, ]); assert.equal(r.status, 0, String(r.stderr)); return f; } function probe(f) { const r = spawnSync("ffprobe", ["-v", "error", "-select_streams", "v:0", "-show_packets", "-show_entries", "packet=pts_time,dts_time,pos,size,flags", "-of", "json", f]); return JSON.parse(String(r.stdout)).packets; } function moovOf(f) { const buf = readFileSync(f); const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength); const boxes = topBoxes(ab); const m = boxes.find((b) => b.type === "moov"); assert.ok(m, `moov in ${boxes.map((b) => b.type).join(",")}`); return ab.slice(m.offset, m.offset + m.size); } for (const [name, extra] of [["faststart.mp4", ["-movflags", "+faststart"]], ["moov-last.mp4", []]]) { test(`the video samples of ${name} are ffprobe's`, { skip: !have && "needs ffmpeg" }, () => { const f = make(name, extra); const track = videoTrackOf(moovOf(f)); assert.ok(track); assert.match(track.codec, /^avc1\.[0-9a-f]{6}$/); assert.ok(track.description && track.description.length > 8); assert.deepEqual([track.width, track.height], [320, 180]); const pk = probe(f); assert.equal(track.samples.length, pk.length); track.samples.forEach((s, i) => { const p = pk[i]; assert.equal(s.offset, Number(p.pos), `offset ${i}`); assert.equal(s.size, Number(p.size), `size ${i}`); assert.ok(Math.abs(s.pts - Number(p.pts_time)) < 1e-4, `pts ${i}: ${s.pts} vs ${p.pts_time}`); assert.ok(Math.abs(s.dts - Number(p.dts_time)) < 1e-4, `dts ${i}: ${s.dts} vs ${p.dts_time}`); assert.equal(s.key, p.flags.startsWith("K"), `key ${i}`); }); // The first presented frame is at 0; a seek to 1.5 s starts at the keyframe at 1.0 s. assert.equal(Math.min(...track.samples.map((s) => s.pts)), 0); const sp = seekPoint(track.samples, 1.5); assert.ok(track.samples[sp.key].key); assert.ok(Math.abs(track.samples[sp.key].pts - 1) < 1e-6); assert.ok(Math.abs(track.samples[sp.frame].pts - 1.5) < 1 / 30); }); } test("a top-level walk stops where the bytes stop, naming the box it could not hold", () => { const ab = new ArrayBuffer(24); const dv = new DataView(ab); dv.setUint32(0, 16); [..."ftyp"].forEach((c, i) => dv.setUint8(4 + i, c.charCodeAt(0))); dv.setUint32(16, 1000); [..."mdat"].forEach((c, i) => dv.setUint8(20 + i, c.charCodeAt(0))); assert.deepEqual(topBoxes(ab), [{ type: "ftyp", offset: 0, size: 16 }, { type: "mdat", offset: 16, size: 1000 }]); });