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
+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 = [