NIG integration: one-call road (film.mjs), auto-discovery of ffmpeg/ffprobe/browser, setup mode
This commit is contained in:
@@ -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));
|
||||
}
|
||||
Reference in New Issue
Block a user