SPB Git

Repository Public

make-test-capsule.ts 5.4 KB 77 lines typescript

1/**2 * pnpm media:test-capsule3 * Generates the synthetic *technical test* capsule used by the preview mode and the end-to-end tests:4 * a test pattern with a tone, encoded exactly like a real capsule would be (two HLS renditions, MP45 * fallback, poster, WebVTT captions, timestamped transcript, chapters). It is not course content and6 * every frame, caption and label says so. Output: $ATLAS_DATA_DIR/media/S01/test-technique/ (outside git).7 */8import { execFile } from "node:child_process";9import { mkdir, readdir, rm, stat, writeFile } from "node:fs/promises";10import { join, resolve } from "node:path";11import { promisify } from "node:util";12import sharp from "sharp";13import { toVtt, type Cue } from "../src/modules/learning-engine/vtt";1415const run = promisify(execFile);16const DURATION = 120, CHAPTER = 30, FREQS = [440, 550, 660, 770];17const out = join(resolve(process.env.ATLAS_DATA_DIR ?? "./data"), "media", "S01", "test-technique");1819await rm(out, { recursive: true, force: true });20await mkdir(out, { recursive: true });2122// Label burnt into every frame (drawn with sharp: the local ffmpeg has no drawtext filter).23const label = join(out, "label.png");24await sharp(Buffer.from(`<svg xmlns="http://www.w3.org/2000/svg" width="1280" height="720">25  <rect x="0" y="0" width="1280" height="92" fill="#0B1426" fill-opacity="0.92"/>26  <text x="40" y="44" font-family="Helvetica, Arial, sans-serif" font-size="30" font-weight="700" fill="#F5F1E6">VIDÉO DE TEST TECHNIQUE</text>27  <text x="40" y="76" font-family="Helvetica, Arial, sans-serif" font-size="20" fill="#AEB9CC">Mire et tonalité générées localement — ce n’est pas un contenu du cours ATLAS IA.</text>28</svg>`)).png().toFile(label);2930const video = `testsrc=size=1280x720:rate=25:duration=${DURATION}`;31const audio = `aevalsrc='0.15*sin(2*PI*t*(${FREQS[0]}+${FREQS[1]! - FREQS[0]!}*floor(t/${CHAPTER})))':s=44100:d=${DURATION}`;32const x264 = (maxrate: number) => ["-c:v", "libx264", "-preset", "veryfast", "-profile:v", "main", "-crf", "30", "-maxrate", `${maxrate}k`, "-bufsize", `${maxrate * 2}k`, "-g", "100", "-keyint_min", "100", "-sc_threshold", "0", "-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "64k", "-ac", "1"];33const hls = (name: string) => ["-f", "hls", "-hls_time", "4", "-hls_playlist_type", "vod", "-hls_segment_filename", join(out, name, "seg_%03d.ts"), join(out, name, "index.m3u8")];3435await mkdir(join(out, "v720")); await mkdir(join(out, "v360"));36await run("ffmpeg", [37  "-hide_banner", "-loglevel", "error", "-y",38  "-f", "lavfi", "-i", video, "-f", "lavfi", "-i", audio, "-i", label,39  "-filter_complex", "[0:v][2:v]overlay=0:0,split=3[hd][sd0][mp0];[sd0]scale=640:360[sd];[mp0]scale=640:360[mp]",40  "-map", "[hd]", "-map", "1:a", ...x264(900), ...hls("v720"),41  "-map", "[sd]", "-map", "1:a", ...x264(400), ...hls("v360"),42  "-map", "[mp]", "-map", "1:a", ...x264(400), "-movflags", "+faststart", join(out, "fallback.mp4"),43], { maxBuffer: 1 << 24 });44await run("ffmpeg", ["-hide_banner", "-loglevel", "error", "-y", "-ss", "3", "-i", join(out, "fallback.mp4"), "-frames:v", "1", "-vf", "scale=1280:720", "-q:v", "4", join(out, "poster.jpg")]);45await rm(label);4647// Master playlist. BANDWIDTH is the measured peak of each rendition, not a guess.48const peak = async (name: string) => {49  const files = (await readdir(join(out, name))).filter((f) => f.endsWith(".ts"));50  const sizes = await Promise.all(files.map(async (f) => (await stat(join(out, name, f))).size));51  return Math.ceil((Math.max(...sizes) * 8) / 4 / 1000) * 1000;52};53await writeFile(join(out, "master.m3u8"), [54  "#EXTM3U", "#EXT-X-VERSION:3",55  `#EXT-X-STREAM-INF:BANDWIDTH=${await peak("v360")},RESOLUTION=640x360,CODECS="avc1.4d401e,mp4a.40.2"`, "v360/index.m3u8",56  `#EXT-X-STREAM-INF:BANDWIDTH=${await peak("v720")},RESOLUTION=1280x720,CODECS="avc1.4d401f,mp4a.40.2"`, "v720/index.m3u8", "",57].join("\n"));5859// Captions and transcript: one cue every five seconds, true to what is on screen and in the audio.60const cues: Cue[] = [];61for (let t = 0; t < DURATION; t += 5) {62  const chapter = Math.floor(t / CHAPTER), n = t / 5 + 1;63  let text: string;64  if (t === 0) text = "Ceci est une vidéo de test technique : une mire et une tonalité.";65  else if (t === 5) text = "Elle ne fait pas partie du cours ATLAS IA et ne contient aucun enseignement.";66  else if (t % CHAPTER === 0) text = `Changement de chapitre : la tonalité passe à ${FREQS[chapter]} hertz.`;67  else if (t === DURATION - 5) text = "Fin de la vidéo de test technique.";68  else text = `Repère de test numéro ${n} : tonalité de ${FREQS[chapter]} hertz, de ${t} à ${t + 5} secondes.`;69  cues.push({ start: t, end: t + 5, text });70}71await writeFile(join(out, "captions.fr.vtt"), toVtt(cues, "ATLAS IA — vidéo de test technique générée localement. Ce n’est pas un contenu du cours."));72await writeFile(join(out, "transcript.json"), `${JSON.stringify({ lang: "fr-CA", segments: cues }, null, 2)}\n`);73await writeFile(join(out, "chapters.json"), `${JSON.stringify(FREQS.map((f, i) => ({ start: i * CHAPTER, title: i === 0 ? `Mire et tonalité de ${f} Hz` : `Tonalité de ${f} Hz` })), null, 2)}\n`);7475const total = async (dir: string): Promise<number> => (await Promise.all((await readdir(dir, { withFileTypes: true })).map(async (e) => (e.isDirectory() ? total(join(dir, e.name)) : (await stat(join(dir, e.name))).size)))).reduce((a, b) => a + b, 0);76console.log(`Capsule de test générée dans ${out} (${(await total(out) / 1e6).toFixed(1)} Mo, ${DURATION} s).`);77
Files main
Menu

Theme