Files
hyperframes/packages/engine/src/utils/psnr.ts
T
9d6ca45857 fix: unbreak the regression suite on Windows (PSNR filtergraph escaping + console window popups) (#3839)
* fix: hide console windows spawned by ffmpeg and nvidia-smi on Windows

Node's `windowsHide` defaults to false, so every child process that is a
console application pops a window on the user's desktop. During a render
or a regression run that is a stream of black windows stealing focus.

The repo already sets `windowsHide: true` across cli, studio-server and
most of engine; these were the remaining gaps, all on paths that run
during a normal render:

- core/mediaGradeAnalyzer: ffprobe + ffmpeg, once per analyzed media file
- engine/browserManager: `nvidia-smi` VRAM probe. This one goes through a
  shell (`execSync`), so it flashes a cmd.exe window even on machines with
  no NVIDIA GPU, where the command exists only to fail
- producer: the regression harness ffmpeg runner, PSNR/audio comparison
  helpers, fixture synthesis, and the ffprobe call in audioPadTrim

No behavior change on POSIX, where the flag is ignored.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(producer): escape PSNR stats_file for ffmpeg's two-pass filtergraph parse

An ffmpeg filtergraph argument is unescaped twice: once when the graph is
split into filters and their options, then again when the option value is
read. `psnrAtFrames` escaped `\` and `:` a single time, which only survives
the first pass.

On POSIX that is invisible, because mkdtemp under tmpdir() produces neither
character. On Windows the stats path is always `C:\Users\...`, so it reaches
the option parser with a bare `:`, which starts a new option and fails the
whole graph:

    [AVFilterGraph] No option name near '\Users\...\psnr.log'
    Error parsing filterchain '[rv][gv]psnr=...:stats_file=C\:\Users\...'

Every suite that got as far as quality validation died there, so a Windows
regression run reported 0 passes with only 2 genuine compilation failures.
Escaping for both passes fixes it and stays a no-op on POSIX.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(producer): pin PSNR stats path escaping with a colon and backslash path

* fix(producer): keep the PSNR stats path out of the filtergraph

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Miguel Ángel <miguel.sierra@heygen.com>
2026-10-02 04:14:36 -07:00

46 lines
2.0 KiB
TypeScript

import { execFile } from "node:child_process";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { promisify } from "node:util";
import { getFfmpegBinary } from "./ffmpegBinaries.js";
/**
* PSNR (average, dB) between two same-dimension encoded images via ffmpeg.
* Infinity means bit-identical pixels. Single source of truth for every
* drawElement self-verify comparison (streaming drain + parallel disk path).
*/
export async function psnrDb(a: Buffer, b: Buffer): Promise<number> {
// promisify(execFile) lazily, not at module load: this module is in the
// engine's parallel-capture import chain, and a top-level call to a builtin
// crashes any downstream test that partially mocks node:child_process
// without an execFile export (vitest surfaces it as a load-time error).
const execFileP = promisify(execFile);
const dir = await mkdtemp(join(tmpdir(), "hf-de-verify-"));
try {
const pa = join(dir, "a.jpg");
const pb = join(dir, "b.jpg");
await Promise.all([writeFile(pa, a), writeFile(pb, b)]);
const { stderr } = await execFileP(
getFfmpegBinary(),
["-hide_banner", "-i", pa, "-i", pb, "-lavfi", "psnr", "-f", "null", "-"],
{ maxBuffer: 4 * 1024 * 1024, windowsHide: true },
);
const m = /average:(inf|[\d.]+)/.exec(stderr);
if (!m) throw new Error(`psnr parse failed: ${stderr.slice(-300)}`);
return m[1] === "inf" ? Infinity : Number(m[1]);
} finally {
await rm(dir, { recursive: true, force: true }).catch(() => {});
}
}
/**
* The drawElement self-verify PSNR floor (dB). HF_DE_VERIFY_MIN_DB overrides,
* clamped to [10, 60]; out-of-range or unset falls back to 32 — the threshold
* every prior eval used to separate real compositor damage from encoder noise.
*/
export function resolveDeVerifyMinDb(): number {
const raw = Number(process.env.HF_DE_VERIFY_MIN_DB ?? "32");
return Number.isFinite(raw) && raw >= 10 && raw <= 60 ? raw : 32;
}