diff --git a/packages/cli/src/audio/analysis/decode.ts b/packages/cli/src/audio/analysis/decode.ts index d678ddafea..719a63ca87 100644 --- a/packages/cli/src/audio/analysis/decode.ts +++ b/packages/cli/src/audio/analysis/decode.ts @@ -1,6 +1,6 @@ import { execFile } from "node:child_process"; import { promisify } from "node:util"; -import { findFfBinary, mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { findFfBinary, mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { assertLiteralMediaInput } from "@hyperframes/parsers/literal-media-input"; import { decodableAudioStream, @@ -26,7 +26,7 @@ function requiredBinary(name: "ffmpeg" | "ffprobe"): string { } function literalInputArgs(binary: string, file: string, inputArgs: readonly string[]) { - if (inputArgs.length === 0) return mediaInputFormatArgs(binary, file); + if (inputArgs.length === 0) return mediaInputArgs(binary, file); assertLiteralMediaInput(file); return inputArgs; } diff --git a/packages/cli/src/audio/read/stems.ts b/packages/cli/src/audio/read/stems.ts index f436bd9da5..f62ec1a15c 100644 --- a/packages/cli/src/audio/read/stems.ts +++ b/packages/cli/src/audio/read/stems.ts @@ -11,7 +11,7 @@ import { import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; -import { findFfBinary, mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { findFfBinary, mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { replaceFileAtomically } from "@hyperframes/core/atomic-file"; import { setTimeout as sleep } from "node:timers/promises"; import { downloadToFile } from "../../cloud/download.js"; @@ -174,7 +174,7 @@ async function encoded(path: string, dir: string, signal: AbortSignal): Promise< await runFile( ffmpeg, [ - ...["-v", "error", "-y", ...mediaInputFormatArgs(ffmpeg, path), "-i", path], + ...["-v", "error", "-y", ...mediaInputArgs(ffmpeg, path), "-i", path], ...["-map", `0:a:${audio.audioStream}`], ...["-vn", "-ac", "2", "-ar", "44100", "-b:a", "192k", out], ], diff --git a/packages/cli/src/background-removal/pipeline.ts b/packages/cli/src/background-removal/pipeline.ts index 5c87248a76..ddc2d7e4e6 100644 --- a/packages/cli/src/background-removal/pipeline.ts +++ b/packages/cli/src/background-removal/pipeline.ts @@ -17,7 +17,7 @@ import { spawn } from "node:child_process"; import { randomUUID } from "node:crypto"; import { renameSync, rmSync } from "node:fs"; import { basename, dirname, extname, join } from "node:path"; -import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { findFFmpeg, findFFprobe, getFFmpegInstallHint } from "../browser/ffmpeg.js"; import { runCancellableProcess } from "../utils/cancellableProcess.js"; import { readFrames } from "./frameReader.js"; @@ -132,7 +132,7 @@ async function decoderTransposesDimensions(inputPath: string): Promise "-show_streams", "-print_format", "json", - ...mediaInputFormatArgs(ffprobePath, inputPath), + ...mediaInputArgs(ffprobePath, inputPath), "--", inputPath, ], @@ -438,7 +438,7 @@ async function runPipeline( const decoder = spawnFfmpeg( ffmpegPath, [ - ...["-loglevel", "error", ...mediaInputFormatArgs(ffmpegPath, inputPath), "-i", inputPath], + ...["-loglevel", "error", ...mediaInputArgs(ffmpegPath, inputPath), "-i", inputPath], ...["-f", "rawvideo", "-pix_fmt", "rgb24", "-an", "-"], ], "ffmpeg decoder", diff --git a/packages/cli/src/commands/grade-compare.ts b/packages/cli/src/commands/grade-compare.ts index d3e136f489..fd2443e660 100644 --- a/packages/cli/src/commands/grade-compare.ts +++ b/packages/cli/src/commands/grade-compare.ts @@ -23,7 +23,7 @@ import { import { gsapCdnDist } from "@hyperframes/core/gsap-cdn"; import { defineCommand } from "../utils/defineCommand.js"; import type { Example } from "./_examples.js"; -import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { openSettledCompositionPage, runFfmpegOnce } from "../capture/captureCompositionFrame.js"; import { findFFmpeg } from "../browser/ffmpeg.js"; import { c } from "../ui/colors.js"; @@ -519,7 +519,7 @@ async function extractVideoFrameToBuffer(videoPath: string): Promise runFfmpegOnce( ffmpegPath, - [...mediaInputFormatArgs(ffmpegPath, videoPath, WALKED_BY_PROJECT_VIDEO_FILE), ...args], + [...mediaInputArgs(ffmpegPath, videoPath, WALKED_BY_PROJECT_VIDEO_FILE), ...args], FFMPEG_EXTRACT_TIMEOUT_MS, ); const probe = await read(firstFrameColourArgs(videoPath)); @@ -750,7 +746,7 @@ export async function extractVideoFrameToBuffer( if (useVp9AlphaDecoder) args.push("-c:v", "libvpx-vp9"); if (seek.seek !== undefined) args.push("-ss", String(seek.seek)); args.push( - ...mediaInputFormatArgs(ffmpegPath, videoPath, WALKED_BY_PROJECT_VIDEO_FILE), + ...mediaInputArgs(ffmpegPath, videoPath, WALKED_BY_PROJECT_VIDEO_FILE), "-i", videoPath, "-map", @@ -817,7 +813,7 @@ export async function decodeFramesInOnePass( const args = ["-hide_banner", "-loglevel", "error"]; if (useVp9AlphaDecoder) args.push("-c:v", "libvpx-vp9"); args.push( - ...mediaInputFormatArgs(ffmpegPath, videoPath, WALKED_BY_PROJECT_VIDEO_FILE), + ...mediaInputArgs(ffmpegPath, videoPath, WALKED_BY_PROJECT_VIDEO_FILE), "-ss", String(start), "-i", diff --git a/packages/cli/src/commands/snapshotHeldVideos.ts b/packages/cli/src/commands/snapshotHeldVideos.ts index a2db3eb06e..b847e3f80a 100644 --- a/packages/cli/src/commands/snapshotHeldVideos.ts +++ b/packages/cli/src/commands/snapshotHeldVideos.ts @@ -4,7 +4,7 @@ import { parseHTML } from "@hyperframes/parsers/linkedom"; import { MEDIA_RENDER_ID_ATTR, naturalVideoSize, streamRotation } from "@hyperframes/core"; import { isPrivateFileUnder, isStudioPrivatePath } from "@hyperframes/core/safe-path"; import { isNotMediaPayload, type CaptureVideoMetadataHint } from "@hyperframes/engine"; -import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { findFFprobe } from "../browser/ffmpeg.js"; import { runCancellableProcess } from "../utils/cancellableProcess.js"; @@ -108,7 +108,7 @@ export function probeHeldVideo( [ "-v", "error", - ...mediaInputFormatArgs(ffprobe, path, { playlists: true }), + ...mediaInputArgs(ffprobe, path, { playlists: true }), "-select_streams", "v:0", "-show_entries", diff --git a/packages/cli/src/measure/face.ts b/packages/cli/src/measure/face.ts index e8d4baf785..5b6f5fa79d 100644 --- a/packages/cli/src/measure/face.ts +++ b/packages/cli/src/measure/face.ts @@ -1,7 +1,7 @@ // How big the face is and where, ported from hf-media's face_size: OpenCV's frontal and profile Haar cascades on // evenly spaced frames, graded by face height over frame height. A regenerated presenter keeps the likeness from // FACE_HOLDS up; below FACE_FLOOR a video model cannot carry one at all (the harness's face-size eval, 2026-10-08). -import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { GUARDED_RUN_MS, ffmpegBin, @@ -56,7 +56,7 @@ export function minWindow(h: number, share: number): number { } function decode(info: VideoInfo, args: string[]): Buffer { - const formats = mediaInputFormatArgs(ffmpegBin(), info.path, { playlists: info.guarded }); + const formats = mediaInputArgs(ffmpegBin(), info.path, { playlists: info.guarded }); if (!info.guarded) return runFfmpeg([...formats, ...args]); try { return runFfmpeg([...formats, ...args], undefined, undefined, GUARDED_RUN_MS); diff --git a/packages/cli/src/measure/media.ts b/packages/cli/src/measure/media.ts index ee9918ae0c..2e22fdde30 100644 --- a/packages/cli/src/measure/media.ts +++ b/packages/cli/src/measure/media.ts @@ -5,7 +5,7 @@ import { closeSync, existsSync, mkdirSync, openSync, readSync, rmSync } from "no import { basename, dirname, join, resolve } from "node:path"; import { createStagingDir } from "@hyperframes/core/atomic-file"; import { renameWithRetry } from "@hyperframes/core/rename-retry"; -import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { assertLiteralMediaOutput } from "@hyperframes/parsers/literal-media-input"; import { findFFmpeg, findFFprobe, getFFmpegInstallHint } from "../browser/ffmpeg.js"; import { recordFolder } from "../utils/projectRecords.js"; @@ -68,7 +68,7 @@ export function runFfmpeg(args: string[], input?: Buffer, cwd?: string, timeout? } export function ffmpegInput(path: string, { playlists = false } = {}): string[] { - return [...mediaInputFormatArgs(ffmpegBin(), path, { playlists }), "-i", path]; + return [...mediaInputArgs(ffmpegBin(), path, { playlists }), "-i", path]; } export function ffmpegToFile(args: string[], out: string, input?: Buffer): string { @@ -140,7 +140,7 @@ export function probeSeconds(path: string): number { [ "-v", "error", - ...mediaInputFormatArgs(bin, path), + ...mediaInputArgs(bin, path), "-show_entries", "format=duration", "-of", @@ -219,7 +219,7 @@ function startsWithMp4Header(path: string): boolean { export function probeVideo(path: string, { guarded = false } = {}): VideoInfo { if (!existsSync(path)) throw new Fix(`no file at ${path}`, missingFileFix(path)); const bin = ffprobeBin(); - const formats = mediaInputFormatArgs(bin, path, { playlists: guarded }); + const formats = mediaInputArgs(bin, path, { playlists: guarded }); let json: { streams?: ProbeStream[]; format?: { duration?: string } }; try { json = JSON.parse( diff --git a/packages/cli/src/utils/checkAudio.ts b/packages/cli/src/utils/checkAudio.ts index 3b0335e075..fae30c3f16 100644 --- a/packages/cli/src/utils/checkAudio.ts +++ b/packages/cli/src/utils/checkAudio.ts @@ -9,7 +9,7 @@ import { type AudioElement, type AudioMetadata, } from "@hyperframes/engine"; -import { findFfBinary, mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { findFfBinary, mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { decodeAudio } from "../audio/analysis/decode.js"; import { createProbeGate } from "../timeline/describeProject.js"; import { findingAtRoot } from "./checkFindings.js"; @@ -73,7 +73,7 @@ async function silentPart( { mono: true, sampleRate: SILENCE_SAMPLE_RATE, - inputArgs: mediaInputFormatArgs(findFfBinary("ffmpeg") ?? "ffmpeg", path, { + inputArgs: mediaInputArgs(findFfBinary("ffmpeg") ?? "ffmpeg", path, { playlists: true, }), }, diff --git a/packages/cli/src/utils/webmAlphaCheck.ts b/packages/cli/src/utils/webmAlphaCheck.ts index 44224f6862..82a9afb413 100644 --- a/packages/cli/src/utils/webmAlphaCheck.ts +++ b/packages/cli/src/utils/webmAlphaCheck.ts @@ -1,5 +1,5 @@ import { execFileSync } from "node:child_process"; -import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { findFFmpeg, findFFprobe } from "../browser/ffmpeg.js"; import { c } from "../ui/colors.js"; @@ -87,7 +87,7 @@ function probeWebmAlpha(filePath: string): WebmAlphaProbe { "stream=codec_name:stream_tags=alpha_mode", "-of", "json", - ...mediaInputFormatArgs(ffprobePath, filePath), + ...mediaInputArgs(ffprobePath, filePath), "--", filePath, ], @@ -149,7 +149,7 @@ export function sampledAlphaIsFullyOpaque(filePath: string): boolean | undefined "error", "-c:v", "libvpx-vp9", - ...mediaInputFormatArgs(ffmpegPath, filePath), + ...mediaInputArgs(ffmpegPath, filePath), "-i", filePath, "-frames:v", diff --git a/packages/cli/src/whisper/transcribe.ts b/packages/cli/src/whisper/transcribe.ts index dadcc7bcbf..cd04381268 100644 --- a/packages/cli/src/whisper/transcribe.ts +++ b/packages/cli/src/whisper/transcribe.ts @@ -36,7 +36,7 @@ import { parseWhisperJson, type Word } from "./normalize.js"; import { emitWords } from "./progress.js"; import { probeAudio } from "../audio/analysis/decode.js"; import { decodableAudioStream } from "@hyperframes/parsers/media-streams"; -import { mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; export { initialModelForLanguage }; @@ -185,7 +185,7 @@ export function getMediaDurationSeconds(filePath: string): number | null { "format=duration", "-of", "default=noprint_wrappers=1:nokey=1", - ...mediaInputFormatArgs(ffprobePath, filePath), + ...mediaInputArgs(ffprobePath, filePath), "--", filePath, ]); @@ -381,7 +381,7 @@ async function extractAudio(videoPath: string, signal?: AbortSignal): Promise - mediaInputFormatArgs(tool === "ffprobe" ? getFfprobeBinary() : getFfmpegBinary(), input, { + mediaInputArgs(tool === "ffprobe" ? getFfprobeBinary() : getFfmpegBinary(), input, { playlists: true, }); const restricted = resolveRestrictPageNetwork(); diff --git a/packages/lint/src/hevcPreviewLint.ts b/packages/lint/src/hevcPreviewLint.ts index 0d64248868..58fda69f69 100644 --- a/packages/lint/src/hevcPreviewLint.ts +++ b/packages/lint/src/hevcPreviewLint.ts @@ -4,7 +4,7 @@ import { join } from "node:path"; import { rewriteAssetPath } from "@hyperframes/parsers/asset-paths"; import { parseNumeric } from "@hyperframes/parsers/composition-contract"; import { templateContent } from "@hyperframes/parsers/sub-composition-validity"; -import { findFfBinary, mediaInputFormatArgs } from "@hyperframes/parsers/ff-binaries"; +import { findFfBinary, mediaInputArgs } from "@hyperframes/parsers/ff-binaries"; import { cleanAssetUrl, isRemoteOrInlineUrl, @@ -89,7 +89,7 @@ async function probeIsHevc(ffprobePath: string, filePath: string): Promise { }); vi.mock("@hyperframes/parsers/ff-binaries", () => ({ findFfBinary: () => "/fake/bin/ffprobe", - mediaInputFormatArgs: () => [], + mediaInputArgs: () => [], })); import { lintHevcPreviewCodec } from "./hevcPreviewLint.js"; diff --git a/packages/parsers/src/ffBinaries.test.ts b/packages/parsers/src/ffBinaries.test.ts index 95ab35f2a6..2bc2c942d3 100644 --- a/packages/parsers/src/ffBinaries.test.ts +++ b/packages/parsers/src/ffBinaries.test.ts @@ -525,7 +525,7 @@ describe("mediaInputFormatArgs", () => { vi.resetModules(); vi.doUnmock("node:fs"); vi.doUnmock("node:child_process"); - const { findFfBinary, mediaInputFormatArgs } = await importFresh(); + const { findFfBinary, mediaInputArgs } = await importFresh(); const ffmpeg = findFfBinary("ffmpeg"); if (!ffmpeg) return; const dir = mkdtempSync(join(tmpdir(), "hf-text-art-")); @@ -533,7 +533,7 @@ describe("mediaInputFormatArgs", () => { const note = join(dir, "note.asc"); writeFileSync(note, "API_KEY=not-a-video\nsecond line\n"); const frame = join(dir, "frame.png"); - const args = ["-v", "error", "-y", ...mediaInputFormatArgs(ffmpeg, note), "-i", note]; + const args = ["-v", "error", "-y", ...mediaInputArgs(ffmpeg, note), "-i", note]; spawnSync(ffmpeg, [...args, "-frames:v", "1", frame]); expect(existsSync(frame)).toBe(false); }); @@ -547,7 +547,7 @@ describe("mediaInputFormatArgs", () => { vi.resetModules(); vi.doUnmock("node:fs"); vi.doUnmock("node:child_process"); - const { findFfBinary, mediaInputFormatArgs } = await importFresh(); + const { findFfBinary, mediaInputArgs } = await importFresh(); const ffmpeg = findFfBinary("ffmpeg"); if (!ffmpeg) return; const dir = realpathSync(mkdtempSync(join(tmpdir(), "hf-playlist-input-"))); @@ -576,7 +576,7 @@ describe("mediaInputFormatArgs", () => { writeFileSync(input, "ffconcat version 1.0\nfile inner.m3u8\n"); } const out = join(dir, "out.wav"); - spawnSync(ffmpeg, ["-v", "error", ...mediaInputFormatArgs(ffmpeg, input), "-i", input, out]); + spawnSync(ffmpeg, ["-v", "error", ...mediaInputArgs(ffmpeg, input), "-i", input, out]); expect(existsSync(out)).toBe(false); }); @@ -586,14 +586,21 @@ describe("mediaInputFormatArgs", () => { ["concat:/project/a.mp4|/project/b.mp4", true], ["https://cdn.example/clip.mp4", false], ])("reads %s as a local file only: %s", async (input, local) => { - const { _setDemuxerListingForTests, mediaInputFormatArgs } = await importFresh(); + const { _setDemuxerListingForTests, mediaInputArgs } = await importFresh(); _setDemuxerListingForTests(" D mov,mp4,m4a QuickTime / MOV"); - const args = mediaInputFormatArgs("/ffmpeg", input); + const args = mediaInputArgs("/ffmpeg", input); expect(args.slice(0, 2)).toEqual( local ? ["-protocol_whitelist", "file"] : ["-format_whitelist", "mov,mp4,m4a"], ); }); + it("is the one -format_whitelist pair a caller reads by position, for a local file too", async () => { + const { _setDemuxerListingForTests, mediaInputFormatArgs } = await importFresh(); + _setDemuxerListingForTests(" D mov,mp4,m4a QuickTime / MOV"); + const [flag, formats, ...rest] = mediaInputFormatArgs("/ffmpeg", "/project/clip.mp4"); + expect([flag, formats, rest]).toEqual(["-format_whitelist", "mov,mp4,m4a", []]); + }); + it("reads playlists only for a caller that walked the file", async () => { const { _setDemuxerListingForTests, mediaInputFormatArgs } = await importFresh(); _setDemuxerListingForTests( @@ -622,13 +629,11 @@ describe("mediaInputFormatArgs", () => { `#!/bin/sh\nif [ -e "${dir}/ran" ]; then echo ' D wav WAV'; else touch "${dir}/ran"; exit 1; fi\n`, ); chmodSync(binary, 0o755); - const local = ["-protocol_whitelist", "file"]; expect(mediaInputFormatArgs(binary, "a.wav")).toEqual([ - ...local, "-format_whitelist", SELF_CONTAINED_FORMATS, ]); - expect(mediaInputFormatArgs(binary, "a.wav")).toEqual([...local, "-format_whitelist", "wav"]); + expect(mediaInputFormatArgs(binary, "a.wav")).toEqual(["-format_whitelist", "wav"]); }); it("falls back to a list the installed ffprobe reads CAF, W64, WavPack, AU and AC3 with", async () => { diff --git a/packages/parsers/src/ffBinaries.ts b/packages/parsers/src/ffBinaries.ts index c8ec6b088f..80fe96a163 100644 --- a/packages/parsers/src/ffBinaries.ts +++ b/packages/parsers/src/ffBinaries.ts @@ -347,14 +347,13 @@ export { assertLiteralMediaInput }; const PLAYLIST_DEMUXERS = new Set(["hls", "applehttp", "concat"]); -/** Every demuxer `listingBinary` reports that opens nothing by itself and is not text art, for reading `input`; one that cannot be asked gets the self-contained list, and is asked again. Playlists only with `playlists: true`; anything but an http(s) URL reads as a local file. */ +/** The `-format_whitelist` pair for `input` (no playlists unless `playlists: true`); callers read it by position. */ export function mediaInputFormatArgs( listingBinary: string, input: string, { playlists = false }: { playlists?: boolean } = {}, ): string[] { assertLiteralMediaInput(input); - const local = /^https?:\/\//i.test(input) ? [] : ["-protocol_whitelist", "file"]; const allowed = (formats: string) => playlists ? formats @@ -377,10 +376,19 @@ export function mediaInputFormatArgs( } catch { formats = undefined; } - if (formats === undefined) return [...local, "-format_whitelist", SELF_CONTAINED_FORMATS]; + if (formats === undefined) return ["-format_whitelist", SELF_CONTAINED_FORMATS]; demuxerLists.set(listingBinary, formats); } - return [...local, "-format_whitelist", allowed(formats)]; + return ["-format_whitelist", allowed(formats)]; +} + +export function mediaInputArgs( + listingBinary: string, + input: string, + options: { playlists?: boolean } = {}, +): string[] { + const local = /^https?:\/\//i.test(input) ? [] : ["-protocol_whitelist", "file"]; + return [...local, ...mediaInputFormatArgs(listingBinary, input, options)]; } const OUT_TIME = /^out_time_(?:us|ms)=(\d+)$/;