diff --git a/skills-manifest.json b/skills-manifest.json index cffa723677..0e0ce555d0 100644 --- a/skills-manifest.json +++ b/skills-manifest.json @@ -18,8 +18,8 @@ "files": 13 }, "media-use": { - "hash": "50d6fb135e26b3d7", - "files": 133 + "hash": "211ae67ed751ede5", + "files": 134 }, "remotion-to-hyperframes": { "hash": "3a1dd4a513701ef6", diff --git a/skills/media-use/audio/scripts/lib/media-input.mjs b/skills/media-use/audio/scripts/lib/media-input.mjs index 8366b8750e..e1a44f5f8a 100644 --- a/skills/media-use/audio/scripts/lib/media-input.mjs +++ b/skills/media-use/audio/scripts/lib/media-input.mjs @@ -20,12 +20,18 @@ const INDIRECT_FORMATS = new Set([ "vapoursynth", ]); +// Opening a file runs its reader, and a script reader (avisynth, vapoursynth) runs the script, so even the +// probe names the readers it may use: audio and video containers that hold their own media. +export const SELF_CONTAINED_FORMATS = + "mov,matroska,mxf,avi,mpeg,mpegts,mpegvideo,flv,asf,ogg,wav,mp3,aac,flac,aiff"; +const PROBE_INPUT = ["-protocol_whitelist", "file", "-format_whitelist", SELF_CONTAINED_FORMATS]; + // The demuxer name for src, such as "mp3" or "mov,mp4,m4a,3gp,3g2,mj2"; null when it cannot tell. Some // Windows installs ship ffmpeg without ffprobe, so ffmpeg's input banner is the fallback. export function probeFormat(src) { const r = spawnSync( "ffprobe", - ["-v", "error", "-protocol_whitelist", "file", "-show_entries", "format=format_name"].concat([ + ["-v", "error", ...PROBE_INPUT, "-show_entries", "format=format_name"].concat([ "-of", "default=nw=1:nk=1", "--", @@ -34,7 +40,7 @@ export function probeFormat(src) { { encoding: "utf8" }, ); if (r.error?.code === "ENOENT") { - const banner = spawnSync("ffmpeg", ["-hide_banner", "-protocol_whitelist", "file", "-i", src], { + const banner = spawnSync("ffmpeg", ["-hide_banner", ...PROBE_INPUT, "-i", src], { encoding: "utf8", }); return /^Input #0, (.+?), from /m.exec(banner.stderr ?? "")?.[1] ?? null; diff --git a/skills/media-use/audio/scripts/media-input.test.mjs b/skills/media-use/audio/scripts/media-input.test.mjs new file mode 100644 index 0000000000..4a78fda8b0 --- /dev/null +++ b/skills/media-use/audio/scripts/media-input.test.mjs @@ -0,0 +1,144 @@ +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { delimiter, join } from "node:path"; +import { test } from "node:test"; +import { fileURLToPath } from "node:url"; +import { SELF_CONTAINED_FORMATS, probeFormat } from "./lib/media-input.mjs"; + +const scripts = fileURLToPath(new URL(".", import.meta.url)); +const hasFfmpeg = spawnSync("ffprobe", ["-version"]).status === 0; +const posix = process.platform !== "win32"; + +function sandbox(t) { + const dir = mkdtempSync(join(tmpdir(), "media-input-")); + t.after(() => rmSync(dir, { recursive: true, force: true })); + return dir; +} + +/** A stand-in for `name` that records its arguments, one per line, and fails. */ +function recorder(bin, name) { + mkdirSync(bin, { recursive: true }); + const log = join(bin, `${name}.args`); + writeFileSync( + join(bin, name), + `#!/bin/sh\nfor a in "$@"; do echo "$a"; done > "${log}"\nexit 1\n`, + { + mode: 0o755, + }, + ); + return () => readFileSync(log, "utf8").trim().split("\n"); +} + +function withPath(t, path) { + const saved = process.env.PATH; + process.env.PATH = path; + t.after(() => (process.env.PATH = saved)); +} + +// A script reader runs its script when the file is opened, so the reader list has to come before the file. +const assertListedBeforeFile = (args, file) => { + const at = args.indexOf("-format_whitelist"); + assert.ok(at >= 0, args.join(" ")); + assert.equal(args[at + 1], SELF_CONTAINED_FORMATS); + assert.ok(at < args.indexOf(file), args.join(" ")); +}; + +test( + "the probe names the readers it may use before ffprobe opens the file", + { skip: !posix }, + (t) => { + const dir = sandbox(t); + const args = recorder(join(dir, "bin"), "ffprobe"); + withPath(t, `${join(dir, "bin")}${delimiter}${process.env.PATH}`); + const file = join(dir, "clip.avs"); + writeFileSync(file, "Version()"); + assert.equal(probeFormat(file), null); + assertListedBeforeFile(args(), file); + }, +); + +test("the ffmpeg fallback names them too, when there is no ffprobe", { skip: !posix }, (t) => { + const dir = sandbox(t); + const args = recorder(join(dir, "bin"), "ffmpeg"); + withPath(t, join(dir, "bin")); + const file = join(dir, "clip.avs"); + writeFileSync(file, "Version()"); + assert.equal(probeFormat(file), null); + assertListedBeforeFile(args(), file); +}); + +test( + "ordinary audio and video still probe; scripts and lists named as media do not", + { skip: !hasFfmpeg }, + (t) => { + const dir = sandbox(t); + for (const name of ["a.wav", "a.mp3", "a.m4a", "a.mp4"]) { + const made = spawnSync("ffmpeg", [ + "-v", + "error", + "-f", + "lavfi", + "-i", + "sine=duration=1", + join(dir, name), + ]); + assert.equal(made.status, 0); + assert.ok(probeFormat(join(dir, name)), name); + } + writeFileSync(join(dir, "script.avs"), "Version()"); + writeFileSync(join(dir, "list.wav"), `ffconcat version 1.0\nfile '${join(dir, "a.wav")}'\n`); + writeFileSync( + join(dir, "play.mp3"), + `#EXTM3U\n#EXTINF:1,\n${join(dir, "a.wav")}\n#EXT-X-ENDLIST\n`, + ); + for (const name of ["script.avs", "list.wav", "play.mp3"]) { + assert.equal(probeFormat(join(dir, name)), null, name); + } + }, +); + +test("carve refuses a clip whose source is a script", { skip: !hasFfmpeg }, (t) => { + const dir = sandbox(t); + spawnSync("ffmpeg", [ + "-v", + "error", + "-f", + "lavfi", + "-i", + "sine=duration=2", + join(dir, "bed.wav"), + ]); + writeFileSync(join(dir, "narration.avs"), "Version()"); + writeFileSync( + join(dir, "index.html"), + '\n' + + '\n', + ); + const run = spawnSync(process.execPath, [join(scripts, "carve.mjs"), "--comp", "index.html"], { + cwd: dir, + encoding: "utf8", + }); + assert.equal(run.status, 1, run.stderr); + assert.match(run.stderr, /cannot read it as media/); +}); + +test("the recording path refuses a recording that is a script", { skip: !hasFfmpeg }, (t) => { + const dir = sandbox(t); + writeFileSync(join(dir, "take.avs"), "Version()"); + writeFileSync( + join(dir, "audio_request.json"), + JSON.stringify({ lines: [{ id: "01", audio: "take.avs", words: [] }] }), + ); + const run = spawnSync( + process.execPath, + [join(scripts, "audio.mjs"), "--hyperframes", dir, "--only", "tts"], + { + encoding: "utf8", + env: { ...process.env, HOME: dir }, + }, + ); + assert.match(run.stdout, /line 01: recording failed — omitted/); + assert.match(run.stdout, /cannot read/); +}); diff --git a/skills/media-use/audio/scripts/recording.test.mjs b/skills/media-use/audio/scripts/recording.test.mjs index 72eab091da..0ebe7911ba 100644 --- a/skills/media-use/audio/scripts/recording.test.mjs +++ b/skills/media-use/audio/scripts/recording.test.mjs @@ -199,7 +199,7 @@ for (const [label, name, list, reason] of [ "an HLS playlist", "take.m3u8", (media) => `#EXTM3U\n#EXT-X-TARGETDURATION:1\n#EXTINF:1,\n${media}\n#EXT-X-ENDLIST\n`, - /is a hls list that points at other files/, + /is a hls list that points at other files|cannot read/, ], [ "a concat list",