diff --git a/packages/cli/src/measure/face.test.ts b/packages/cli/src/measure/face.test.ts index 39ea37f792..630488493e 100644 --- a/packages/cli/src/measure/face.test.ts +++ b/packages/cli/src/measure/face.test.ts @@ -1,13 +1,28 @@ import { execFileSync } from "node:child_process"; -import { cpSync, existsSync, mkdtempSync, rmSync } from "node:fs"; +import { cpSync, existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir, userInfo } from "node:os"; import { join } from "node:path"; -import { afterAll, describe, expect, it } from "vitest"; +import { afterAll, describe, expect, it, vi } from "vitest"; import { findFFmpeg, findFFprobe } from "../browser/ffmpeg.js"; import { againstStart, faceBoxes, faceFit, minWindow, readSamples, sampleFrames } from "./face.js"; import { probeVideo, runFfmpeg } from "./media.js"; import { OPENCV_DIR } from "./opencv.js"; +// A run with the 30 s limit that names this file fails the way execFileSync reports a run its timeout stopped. +const stalled = vi.hoisted(() => ({ file: "" })); +vi.mock("node:child_process", async (importOriginal) => { + const actual = await importOriginal(); + const execFileSync = ((...args: Parameters) => { + if (stalled.file && args[1]?.includes(stalled.file) && args[2]?.timeout === 30_000) + throw Object.assign(new Error(`spawnSync ${args[0]} ETIMEDOUT`), { + code: "ETIMEDOUT", + signal: "SIGTERM", + }); + return actual.execFileSync(...args); + }) as typeof actual.execFileSync; + return { ...actual, execFileSync }; +}); + // As in opencv.test.ts: borrow this machine's verified opencv.js cache, skip without one. const cached = join(userInfo().homedir, ".cache", "hyperframes", "opencv", "4.10.0"); if (existsSync(join(cached, "opencv.js")) && !existsSync(join(OPENCV_DIR, "opencv.js"))) @@ -137,6 +152,26 @@ describe.skipIf(!media)("readSamples", () => { readSamples(probeVideo(video), ks, 80, 48), ); }); + + it("refuses text that a text-art demuxer would show as a picture", () => { + const art = join(dir, "notes.ans"); + writeFileSync(art, "SECRET=abc\n"); + const info = probeVideo(art); + expect(readSamples(info, [0], 80, 48)).toHaveLength(1); + expect(() => readSamples({ ...info, guarded: true }, [0], 80, 48)).toThrow(/not on whitelist/); + }); + + it("says the time limit stopped a guarded decode", () => { + const info = probeVideo(video, { guarded: true }); + stalled.file = video; + try { + expect(() => readSamples(info, [1], 80, 48)).toThrow( + `ffmpeg cannot read ${video}: stopped after 30 s`, + ); + } finally { + stalled.file = ""; + } + }); }); describe.skipIf(!opencv)("faceBoxes", () => { diff --git a/packages/cli/src/measure/face.ts b/packages/cli/src/measure/face.ts index f5ddfe54a3..8d518fb143 100644 --- a/packages/cli/src/measure/face.ts +++ b/packages/cli/src/measure/face.ts @@ -6,6 +6,7 @@ import { GUARDED_RUN_MS, ffmpegBin, frameAt, + guardedLimitReason, round, runFfmpeg, sumOf, @@ -54,6 +55,17 @@ export function minWindow(h: number, share: number): number { return Math.round(WINDOW_PX * STEP ** steps); } +function decode(info: VideoInfo, args: string[]): Buffer { + if (!info.guarded) return runFfmpeg(args); + const formats = mediaInputFormatArgs(ffmpegBin(), info.path); + try { + return runFfmpeg([...formats, ...args], undefined, undefined, GUARDED_RUN_MS); + } catch (err) { + const stopped = guardedLimitReason(err); + throw stopped ? new Error(`ffmpeg cannot read ${info.path}: ${stopped}`) : err; + } +} + /** Frames `ks` (indices from the first) as grey bytes at `w` x `h`, keeping the video's aspect. Each run of nearby * frames is one ffmpeg pass from its first frame to its last; a variable-rate video, which a seek can't index, is read * from its start. */ @@ -74,27 +86,21 @@ export function readSamples(info: VideoInfo, ks: number[], w: number, h: number) ...(w !== info.width || h !== info.height ? [`scale=${w}:${h}`] : []), "format=gray", ]; - const raw = runFfmpeg( - [ - ...seek, - ...(info.guarded ? mediaInputFormatArgs(ffmpegBin(), info.path) : []), - "-i", - info.path, - "-an", - "-vf", - filters.join(","), - "-fps_mode", - "passthrough", - "-frames:v", - String(run.length), - "-f", - "rawvideo", - "-", - ], - undefined, - undefined, - info.guarded ? GUARDED_RUN_MS : undefined, - ); + const raw = decode(info, [ + ...seek, + "-i", + info.path, + "-an", + "-vf", + filters.join(","), + "-fps_mode", + "passthrough", + "-frames:v", + String(run.length), + "-f", + "rawvideo", + "-", + ]); const frames: Uint8Array[] = []; for (let i = 0; i + size <= raw.length; i += size) frames.push(raw.subarray(i, i + size)); return frames; diff --git a/packages/cli/src/measure/media.ts b/packages/cli/src/measure/media.ts index d1c27c0b4a..353f9cc91e 100644 --- a/packages/cli/src/measure/media.ts +++ b/packages/cli/src/measure/media.ts @@ -14,6 +14,13 @@ const MAX_BUFFER = 1 << 30; const FFMPEG_QUIET = ["-nostdin", "-v", "error"]; export const GUARDED_RUN_MS = 30_000; +export function guardedLimitReason(err: unknown): string | undefined { + const { code, signal } = (err ?? {}) as { code?: unknown; signal?: unknown }; + return code === "ETIMEDOUT" || signal === "SIGTERM" + ? `stopped after ${GUARDED_RUN_MS / 1000} s` + : undefined; +} + /** An error whose message says what to run instead; commands print `fix` as its own line. */ export class Fix extends Error { constructor( @@ -220,7 +227,7 @@ export function probeVideo(path: string, { guarded = false } = {}): VideoInfo { ).toString(), ); } catch (err) { - const reason = ffprobeReason(err, path); + const reason = (guarded ? guardedLimitReason(err) : undefined) ?? ffprobeReason(err, path); throw new Fix( `ffprobe cannot read ${path}${reason ? `: ${reason}` : ""}`, unreadableFix(reason, startsWithMp4Header(path)), diff --git a/packages/cli/src/utils/textOverFace.test.ts b/packages/cli/src/utils/textOverFace.test.ts index ed27e61b14..c8092cd6a3 100644 --- a/packages/cli/src/utils/textOverFace.test.ts +++ b/packages/cli/src/utils/textOverFace.test.ts @@ -3,6 +3,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; +import { findFFprobe } from "../browser/ffmpeg.js"; import type { CheckBbox, TextOverVideoCandidate } from "./checkTypes.js"; import { FACE_COVER_REPORT, @@ -14,6 +15,8 @@ import { } from "./textOverFace.js"; const spawned = vi.hoisted(() => [] as string[]); +// A run with the 30 s limit that names this file fails the way execFileSync reports a run its timeout stopped. +const stalled = vi.hoisted(() => ({ file: "" })); vi.mock("node:child_process", async (importOriginal) => { const actual = await importOriginal(); const recorded = @@ -22,10 +25,18 @@ vi.mock("node:child_process", async (importOriginal) => { spawned.push(String(args[0])); return run(...args); }; + const execFileSync = ((...args: Parameters) => { + if (stalled.file && args[1]?.includes(stalled.file) && args[2]?.timeout === 30_000) + throw Object.assign(new Error(`spawnSync ${args[0]} ETIMEDOUT`), { + code: "ETIMEDOUT", + signal: "SIGTERM", + }); + return actual.execFileSync(...args); + }) as typeof actual.execFileSync; return { ...actual, execFile: recorded(actual.execFile), - execFileSync: recorded(actual.execFileSync), + execFileSync: recorded(execFileSync), spawn: recorded(actual.spawn), spawnSync: recorded(actual.spawnSync), }; @@ -352,3 +363,33 @@ describe("findTextOverFace on a shipped playlist", () => { }, ); }); + +describe.skipIf(!findFFprobe())("findTextOverFace on a source the time limit stops", () => { + afterEach(() => { + stalled.file = ""; + vi.restoreAllMocks(); + }); + + it("says the probe stopped after 30 s", async () => { + const dir = mkdtempSync(join(tmpdir(), "hf-face-stalled-")); + try { + const file = join(dir, "take.mp4"); + // An MP4's first bytes, so the file reads as media. + writeFileSync(file, Buffer.from("000000186674797069736f6d", "hex")); + const errors = vi.spyOn(console, "error").mockImplementation(() => undefined); + stalled.file = file; + + const { faceCheck } = await findTextOverFace([ + candidate({ video: { ...candidate().video, file } }), + ]); + + const reason = `ffprobe cannot read ${file}: stopped after 30 s`; + expect(faceCheck).toEqual({ ran: false, reason }); + expect(errors).toHaveBeenCalledWith( + `[hyperframes] check: text over a face was not looked for in 1 of 1 video(s): ${reason}`, + ); + } finally { + rmSync(dir, { recursive: true, force: true }); + } + }); +});