131 lines
6.9 KiB
JavaScript
131 lines
6.9 KiB
JavaScript
#!/usr/bin/env node
|
|
// Verifies one course episode: media codecs/duration/audio, subtitles, HTTP
|
|
// range, page errors, stills and checkpoint presence. Writes evidence JSON.
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { spawnSync } from "node:child_process";
|
|
import { fileURLToPath } from "node:url";
|
|
import { checkLineCheckpoint } from "./lib/line-precision.mjs";
|
|
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
const webRoot = path.resolve(here, "../..");
|
|
const args = process.argv.slice(2);
|
|
const option = (name, fallback = "") => {
|
|
const index = args.indexOf(name);
|
|
return index < 0 ? fallback : args[index + 1] ?? fallback;
|
|
};
|
|
const episodeId = option("--episode");
|
|
if (!episodeId) throw new Error("--episode is required");
|
|
const course = JSON.parse(fs.readFileSync(path.join(here, "course.json"), "utf8"));
|
|
const episode = course.episodes.find(item => item.id === episodeId);
|
|
if (!episode) throw new Error(`unknown episode ${episodeId}`);
|
|
const episodeDir = path.join(here, "episodes", episodeId);
|
|
const rawDir = path.join(here, "raw", episodeId);
|
|
const evidenceDir = path.join(here, "evidence", episodeId);
|
|
fs.mkdirSync(evidenceDir, { recursive: true });
|
|
const base = `${episode.id}-${episode.slug}`;
|
|
const mediaDir = path.join(webRoot, "teaching", "media", "line-course");
|
|
const mp4 = path.join(mediaDir, `${base}.mp4`);
|
|
const webm = path.join(mediaDir, `${base}.webm`);
|
|
const vtt = path.join(mediaDir, `${base}.zh.vtt`);
|
|
const srt = path.join(mediaDir, `${base}.zh.srt`);
|
|
const poster = path.join(mediaDir, `${base}-poster.png`);
|
|
const checks = [];
|
|
const check = (name, passed, detail) => {
|
|
checks.push({ name, passed: Boolean(passed), detail });
|
|
if (!passed) throw new Error(`${name}: ${JSON.stringify(detail)}`);
|
|
};
|
|
const probe = file => {
|
|
const result = spawnSync("ffprobe", ["-v", "error", "-show_streams", "-show_format", "-of", "json", file], { encoding: "utf8", maxBuffer: 32 * 1024 * 1024 });
|
|
if (result.status !== 0) throw new Error(`ffprobe failed for ${file}: ${result.stderr}`);
|
|
return JSON.parse(result.stdout);
|
|
};
|
|
const volume = file => {
|
|
const result = spawnSync("ffmpeg", ["-hide_banner", "-i", file, "-af", "volumedetect", "-f", "null", "-"], { encoding: "utf8", maxBuffer: 32 * 1024 * 1024 });
|
|
const text = `${result.stderr || ""}`;
|
|
const mean = Number(/mean_volume:\s*(-?\d+(?:\.\d+)?) dB/.exec(text)?.[1]);
|
|
const max = Number(/max_volume:\s*(-?\d+(?:\.\d+)?) dB/.exec(text)?.[1]);
|
|
return { mean, max };
|
|
};
|
|
const fetchHead = async url => {
|
|
const response = await fetch(url, { method: "HEAD" });
|
|
return { status: response.status, type: response.headers.get("content-type"), ranges: response.headers.get("accept-ranges") };
|
|
};
|
|
|
|
check("mp4 exists", fs.existsSync(mp4), mp4);
|
|
check("webm exists", fs.existsSync(webm), webm);
|
|
check("vtt exists", fs.existsSync(vtt), vtt);
|
|
check("srt exists", fs.existsSync(srt), srt);
|
|
check("poster exists", fs.existsSync(poster), poster);
|
|
const mp4Probe = probe(mp4);
|
|
const webmProbe = probe(webm);
|
|
const video = mp4Probe.streams.find(stream => stream.codec_type === "video");
|
|
const audio = mp4Probe.streams.find(stream => stream.codec_type === "audio");
|
|
const webmVideo = webmProbe.streams.find(stream => stream.codec_type === "video");
|
|
const webmAudio = webmProbe.streams.find(stream => stream.codec_type === "audio");
|
|
const duration = Number(mp4Probe.format.duration);
|
|
check("mp4 h264 1600x900", video?.codec_name === "h264" && video.width === 1600 && video.height === 900, video);
|
|
check("mp4 aac audio", audio?.codec_name === "aac", audio);
|
|
check("webm vp9/opus", webmVideo?.codec_name === "vp9" && webmAudio?.codec_name === "opus", { video: webmVideo?.codec_name, audio: webmAudio?.codec_name });
|
|
const [minDuration, maxDuration] = episode.durationRange ?? [60, 1800];
|
|
check("duration in range", duration >= minDuration && duration <= maxDuration, { duration, range: [minDuration, maxDuration] });
|
|
const levels = volume(mp4);
|
|
check("audio not silent", levels.mean > -50 && levels.max > -9, levels);
|
|
const cueCount = (fs.readFileSync(vtt, "utf8").match(/-->/g) ?? []).length;
|
|
check("vtt cues", cueCount > 0, cueCount);
|
|
|
|
const mediaUrl = `http://127.0.0.1:5190/media/line-course/${base}.mp4`;
|
|
const head = await fetchHead(mediaUrl);
|
|
check("media HTTP 200", head.status === 200, head);
|
|
check("media MIME", /video\/mp4/.test(head.type ?? ""), head.type);
|
|
check("media Accept-Ranges", /bytes/.test(head.ranges ?? ""), head.ranges);
|
|
const range = await fetch(mediaUrl, { headers: { Range: "bytes=0-1023" } });
|
|
check("media Range 206", range.status === 206, range.status);
|
|
const badRange = await fetch(mediaUrl, { headers: { Range: "bytes=999999999-" } });
|
|
check("media bad Range 416", badRange.status === 416, badRange.status);
|
|
|
|
const sessionPath = path.join(rawDir, "session.json");
|
|
const session = fs.existsSync(sessionPath) ? JSON.parse(fs.readFileSync(sessionPath, "utf8")) : null;
|
|
check("session exists", Boolean(session), sessionPath);
|
|
check("0 pageErrors", (session?.pageErrors?.length ?? 0) === 0, session?.pageErrors);
|
|
check("0 consoleErrors", (session?.consoleErrors?.length ?? 0) === 0, session?.consoleErrors);
|
|
const stills = fs.existsSync(path.join(here, "stills", episodeId))
|
|
? fs.readdirSync(path.join(here, "stills", episodeId)).filter(name => name.endsWith(".png"))
|
|
: [];
|
|
check("stills exist", stills.length > 0, stills.length);
|
|
const checkpoint = path.join(here, "checkpoints", `${episodeId}.FCStd`);
|
|
if (episode.expectedObjects !== null && episode.expectedObjects !== undefined) {
|
|
check("checkpoint exists", fs.existsSync(checkpoint), checkpoint);
|
|
}
|
|
let precision = null;
|
|
const expectedObjects = episode.checkpointObjects ?? episode.objects;
|
|
if (fs.existsSync(checkpoint) && Array.isArray(expectedObjects) && expectedObjects.length) {
|
|
precision = await checkLineCheckpoint(checkpoint, expectedObjects);
|
|
check("precision profile", precision.ok, {
|
|
objectCount: precision.objectCount,
|
|
extra: precision.extra,
|
|
models: precision.models.filter(model => !model.ok),
|
|
failures: precision.results.filter(result => !result.ok).map(result => ({ name: result.name, issues: result.issues })),
|
|
});
|
|
}
|
|
const report = {
|
|
schema: "freecad-web-line-course/video-verification/v1",
|
|
generatedAt: new Date().toISOString(),
|
|
episode: episodeId,
|
|
title: episode.title,
|
|
status: "passed",
|
|
checks,
|
|
duration,
|
|
scenes: JSON.parse(fs.readFileSync(path.join(episodeDir, "assembly.json"), "utf8")).scenes,
|
|
cues: cueCount,
|
|
audio: levels,
|
|
stills,
|
|
precision,
|
|
checkpoint: fs.existsSync(checkpoint) ? path.relative(here, checkpoint) : null,
|
|
mp4: { video, audio },
|
|
webm: { video: webmVideo, audio: webmAudio },
|
|
};
|
|
fs.writeFileSync(path.join(evidenceDir, "verification.json"), `${JSON.stringify(report, null, 2)}\n`);
|
|
fs.writeFileSync(path.join(evidenceDir, "ffprobe.json"), `${JSON.stringify({ mp4: mp4Probe, webm: webmProbe }, null, 2)}\n`);
|
|
console.log(JSON.stringify({ ok: true, episode: episodeId, duration, checks: checks.length, cues: cueCount, stills: stills.length }));
|