diff --git a/packages/engine/src/services/chunkEncoder.relativePaths.test.ts b/packages/engine/src/services/chunkEncoder.relativePaths.test.ts index 5266304d42..5e48205116 100644 --- a/packages/engine/src/services/chunkEncoder.relativePaths.test.ts +++ b/packages/engine/src/services/chunkEncoder.relativePaths.test.ts @@ -152,7 +152,7 @@ describe.skipIf(!HAS_FFMPEG)("concat input formats", () => { expect(result).toEqual({ success: false, - error: expect.stringContaining("control character"), + error: expect.stringContaining("line break or NUL"), externalInterruption: false, }); expect(readdirSync(folder).filter((name) => name.startsWith("concat-list-"))).toEqual([]); diff --git a/packages/parsers/src/ffBinaries.test.ts b/packages/parsers/src/ffBinaries.test.ts index f2ad2bf175..c5c7d524b5 100644 --- a/packages/parsers/src/ffBinaries.test.ts +++ b/packages/parsers/src/ffBinaries.test.ts @@ -640,22 +640,83 @@ describe.skipIf(spawnSync("ffmpeg", ["-version"]).status !== 0)("concatListInput expect(run.status).not.toBe(0); expect(run.stderr).toContain("not on whitelist 'file'"); }); + + it.skipIf(process.platform === "win32")( + "joins every clip under a folder whose name holds a tab, a quote and a #", + async () => { + const { concatListInputArgs, concatListText } = await importFresh(); + const root = mkdtempSync(join(tmpdir(), "hf-concat-tab-")); + onTestFinished(() => rmSync(root, { recursive: true, force: true })); + const dir = join(root, "out\tit's #dir"); + mkdirSync(dir); + const clips = ["a.mp4", "b.mp4"].map((name) => join(dir, name)); + for (const clip of clips) { + const made = spawnSync("ffmpeg", [ + "-v", + "error", + "-f", + "lavfi", + "-i", + "testsrc=size=64x64:rate=10", + "-frames:v", + "10", + "-c:v", + "mpeg4", + "-y", + clip, + ]); + expect(made.status).toBe(0); + } + const list = join(dir, "list.txt"); + writeFileSync(list, concatListText(clips)); + const out = join(dir, "joined.mp4"); + const run = spawnSync( + "ffmpeg", + ["-v", "error", ...concatListInputArgs(list), "-c", "copy", "-y", out], + { + encoding: "utf8", + }, + ); + expect(run.stderr).toBe(""); + const probe = spawnSync( + "ffprobe", + [ + "-v", + "error", + "-count_packets", + "-select_streams", + "v", + "-show_entries", + "stream=nb_read_packets", + "-of", + "csv=p=0", + "--", + out, + ], + { encoding: "utf8" }, + ); + expect(probe.stdout.trim()).toBe("20"); + }, + ); }); describe("concatListText", () => { // fails without the fix: every list writer escaped only the single quote, so a line break started a new list line. - it("writes one quoted entry per path and refuses a path holding a control character", async () => { + it("writes one quoted entry per path and refuses a path holding a line break or NUL", async () => { const { concatListText } = await importFresh(); expect(concatListText(["/a/it's.mp4", "/b/c.mp4"])).toBe( "file '/a/it'\\''s.mp4'\nfile '/b/c.mp4'\n", ); - for (const path of [ - "/p\nfile '/etc/x.mov'\n#/c.mp4", - "/p\r/c.mp4", - "/p\u0000/c.mp4", - "/p\u007f/c.mp4", - ]) { - expect(() => concatListText([path])).toThrow("control character"); + for (const path of ["/p\nfile '/etc/x.mov'\n#/c.mp4", "/p\r/c.mp4", "/p\u0000/c.mp4"]) { + expect(() => concatListText([path])).toThrow("line break or NUL"); } }); + + // fails without the fix: a tab inside the quotes is read fine by ffmpeg, but the writer refused it. + it("keeps a tab or DEL in a path, which do not end a list line", async () => { + const { concatListText } = await importFresh(); + expect(concatListText(["/out\tdir/c.mp4", "/p\u007f/c.mp4"])).toBe( + "file '/out\tdir/c.mp4'\nfile '/p\u007f/c.mp4'\n", + ); + }); }); diff --git a/packages/parsers/src/ffBinaries.ts b/packages/parsers/src/ffBinaries.ts index 68f04ab606..5617dc3fdb 100644 --- a/packages/parsers/src/ffBinaries.ts +++ b/packages/parsers/src/ffBinaries.ts @@ -249,16 +249,15 @@ export function clearFfBinaryLookupCache(): void { skippedX86Binaries.clear(); } -const holdsControlCharacter = (text: string) => - [...text].some((char) => char < " " || char === "\u007f"); +const endsAListLine = (text: string) => ["\n", "\r", "\u0000"].some((char) => text.includes(char)); -/** One `file` entry per path; a control character (a line break starts a list line) is refused. */ +/** One `file` entry per path; a path holding what ends a list line (LF, CR, NUL) is refused. */ export function concatListText(paths: readonly string[]): string { return paths .map((path) => { - if (holdsControlCharacter(path)) { + if (endsAListLine(path)) { throw new Error( - `A file to join holds a control character in its path: ${JSON.stringify(path)}`, + `A file to join holds a line break or NUL in its path: ${JSON.stringify(path)}`, ); } return `file '${path.replace(/'/g, "'\\''")}'\n`; diff --git a/packages/producer/src/services/distributed/assemble.test.ts b/packages/producer/src/services/distributed/assemble.test.ts index 738d62f803..0abedccf91 100644 --- a/packages/producer/src/services/distributed/assemble.test.ts +++ b/packages/producer/src/services/distributed/assemble.test.ts @@ -388,21 +388,20 @@ describe("assemble()", () => { ); // fails without the fix: the line break ended the entry and ffmpeg read the rest as list lines. - it( - "refuses chunk paths whose folder name holds a line break, before writing a list", + it.skipIf(process.platform === "win32")( + "refuses chunk paths whose folder name holds a line break", async () => { const chunks: ChunkSliceJson[] = [ { index: 0, startFrame: 0, endFrame: 5 }, { index: 1, startFrame: 5, endFrame: 10 }, ]; const planDir = buildPlanDir("mp4", chunks, 10, false); - if (process.platform === "win32") return; // Windows paths cannot hold a line break const folder = join(planDir, "chunks\nfile 'other.mov'\n#"); mkdirSync(folder); const chunkPaths = ["0.mp4", "1.mp4"].map((name) => join(folder, name)); for (const path of chunkPaths) writeFileSync(path, "not a video"); await expect(assemble(planDir, chunkPaths, null, join(planDir, "out.mp4"))).rejects.toThrow( - "control character", + "line break or NUL", ); }, TIMEOUT_MS,