mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-16 14:27:26 +00:00
fix(scripts): the capture checker time limit covers every step
The limit is now one budget per video shared by ffprobe, ffmpeg and the comparison, checked on every frame pair so a large --window cannot run past it. A timed-out tool is killed with its whole process group, so a shell-wrapper ffmpeg leaves no orphan. --timeout is capped below the Node timer limit and inherited option names are rejected.
This commit is contained in:
@@ -9,6 +9,7 @@ import { fileURLToPath } from "node:url";
|
||||
|
||||
const WIDTH = 96;
|
||||
const DEFAULT_TIMEOUT_S = 120;
|
||||
const MAX_TIMEOUT_S = 86400;
|
||||
const USAGE =
|
||||
"usage: node scripts/check-capture-reversion.mjs [--crop=W:H:X:Y] [--window=N] [--change=D] [--same=D] [--timeout=S] <video> ...";
|
||||
|
||||
@@ -17,52 +18,50 @@ export function frameHeight(cw, ch) {
|
||||
return Math.max(2, 2 * Math.round((WIDTH * (ch / cw)) / 2));
|
||||
}
|
||||
|
||||
function readFrames(path, crop, height, timeoutMs) {
|
||||
/** Runs a tool to completion, killing its whole process group if it outlives timeoutMs. */
|
||||
function runTool(cmd, args, timeoutMs, label) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const scale = `scale=${WIDTH}:${height}:flags=area,format=gray`;
|
||||
const vf = crop ? `crop=${crop},${scale}` : scale;
|
||||
const child = spawn("ffmpeg", ["-v", "error", "-i", path, "-vf", vf, "-f", "rawvideo", "-"]);
|
||||
const child = spawn(cmd, args, { detached: process.platform !== "win32" });
|
||||
const chunks = [];
|
||||
let stderr = "";
|
||||
const timer = setTimeout(() => {
|
||||
child.kill();
|
||||
reject(new Error(`ffmpeg did not finish within ${timeoutMs / 1000}s for ${path}`));
|
||||
try {
|
||||
process.kill(process.platform === "win32" ? child.pid : -child.pid);
|
||||
} catch {}
|
||||
reject(new Error(`${cmd} did not finish within ${timeoutMs / 1000}s for ${label}`));
|
||||
}, timeoutMs);
|
||||
child.stdout.on("data", (c) => chunks.push(c));
|
||||
child.stderr.on("data", (c) => (stderr += c));
|
||||
child.on("error", reject);
|
||||
child.on("error", (error) => {
|
||||
clearTimeout(timer);
|
||||
reject(error);
|
||||
});
|
||||
child.on("close", (code) => {
|
||||
clearTimeout(timer);
|
||||
if (code !== 0) return reject(new Error(`ffmpeg exited ${code} for ${path}: ${stderr}`));
|
||||
resolve(Buffer.concat(chunks));
|
||||
if (code === 0) resolve(Buffer.concat(chunks));
|
||||
else reject(new Error(`${cmd} exited ${code} for ${label}: ${stderr}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Source video size, used to derive the output frame height when no crop is given.
|
||||
function probeSize(path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn("ffprobe", [
|
||||
"-v",
|
||||
"error",
|
||||
"-select_streams",
|
||||
"v:0",
|
||||
"-show_entries",
|
||||
"stream=width,height",
|
||||
"-of",
|
||||
"csv=p=0",
|
||||
path,
|
||||
]);
|
||||
let out = "";
|
||||
child.stdout.on("data", (c) => (out += c));
|
||||
child.on("error", reject);
|
||||
child.on("close", (code) => {
|
||||
const [w, h] = out.trim().split(",").map(Number);
|
||||
if (code !== 0 || !(w > 0) || !(h > 0))
|
||||
return reject(new Error(`ffprobe found no video size in ${path}`));
|
||||
resolve([w, h]);
|
||||
});
|
||||
});
|
||||
function readFrames(path, crop, height, timeoutMs) {
|
||||
const scale = `scale=${WIDTH}:${height}:flags=area,format=gray`;
|
||||
const vf = crop ? `crop=${crop},${scale}` : scale;
|
||||
return runTool(
|
||||
"ffmpeg",
|
||||
["-v", "error", "-i", path, "-vf", vf, "-f", "rawvideo", "-"],
|
||||
timeoutMs,
|
||||
path,
|
||||
);
|
||||
}
|
||||
|
||||
/** Source video size, used to derive the output frame height when no crop is given. */
|
||||
async function probeSize(path, timeoutMs) {
|
||||
const args = ["-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height"];
|
||||
const out = await runTool("ffprobe", [...args, "-of", "csv=p=0", path], timeoutMs, path);
|
||||
const [w, h] = out.toString().trim().split(",").map(Number);
|
||||
if (!(w > 0) || !(h > 0)) throw new Error(`ffprobe found no video size in ${path}`);
|
||||
return [w, h];
|
||||
}
|
||||
|
||||
function dist(buf, frameSize, a, b) {
|
||||
@@ -94,8 +93,8 @@ export function findReversions(buf, frameSize, { window, change, same }, deadlin
|
||||
const count = Math.floor(buf.length / frameSize);
|
||||
const byPeak = new Map();
|
||||
for (let i = 0; i < count; i++) {
|
||||
if (Date.now() > deadline) throw new Error("comparison did not finish within the time limit");
|
||||
for (let k = i + 2; k <= Math.min(count - 1, i + window); k++) {
|
||||
if (Date.now() > deadline) throw new Error("comparison did not finish within the time limit");
|
||||
if (dist(buf, frameSize, i, k) > same) continue;
|
||||
const { peak, delta } = peakBetween(buf, frameSize, i, k, change);
|
||||
const known = byPeak.get(peak);
|
||||
@@ -126,9 +125,9 @@ export function parseArgs(argv) {
|
||||
if (!/^[1-9]\d*:[1-9]\d*:\d+:\d+$/.test(m[2]))
|
||||
throw new Error(`--crop needs W:H:X:Y, got ${m[2]}`);
|
||||
options.crop = m[2];
|
||||
} else if (m[1] in options) {
|
||||
} else if (Object.hasOwn(options, m[1])) {
|
||||
const n = Number(m[2]);
|
||||
if (m[2] === "" || !Number.isFinite(n) || n < 0)
|
||||
if (m[2] === "" || !Number.isFinite(n) || n < 0 || (m[1] === "timeout" && n > MAX_TIMEOUT_S))
|
||||
throw new Error(`--${m[1]} needs a number, got ${m[2]}`);
|
||||
options[m[1]] = n;
|
||||
} else {
|
||||
@@ -140,11 +139,14 @@ export function parseArgs(argv) {
|
||||
|
||||
// fallow-ignore-next-line complexity
|
||||
async function checkVideo(path, options) {
|
||||
const [cw, ch] = options.crop ? options.crop.split(":").map(Number) : await probeSize(path);
|
||||
const deadline = Date.now() + options.timeout * 1000;
|
||||
const left = () => Math.max(1, deadline - Date.now());
|
||||
const [cw, ch] = options.crop
|
||||
? options.crop.split(":").map(Number)
|
||||
: await probeSize(path, left());
|
||||
const height = frameHeight(cw, ch);
|
||||
const frameSize = WIDTH * height;
|
||||
const deadline = Date.now() + options.timeout * 1000;
|
||||
const buf = await readFrames(path, options.crop, height, options.timeout * 1000);
|
||||
const buf = await readFrames(path, options.crop, height, left());
|
||||
const frames = Math.floor(buf.length / frameSize);
|
||||
if (frames === 0) throw new Error(`no frames decoded from ${path}`);
|
||||
const hits = findReversions(buf, frameSize, options, deadline);
|
||||
@@ -182,4 +184,5 @@ async function main(argv) {
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
if (realpathSync(process.argv[1]) === fileURLToPath(import.meta.url)) main(process.argv.slice(2));
|
||||
const entry = process.argv[1] && realpathSync(process.argv[1]);
|
||||
if (entry === fileURLToPath(import.meta.url)) main(process.argv.slice(2));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { strict as assert } from "node:assert";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { mkdtempSync, symlinkSync } from "node:fs";
|
||||
import { chmodSync, mkdtempSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -158,3 +158,51 @@ test("a comparison past its deadline throws", () => {
|
||||
test("--timeout is parsed as seconds", () => {
|
||||
assert.equal(parseArgs(["--timeout=30", "v.webm"]).options.timeout, 30);
|
||||
});
|
||||
|
||||
test("a comparison that outlives --timeout stops at the deadline, not after the frame", () => {
|
||||
const big = Buffer.alloc(SIZE * 400, 7);
|
||||
const opts = { window: 400, change: 0, same: 255 };
|
||||
const started = Date.now();
|
||||
assert.throws(() => findReversions(big, SIZE, opts, started + 20), /time limit/);
|
||||
assert.ok(Date.now() - started < 2000);
|
||||
});
|
||||
|
||||
test("--timeout stops a hung ffmpeg or ffprobe wrapper, children included, with exit 2", () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "reversion-"));
|
||||
for (const tool of ["ffmpeg", "ffprobe"]) {
|
||||
writeFileSync(join(dir, tool), "#!/bin/sh\nsleep 37.31\n");
|
||||
chmodSync(join(dir, tool), 0o755);
|
||||
}
|
||||
const env = { ...process.env, PATH: `${dir}:${process.env.PATH}` };
|
||||
for (const extra of [["--crop=32:32:0:0"], []]) {
|
||||
const started = Date.now();
|
||||
const r = spawnSync(process.execPath, [SCRIPT, "--timeout=1", ...extra, "any.mp4"], {
|
||||
encoding: "utf8",
|
||||
env,
|
||||
});
|
||||
assert.equal(r.status, 2);
|
||||
assert.match(r.stderr, /did not finish within 1s/);
|
||||
assert.ok(Date.now() - started < 10000);
|
||||
}
|
||||
const orphans = spawnSync("pgrep", ["-f", "sleep 37.31"], { encoding: "utf8" });
|
||||
assert.notEqual(orphans.status, 0, `orphaned children: ${orphans.stdout}`);
|
||||
});
|
||||
|
||||
test("--timeout above the timer limit and inherited option names are rejected", () => {
|
||||
assert.throws(() => parseArgs(["--timeout=1e10", "v.mp4"]));
|
||||
assert.throws(() => parseArgs(["--constructor=5", "v.mp4"]));
|
||||
});
|
||||
|
||||
test(
|
||||
"a flagged video is not hidden when a later one is missing or an earlier one is",
|
||||
{ skip: !hasFfmpeg },
|
||||
() => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "reversion-"));
|
||||
const blink = makeClip(
|
||||
dir,
|
||||
"blink.mp4",
|
||||
"color=c=black:s=64x64:r=10,drawbox=x=0:y=0:w=64:h=64:c=white:t=fill:enable='between(n,2,2)'",
|
||||
);
|
||||
assert.equal(run(SCRIPT, join(dir, "missing.mp4"), blink).status, 2);
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user