From 21e951668d92cc787fb1cd3303497093490c93cb Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Thu, 19 Nov 2026 20:44:32 -0500 Subject: [PATCH] fix(studio-server): a preview copy of a clip whose picture starts late keeps that start On ffmpeg 8 the piece join moved the first piece to zero, so a picture that starts after the sound lost its lead against the sound added later. The join now keeps each piece's own timestamps (ffconcat inpoint 0 with each stretch's duration, and -copyts), on ffmpeg 7 and 8 alike. The long-source path moves into copyInPieces. --- packages/parsers/src/ffBinaries.test.ts | 4 +- packages/parsers/src/ffBinaries.ts | 3 +- .../src/helpers/proxyTranscoder.test.ts | 3 +- .../src/helpers/proxyTranscoder.ts | 92 +++++++++++-------- 4 files changed, 59 insertions(+), 43 deletions(-) diff --git a/packages/parsers/src/ffBinaries.test.ts b/packages/parsers/src/ffBinaries.test.ts index f3fc46125f..66ed48b99d 100644 --- a/packages/parsers/src/ffBinaries.test.ts +++ b/packages/parsers/src/ffBinaries.test.ts @@ -712,10 +712,10 @@ describe("concatListText", () => { } }); - it("places an entry given a duration exactly that long after the one before", async () => { + it("places an entry given a duration that long after the one before, on its own timestamps", async () => { const { concatListText } = await importFresh(); expect(concatListText([{ path: "/a/it's.mp4", durationSeconds: 15 }, "/b/c.mp4"])).toBe( - "file '/a/it'\\''s.mp4'\nduration 15\nfile '/b/c.mp4'\n", + "file '/a/it'\\''s.mp4'\ninpoint 0\nduration 15\nfile '/b/c.mp4'\n", ); expect(() => concatListText([{ path: "/p\n.mp4", durationSeconds: 1 }])).toThrow( "line break or NUL", diff --git a/packages/parsers/src/ffBinaries.ts b/packages/parsers/src/ffBinaries.ts index 10321f578d..06b901b999 100644 --- a/packages/parsers/src/ffBinaries.ts +++ b/packages/parsers/src/ffBinaries.ts @@ -265,7 +265,8 @@ export function concatListText( `A file to join holds a line break or NUL in its path: ${JSON.stringify(path)}`, ); } - const placed = typeof entry === "string" ? "" : `duration ${entry.durationSeconds}\n`; + const placed = + typeof entry === "string" ? "" : `inpoint 0\nduration ${entry.durationSeconds}\n`; return `file '${path.replace(/'/g, "'\\''")}'\n${placed}${entryOptions}`; }) .join(""); diff --git a/packages/studio-server/src/helpers/proxyTranscoder.test.ts b/packages/studio-server/src/helpers/proxyTranscoder.test.ts index eed25ecd9e..e68ff19c24 100644 --- a/packages/studio-server/src/helpers/proxyTranscoder.test.ts +++ b/packages/studio-server/src/helpers/proxyTranscoder.test.ts @@ -272,7 +272,8 @@ describe("resolveProxy on a long source", () => { await answerAll(calls); await result; const joinArgs = calls.at(-1)!.args; - expect(joinArgs.slice(1, 14)).toEqual([ + expect(joinArgs.slice(1, 15)).toEqual([ + "-copyts", "-xerror", "-protocol_whitelist", "file", diff --git a/packages/studio-server/src/helpers/proxyTranscoder.ts b/packages/studio-server/src/helpers/proxyTranscoder.ts index cf3ea6ef74..b6d626123e 100644 --- a/packages/studio-server/src/helpers/proxyTranscoder.ts +++ b/packages/studio-server/src/helpers/proxyTranscoder.ts @@ -824,41 +824,18 @@ async function runFfmpeg( onProgress({ doneSeconds: encodedSeconds, totalSeconds }), ); } - const picturePath = soundMap.length ? besideAsTemp(outputPath) : outputPath; - await encodeInChunks( - ranges, - chunks, - picturePath, - encode, - (listPath, target) => { - // ffmpeg resolves relative list entries against the list path, which breaks on '#' or '?' in a folder name. - const cwd = JOIN_FROM_CACHE_FOLDER ? dirname(listPath) : undefined; - return spawnFfmpeg( - ffmpegPath, - concatArgs(cwd ? basename(listPath) : listPath, target, variant), - CONCAT_TIMEOUT_MS, - signal, - undefined, - false, - cwd, - ); - }, + await copyInPieces(ranges, outputPath, { + ffmpegPath, + sourcePath, + soundMap, + variant, + limitSeconds, signal, gate, - (doneSeconds) => onProgress({ doneSeconds, totalSeconds }), - ); - if (picturePath !== outputPath) { - await addSoundFromSource( - ffmpegPath, - sourcePath, - soundMap, - picturePath, - outputPath, - variant, - limitSeconds, - signal, - ); - } + chunks, + encode, + onDone: (doneSeconds) => onProgress({ doneSeconds, totalSeconds }), + }); } catch (err) { throw withSource(err, () => describeSource(sourcePath, metadata)); } @@ -869,15 +846,51 @@ function besideAsTemp(path: string, extension = extname(path)): string { return join(dirname(path), `.tmp-${randomUUID()}-${copyName}${extension}`); } +type PieceCopy = { + ffmpegPath: string; + sourcePath: string; + soundMap: string[]; + variant: ProxyVariant; + limitSeconds: number | undefined; + signal: AbortSignal; + gate: PieceGate; + chunks: ChunkFiles; + encode: EncodeFn; + onDone: (doneSeconds: number) => void; +}; + +async function copyInPieces(ranges: ChunkRange[], outputPath: string, copy: PieceCopy) { + const withSound = copy.soundMap.length > 0; + const picturePath = withSound ? besideAsTemp(outputPath) : outputPath; + await encodeInChunks( + ranges, + copy.chunks, + picturePath, + copy.encode, + (listPath, target) => { + // ffmpeg resolves relative list entries against the list path, which breaks on '#' or '?' in a folder name. + const cwd = JOIN_FROM_CACHE_FOLDER ? dirname(listPath) : undefined; + return spawnFfmpeg( + copy.ffmpegPath, + concatArgs(cwd ? basename(listPath) : listPath, target, copy.variant), + CONCAT_TIMEOUT_MS, + copy.signal, + undefined, + false, + cwd, + ); + }, + copy.signal, + copy.gate, + copy.onDone, + ); + if (withSound) await addSoundFromSource(copy, picturePath, outputPath); +} + async function addSoundFromSource( - ffmpegPath: string, - sourcePath: string, - soundMap: string[], + { ffmpegPath, sourcePath, soundMap, variant, limitSeconds, signal }: PieceCopy, picturePath: string, outputPath: string, - variant: ProxyVariant, - limitSeconds: number | undefined, - signal: AbortSignal, ): Promise { const keepsAlpha = variant === "vp8"; const soundPath = besideAsTemp(outputPath, keepsAlpha ? ".webm" : ".m4a"); @@ -918,6 +931,7 @@ async function addSoundFromSource( function concatArgs(listPath: string, target: string, variant: ProxyVariant): string[] { return [ "-y", + "-copyts", ...concatListInputArgs(listPath), "-c", "copy",