Files
hyperframes/packages/engine/src/utils/alphaPlaneProbe.ts
T
Vance IngallsandClaude Opus 5.5 ead110aab9 fix(engine,producer,cli): media inputs refuse formats that open other files (#7931)
* fix(engine,producer): media inputs refuse formats that open other files

Composition media read by ffprobe/ffmpeg now passes a format allowlist on every
input: the demuxers the installed ffmpeg reports, minus the ones that open other
files or URLs on their own (DASH, IMF and similar). hls and concat stay allowed,
since ffmpeg carries the list into every file they open. The HDR window,
alpha-plane, GIF, HEIC, audio-sidecar and snapshot reads that skipped it now use it.

The not-media check skips ID3v2 tags before it looks at the first byte, refuses a
playlist entry that is a document, and refuses JSON or non-SVG markup as an <img> src.

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

* fix(engine,parsers,core,lint,cli): format allowlist reads old listings and covers every media probe

The demuxer listing is read from the binary that enforces the flag, accepts ffmpeg 4's
two-column output, and a failed listing is asked again next time. The lint HEVC probe,
the grade analyzer and the snapshot probes use it too. A nested playlist entry is refused
only for a real document signature, so encrypted segments and keys are kept; an image whose
prolog outruns the sniff window, or that is UTF-16, is no longer refused.

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

* chore(parsers,producer): trim comments added with the media format list

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

* test(engine): keep the format-limit probe's argv in the shape the ffprobe contract reads

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

* fix(engine): the image markup check reads a doctype without regex backtracking

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

* chore(parsers): raise the parsers size budget for the media format list

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 00:35:56 -08:00

77 lines
2.3 KiB
TypeScript

import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { runFfmpeg } from "./runFfmpeg.js";
import { urlInputArgs } from "./urlInput.js";
// Up to 3 frames at 8x8 rgba; a 1- or 2-frame clip is still a whole sample.
const BYTES_PER_SAMPLE_FRAME = 8 * 8 * 4;
const MAX_SAMPLE_BYTES = BYTES_PER_SAMPLE_FRAME * 3;
/** true: every sampled alpha byte is 255; false: some transparency; undefined: malformed sample. */
export function sampledRgbaAlphaIsFullyOpaque(buf: Buffer): boolean | undefined {
if (
buf.length === 0 ||
buf.length > MAX_SAMPLE_BYTES ||
buf.length % BYTES_PER_SAMPLE_FRAME !== 0
) {
return undefined;
}
for (let i = 3; i < buf.length; i += 4) {
if (buf[i] !== 255) return false;
}
return true;
}
/**
* Whether a video input that declares alpha decodes fully opaque (a remux can keep `alpha_mode=1`
* while dropping the alpha sidecar). `decoder` must keep alpha. Any failure returns undefined.
*/
export async function probeInputAlphaPlane(
videoPath: string,
decoder: string,
signal?: AbortSignal,
): Promise<boolean | undefined> {
let probeDir: string | undefined;
try {
probeDir = mkdtempSync(join(tmpdir(), "hf-alpha-probe-"));
const samplePath = join(probeDir, "alpha.raw");
const result = await runFfmpeg(
[
"-v",
"error",
...urlInputArgs(videoPath),
"-c:v",
decoder,
"-i",
videoPath,
"-frames:v",
"3",
"-vf",
"scale=8:8",
"-pix_fmt",
"rgba",
"-f",
"rawvideo",
samplePath,
],
{ timeout: 30_000, signal },
);
if (!result.success) return undefined;
return sampledRgbaAlphaIsFullyOpaque(readFileSync(samplePath));
} catch {
return undefined;
} finally {
if (probeDir) rmSync(probeDir, { recursive: true, force: true });
}
}
export function inputAlphaOpaqueWarning(src: string): string {
return (
`[hyperframes:render] WARNING: video src="${src}" declares an alpha channel ` +
"but its first frames decode fully opaque. If it should be transparent, re-export " +
"it with an alpha pixel format and avoid remuxing afterward, which can drop the " +
"alpha while keeping the tag.\n"
);
}