NIG integration: one-call road (film.mjs), auto-discovery of ffmpeg/ffprobe/browser, setup mode

This commit is contained in:
NIG
2026-11-10 17:18:58 +03:30
parent 5d8f1c6efe
commit 331304fd5c
5 changed files with 310 additions and 5 deletions
+9
View File
@@ -44,6 +44,15 @@ node sims/build.mjs piranha-film
node sims/build.mjs wasp-film
```
## The whole road in one call
```
node film.mjs --setup # once: installs deps, a browser and ffmpeg where missing
node film.mjs wasp-film --gif # build -> score -> capture -> mux -> beat sheet -> waveform -> gif
```
`film.mjs` prints the GPU it captured on, the score's loudness, and every artifact path. `--quick` renders a fast draft; flags mirror `sims/capture.mjs`. If the Playwright Chromium download is unavailable, it captures in the installed Edge or Chrome (same WebGL) and finds ffmpeg even when it is only in winget's Links dir.
## How it works
1. **One reference picture** sets the look. Claude reads it and rebuilds it; it never imports it.
+24
View File
@@ -15,7 +15,14 @@ One picture in, one object out: built from code, opened part by part, filmed. Th
cd "$SKILL_DIR" && npm install && npx playwright install chromium
```
Or let one call install whatever is missing (deps, a browser, ffmpeg):
```
node "$SKILL_DIR/film.mjs" --setup
```
The pages need nothing else. The video needs `ffmpeg` on PATH and the score needs Python 3 (standard library only). Set `FFMPEG`, `PYTHON` or `CHROMIUM` to point at other binaries.
`paths.mjs` resolves both tools on its own when they are off PATH (PATH, winget's Links dir, `C:\ffmpeg\bin`, `<skill>/bin`), and if the downloaded Chromium is missing it captures in the installed Edge or Chrome (same WebGL; the captures run on the discrete GPU either way).
## The 3 examples
@@ -104,6 +111,23 @@ runSim(DEFAULTS, async ({cfg, canvas, capture}) => {
- A short version may run its scene faster than the film: `{"duration": 11.3, "film": 10.0}` plays 11.3 s of scene in 10 s.
- Frame 0 is the thumbnail. It must already show the object big, front 3/4, moving.
## The road in one call
`film.mjs` runs the whole road for one entry and leaves the review artifacts behind:
```
node "$SKILL_DIR/film.mjs" piranha-film # build -> score -> capture -> mux -> beat sheet -> waveform
node "$SKILL_DIR/film.mjs" wasp-film --gif # + a palette gif for the docs
node "$SKILL_DIR/film.mjs" ./my-object --score my/score.py --size 1080x1350 --dpr 1
node "$SKILL_DIR/film.mjs" piranha-film --quick # draft: 720p, dpr 1, crf 20 (fast iteration)
```
It prints the GPU it captured on, the score's LUFS/true peak, and the path of every artifact:
`cutaway-out/<name>.mp4` (final, with the score), `<name>-sheet.png` (one tile per beat — read it before
reporting done), `<name>_wave.png` (every hit on a beat line), `<name>-score.wav`, `<name>.gif` with `--gif`.
Flags: `--size --dpr --fps --crf --from --to --cols --out-dir --name --score --no-score --no-sheet --no-wave --no-mux`.
A demo whose scene runs faster than its film (`{"film": T}` in the timeline) gets its score stretched to the film clock.
## Commands
```
+204
View File
@@ -0,0 +1,204 @@
#!/usr/bin/env node
// The whole road in one call: build -> score -> capture -> mux -> review sheet -> waveform.
//
// node film.mjs --setup once: deps, a browser, ffmpeg (installs what is missing)
// node film.mjs wasp-film -> cutaway-out/wasp-film.mp4 with the synced score
// -> cutaway-out/wasp-film-sheet.png (one tile per beat, read it)
// -> cutaway-out/wasp-film-wave.png + loudness line
// node film.mjs wasp-film --gif also a palette GIF next to the mp4 (for docs)
// node film.mjs ./my-object --score my/score.py --size 1080x1350 --dpr 1
//
// Flags: --size 1600x900 --dpr 1.2 --fps N --crf 16 --from A --to B --cols 4
// --out-dir DIR (default ./cutaway-out) --no-score --no-sheet --no-wave --quick (draft speed)
// --setup --score f.py --name LABEL
//
// The score is rendered from the object's own score.py against the entry's timeline.json, so audio cannot drift.
// A demo whose scene runs faster than its film ({"film": T} in the timeline) gets its score stretched to match.
import {spawnSync} from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {CHROMIUM, DEPS, FFMPEG, FFPROBE, PYTHON, SKILL_DIR} from './paths.mjs';
const HERE = path.dirname(fileURLToPath(import.meta.url));
const NODE = process.execPath;
const argv = process.argv.slice(2);
const BOOLS = new Set(['setup', 'gif', 'quick', 'no-score', 'no-sheet', 'no-wave', 'no-mux', 'help']);
const flag = (k) => argv.includes(`--${k}`);
const opt = (k, d) => {
const i = argv.indexOf(`--${k}`);
return i === -1 || i + 1 >= argv.length ? d : argv[i + 1];
};
// The entry is the first bare word that is not the value of a --flag.
const entryArg = argv.find((a, i) => !a.startsWith('--') && (i === 0 || !argv[i - 1].startsWith('--') || BOOLS.has(argv[i - 1].slice(2))));
if (flag('help') || (!entryArg && !flag('setup'))) {
console.log(
'usage: node film.mjs <entry|./folder> [--size 1600x900] [--dpr 1.2] [--fps N] [--crf 16]\n' +
' [--out-dir cutaway-out] [--score f.py] [--gif] [--quick] [--no-score] [--no-sheet]\n' +
' [--no-wave] [--no-mux] [--from A] [--to B]\n' +
' node film.mjs --setup',
);
process.exit(entryArg ? 0 : 1);
}
const run = (cmd, args, opts = {}) => {
const r = spawnSync(cmd, args, {stdio: 'inherit', cwd: HERE, ...opts});
if (r.error) {
console.error(`cannot run ${cmd}: ${r.error.message}`);
process.exit(1);
}
if (r.status !== 0) {
console.error(`failed (${r.status}): ${cmd} ${args.join(' ')}`);
process.exit(r.status ?? 1);
}
};
const runQuiet = (cmd, args, opts = {}) => spawnSync(cmd, args, {encoding: 'utf8', cwd: HERE, ...opts});
// ---------- --setup: install whatever the road needs, idempotent ----------
const have = (p) => {
try {
return fs.existsSync(p);
} catch {
return false;
}
};
if (flag('setup')) {
console.log(`ffmpeg: ${have(FFMPEG) ? FFMPEG : 'MISSING'} ffprobe: ${have(FFPROBE) ? FFPROBE : 'MISSING'}`);
console.log(`browser: ${CHROMIUM || 'playwright bundled (none found yet)'}`);
let touched = false;
if (!have(DEPS)) {
console.log('installing npm deps…');
run(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['install', '--no-audit', '--no-fund'], {shell: false});
touched = true;
}
if (!have(CHROMIUM || '__no_browser__')) {
console.log('installing a browser…');
run(process.platform === 'win32' ? 'npx.cmd' : 'npx', ['playwright', 'install', 'chromium'], {shell: false});
touched = true;
}
if (!have(FFMPEG)) {
console.log('ffmpeg not found: installing Gyan.FFmpeg via winget (or drop an ffmpeg build into ' + path.join(HERE, 'bin') + ')');
const w = runQuiet('winget', ['install', '-e', '--id', 'Gyan.FFmpeg', '--accept-source-agreements', '--accept-package-agreements', '--disable-interactivity'], {shell: true});
if (w.status !== 0) console.log('winget said:\n' + (w.stdout || '') + (w.stderr || ''));
touched = true;
}
const py = runQuiet(PYTHON, ['--version']);
if (py.status !== 0) console.log('WARN python missing: the procedural score needs Python 3 (stdlib only).');
console.log(touched ? 'setup done' : 'nothing to do: the road is ready');
if (!entryArg) process.exit(0);
}
// ---------- resolve the entry ----------
const simDir = fs.existsSync(path.join(HERE, 'sims', entryArg, 'main.js'))
? path.join(HERE, 'sims', entryArg)
: path.resolve(entryArg);
if (!fs.existsSync(path.join(simDir, 'main.js'))) {
console.error(`no main.js in ${simDir}`);
process.exit(1);
}
const label = opt('name', path.basename(simDir));
const outDir = path.resolve(opt('out-dir', 'cutaway-out'));
fs.mkdirSync(path.join(outDir, 'dist'), {recursive: true});
const env = {...process.env, CUTAWAY_OUT: outDir};
const tlPath = path.join(simDir, 'timeline.json');
const tl = fs.existsSync(tlPath) ? JSON.parse(fs.readFileSync(tlPath, 'utf8')) : null;
// ---------- 1. build ----------
console.log(`\n[1/5] build ${label}`);
run(NODE, [path.join(HERE, 'sims', 'build.mjs'), simDir], {env});
// ---------- 2. score (procedural, from the same timeline) ----------
const scoreCandidates = opt('score')
? [path.resolve(opt('score'))]
: [
path.join(simDir, 'score.py'),
...['-film', '-demo'].map((suf) =>
label.endsWith(suf) ? path.join(HERE, 'sims', label.slice(0, -suf.length) + '-shared', label.endsWith('-demo') && fs.existsSync(path.join(HERE, 'sims', label.slice(0, -suf.length) + '-shared', 'score_demo.py')) ? 'score_demo.py' : 'score.py') : null,
),
].filter(Boolean);
const scorePy = scoreCandidates.find(have);
const wav = path.join(outDir, `${label}-score.wav`);
const video = path.join(outDir, `${label}.mp4`);
console.log('\n[2/5] score');
if (!flag('no-score') && scorePy) {
const scoreEnv = {...env, OUT: wav};
if (tl && fs.existsSync(tlPath)) scoreEnv.TL = tlPath;
run(PYTHON, ['-I', path.join(HERE, 'run_sfx.py'), scorePy], {env: scoreEnv});
if (tl && tl.film && tl.film !== tl.duration) {
let f = tl.duration / tl.film;
const chain = [];
while (f > 2) {
chain.push('atempo=2');
f /= 2;
}
while (f < 0.5) {
chain.push('atempo=0.5');
f *= 2;
}
chain.push(`atempo=${f.toFixed(6)}`);
run(FFMPEG, ['-loglevel', 'error', '-y', '-i', wav, '-af', chain.join(','), wav.replace(/\.wav$/, '-film.wav')]);
fs.rmSync(wav);
fs.renameSync(wav.replace(/\.wav$/, '-film.wav'), wav);
console.log(`stretched the score to the film clock (x${(tl.duration / tl.film).toFixed(3)})`);
}
} else {
console.log(scorePy ? 'skipped (--no-score)' : 'no score.py for this entry: silent film');
}
// ---------- 3. capture ----------
const capArgs = [
path.join(HERE, 'sims', 'capture.mjs'),
label,
'--size',
flag('quick') ? opt('size', '1280x720') : opt('size', '1600x900'),
'--dpr',
flag('quick') ? opt('dpr', '1') : opt('dpr', '1.2'),
'--crf',
flag('quick') ? opt('crf', '20') : opt('crf', '16'),
];
if (flag('fps')) capArgs.push('--fps', opt('fps', '60'));
if (flag('from')) capArgs.push('--from', opt('from', '0'));
if (flag('to')) capArgs.push('--to', opt('to', String(tl?.duration ?? 20)));
console.log('\n[3/5] capture (prints the GPU it got: an NVIDIA/AMD line = real GPU, "swiftshader" = software)');
const silent = have(wav) && !flag('no-mux') ? path.join(outDir, `${label}-silent.mp4`) : video;
run(NODE, [...capArgs, '--out', silent], {env});
// ---------- 4. mux ----------
console.log('\n[4/5] mux');
if (silent !== video) {
run(FFMPEG, ['-loglevel', 'error', '-y', '-i', silent, '-i', wav, '-map', '0:v', '-map', '1:a', '-c:v', 'copy', '-c:a', 'aac', '-b:a', '256k', '-shortest', '-movflags', '+faststart', video]);
fs.rmSync(silent);
console.log(`muxed the score (${path.basename(wav)})`);
} else {
console.log(have(wav) ? 'kept video only (--no-mux)' : 'no score: silent film');
}
// ---------- 5. review sheet + waveform + optional gif ----------
console.log('\n[5/5] review');
const sheet = path.join(outDir, `${label}-sheet.png`);
if (!flag('no-sheet') && tl) {
const sheetSize = flag('quick') ? opt('size', '1280x720') : opt('size', '1600x900');
const sheetArgs = [
path.join(HERE, 'sims', 'capture.mjs'), label,
'--size', sheetSize, '--dpr', '1',
'--stills', 'beats', '--cols', opt('cols', '4'), '--timeline', tlPath, '--out', sheet,
];
if (flag('fps')) sheetArgs.push('--fps', opt('fps', '60'));
run(NODE, sheetArgs, {env});
}
if (!flag('no-wave') && have(FFPROBE) && have(FFMPEG)) {
run(NODE, [path.join(HERE, 'wave.mjs'), video, '--timeline', tlPath], {env});
}
if (flag('gif')) {
const gif = video.replace(/\.mp4$/, '.gif');
const vf = 'fps=12,scale=' + (flag('quick') ? 480 : 540) + ':-1:flags=lanczos,split[s0][s1];[s0]palettegen=stats_mode=diff[p];[s1][p]paletteuse=dither=bayer:bayer_scale=5';
run(FFMPEG, ['-loglevel', 'error', '-y', '-i', video, '-filter_complex', vf, '-loop', '0', gif]);
console.log(`gif: ${gif}`);
}
const kb = (p) => (fs.existsSync(p) ? ` ${(fs.statSync(p).size / 1024).toFixed(0)} KB` : '');
console.log('\nartifacts:');
for (const p of [video, sheet, path.join(outDir, `${label}_wave.png`), video.replace(/\.mp4$/, '.gif'), wav]) {
if (have(p)) console.log(' ' + p + kb(p));
}
+5 -1
View File
@@ -6,7 +6,11 @@
"license": "MIT",
"scripts": {
"setup": "npm install && npx playwright install chromium",
"build": "node sims/build.mjs"
"build": "node sims/build.mjs",
"film": "node film.mjs",
"wasp": "node film.mjs wasp-film --gif",
"piranha": "node film.mjs piranha-film --gif",
"tarantula": "node film.mjs tarantula-film --gif"
},
"dependencies": {
"@fontsource-variable/inter": "5.3.0",
+68 -4
View File
@@ -4,17 +4,81 @@
import {createRequire} from 'node:module';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {spawnSync} from 'node:child_process';
import {existsSync, readdirSync} from 'node:fs';
const ROOT = path.dirname(fileURLToPath(import.meta.url));
export const FFMPEG = process.env.FFMPEG || 'ffmpeg';
export const FFPROBE = process.env.FFPROBE || 'ffprobe';
// Resolve a tool once: $FFMPEG etc, then PATH (where.exe), then winget's Links dir,
// then common install targets, then <skill>/bin (what `node film.mjs --setup` fills in).
const findTool = (name, extra = []) => {
if (process.env[name]) return process.env[name];
const bin = name.toLowerCase();
try {
const w = spawnSync('where.exe', [bin], {encoding: 'utf8'});
if (w.status === 0) {
const hit = w.stdout.split(/\r?\n/).map((s) => s.trim()).filter(Boolean).find((p) => existsSync(p));
if (hit) return hit;
}
} catch {}
const la = process.env.LOCALAPPDATA;
const exe = process.platform === 'win32' ? bin + '.exe' : bin;
const cands = [
la && path.join(la, 'Microsoft', 'WinGet', 'Links', exe),
...extra.map((d) => path.join(d, exe)),
path.join(ROOT, 'bin', exe),
].filter(Boolean);
for (const c of cands) if (existsSync(c)) return c;
return bin;
};
export const FFMPEG = findTool('FFMPEG', ['C:\\ffmpeg\\bin']);
export const FFPROBE = findTool('FFPROBE', ['C:\\ffmpeg\\bin']);
export const PYTHON = process.env.PYTHON || (process.platform === 'win32' ? 'python' : 'python3');
export const SKILL_DIR = ROOT;
// three, esbuild, fonts and playwright: `npm install` in this folder puts them here.
export const DEPS = process.env.CUTAWAY_DEPS || path.join(ROOT, 'node_modules');
// Unset: playwright uses the Chromium that `npx playwright install chromium` downloaded.
export const CHROMIUM = process.env.CHROMIUM || undefined;
// Browser: $CHROMIUM, else playwright's downloaded Chromium if installed, else the system Edge/Chrome.
// Both run the same WebGL (the capture verified ANGLE/D3D11 on the discrete GPU through msedge), so a stalled
// `npx playwright install chromium` never blocks a render. Set CHROMIUM= to force playwright's bundled default.
const findBrowser = () => {
if (process.env.CHROMIUM !== undefined) return process.env.CHROMIUM || undefined;
const dir =
process.platform === 'win32'
? path.join(process.env.LOCALAPPDATA || '', 'ms-playwright')
: process.platform === 'darwin'
? path.join(process.env.HOME || '', 'Library/Caches/ms-playwright')
: path.join(process.env.HOME || '', '.cache/ms-playwright');
try {
for (const d of readdirSync(dir).sort().reverse()) {
if (!/^chromium-/.test(d)) continue;
for (const sub of process.platform === 'win32'
? ['chrome-win/chrome.exe', 'chrome-win64/chrome.exe']
: process.platform === 'darwin'
? ['Chromium.app/Contents/MacOS/Chromium', 'chrome-mac/Chromium.app/Contents/MacOS/Chromium']
: ['chrome-linux/chrome', 'chrome-linux64/chrome']) {
const p = path.join(dir, d, sub);
if (existsSync(p)) return p;
}
}
} catch {}
const pf = process.env['PROGRAMFILES(X86)'] || 'C:\\Program Files (x86)';
const pf64 = process.env['PROGRAMFILES'] || 'C:\\Program Files';
for (const p of [
process.platform === 'win32' && path.join(pf, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
process.platform === 'win32' && path.join(pf64, 'Microsoft', 'Edge', 'Application', 'msedge.exe'),
path.join(pf64, 'Google', 'Chrome', 'Application', 'chrome.exe'),
path.join(pf, 'Google', 'Chrome', 'Application', 'chrome.exe'),
process.platform === 'darwin' && '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
process.platform === 'darwin' && '/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
process.platform === 'linux' && '/usr/bin/google-chrome',
process.platform === 'linux' && '/usr/bin/microsoft-edge',
process.platform === 'linux' && '/usr/bin/chromium',
]) if (p && existsSync(p)) return p;
return undefined;
};
export const CHROMIUM = findBrowser();
// Real GPU WebGL instead of a software rasteriser. On Windows d3d11 plus --force_high_performance_gpu keeps ANGLE off
// an integrated GPU when a discrete one exists. capture.mjs prints the GPU it got on every run.
export const CHROMIUM_ARGS = [