diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 93c667f044..5996b80d4d 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -198,7 +198,7 @@ export { } from "./services/screenshotService.js"; // ── Encoding ─────────────────────────────────────────────────────────────────── -export { concatListInputArgs } from "@hyperframes/parsers/ff-binaries"; +export { concatListInputArgs, concatListText } from "@hyperframes/parsers/ff-binaries"; export { buildEncoderArgs, encodeFramesFromDir, diff --git a/packages/engine/src/services/chunkEncoder.ts b/packages/engine/src/services/chunkEncoder.ts index 0a939ff4eb..6e00f22515 100644 --- a/packages/engine/src/services/chunkEncoder.ts +++ b/packages/engine/src/services/chunkEncoder.ts @@ -50,7 +50,7 @@ import { appendVp9CpuUsedArg } from "./vp9Options.js"; import { appendSoftwareEncoderThreadsArg } from "./softwareEncoderThreads.js"; import { appendRenderProvenanceArgs } from "../utils/renderProvenance.js"; import { createStagingDir } from "@hyperframes/core/atomic-file"; -import { concatListInputArgs } from "@hyperframes/parsers/ff-binaries"; +import { concatListInputArgs, concatListText } from "@hyperframes/parsers/ff-binaries"; export type { EncoderOptions, EncodeResult, MuxResult } from "./chunkEncoder.types.js"; @@ -693,7 +693,7 @@ let concatListSeq = 0; function writeConcatList(dir: string, inputPaths: readonly string[]): string { concatListSeq += 1; const listPath = join(dir, `concat-list-${process.pid}-${concatListSeq}.txt`); - const body = inputPaths.map((p) => `file '${resolve(p).replace(/'/g, "'\\''")}'`).join("\n"); + const body = concatListText(inputPaths.map((p) => resolve(p))); writeFileSync(listPath, body, "utf-8"); return listPath; } @@ -721,7 +721,12 @@ export async function concatVideoFiles( // get their own chunk directory but can share an output directory. // The list is left on disk deliberately: it is removed with the work dir, // and `--debug` keeps both so a bad concat can be reproduced from its list. - const listPath = writeConcatList(dirname(firstInput), inputPaths); + let listPath: string; + try { + listPath = writeConcatList(dirname(firstInput), inputPaths); + } catch (error) { + return { success: false, error: (error as Error).message, externalInterruption: false }; + } const encodeTimeout = config?.ffmpegEncodeTimeout ?? DEFAULT_CONFIG.ffmpegEncodeTimeout; const result = await runFfmpeg(buildConcatArgs(listPath, outputPath), { signal, diff --git a/packages/parsers/src/ffBinaries.ts b/packages/parsers/src/ffBinaries.ts index 9f2da0b89c..68f04ab606 100644 --- a/packages/parsers/src/ffBinaries.ts +++ b/packages/parsers/src/ffBinaries.ts @@ -249,6 +249,23 @@ export function clearFfBinaryLookupCache(): void { skippedX86Binaries.clear(); } +const holdsControlCharacter = (text: string) => + [...text].some((char) => char < " " || char === "\u007f"); + +/** One `file` entry per path; a control character (a line break starts a list line) is refused. */ +export function concatListText(paths: readonly string[]): string { + return paths + .map((path) => { + if (holdsControlCharacter(path)) { + throw new Error( + `A file to join holds a control character in its path: ${JSON.stringify(path)}`, + ); + } + return `file '${path.replace(/'/g, "'\\''")}'\n`; + }) + .join(""); +} + /** * Concat input for a list HyperFrames wrote: entries open only as local mov, matroska or concat * files (an ffconcat entry is followed in safe mode), and `-xerror` fails at a refused entry. diff --git a/packages/producer/src/services/distributed/assemble.ts b/packages/producer/src/services/distributed/assemble.ts index 4f700bc528..3e16e3dd36 100644 --- a/packages/producer/src/services/distributed/assemble.ts +++ b/packages/producer/src/services/distributed/assemble.ts @@ -39,6 +39,7 @@ import { appendRateCapArgs, appendRenderProvenanceArgs, concatListInputArgs, + concatListText, describeFfmpegFailure, getEncoderPreset, MIXED_AUDIO_FILENAME, @@ -393,10 +394,8 @@ function concatDemuxerInput( workDir: string, fpsArg: string, ): string[] { - // ffmpeg's concat list escapes a single quote as `'\''`. const listPath = join(workDir, "concat-list.txt"); - const body = chunkPaths.map((path) => `file '${path.replace(/'/g, "'\\''")}'`).join("\n"); - writeFileSync(listPath, `${body}\n`, "utf-8"); + writeFileSync(listPath, concatListText(chunkPaths), "utf-8"); return ["-r", fpsArg, ...concatListInputArgs(listPath)]; } diff --git a/packages/studio-server/src/helpers/proxyTranscoder.ts b/packages/studio-server/src/helpers/proxyTranscoder.ts index e6d03e4c25..bc48bca058 100644 --- a/packages/studio-server/src/helpers/proxyTranscoder.ts +++ b/packages/studio-server/src/helpers/proxyTranscoder.ts @@ -7,6 +7,7 @@ import { hdrToSdrToneMapFilter, type PreviewProxyBox } from "@hyperframes/core"; import { previewToken } from "@hyperframes/core/preview-proof"; import { concatListInputArgs, + concatListText, findFfBinary, followFfmpegProgress, } from "@hyperframes/parsers/ff-binaries"; @@ -659,10 +660,11 @@ async function encodeInChunks( try { writeFileSync( listPath, - pieces - .map((piece) => (JOIN_FROM_CACHE_FOLDER ? basename(piece) : piece.replace(/\\/g, "/"))) - .map((piece) => `file '${piece.replace(/'/g, "'\\''")}'\n`) - .join(""), + concatListText( + pieces.map((piece) => + JOIN_FROM_CACHE_FOLDER ? basename(piece) : piece.replace(/\\/g, "/"), + ), + ), ); await concat(listPath, tempPath); } finally {