From 0a38c58458fd953472deaaf60ab9db32437dd11c Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Thu, 19 Nov 2026 17:51:47 -0500 Subject: [PATCH] fix(studio-server): each picture piece of a preview copy starts exactly where its stretch does At a frame rate that does not divide 15 s, such as 29.97, every piece ran about 15 ms long and the join placed the next piece after it, so the picture drifted from the continuous sound (600 ms over 10 minutes). The join list now gives each piece its stretch's length (ffconcat duration), so pieces land on the source's timeline and the offset stays within a frame. --- packages/parsers/src/ffBinaries.test.ts | 10 +++++++ packages/parsers/src/ffBinaries.ts | 10 ++++--- .../src/helpers/proxyTranscoder.test.ts | 27 ++++++++++--------- .../src/helpers/proxyTranscoder.ts | 7 ++--- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/packages/parsers/src/ffBinaries.test.ts b/packages/parsers/src/ffBinaries.test.ts index c5c7d524b5..f3fc46125f 100644 --- a/packages/parsers/src/ffBinaries.test.ts +++ b/packages/parsers/src/ffBinaries.test.ts @@ -712,6 +712,16 @@ describe("concatListText", () => { } }); + it("places an entry given a duration exactly that long after the one before", 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", + ); + expect(() => concatListText([{ path: "/p\n.mp4", durationSeconds: 1 }])).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(); diff --git a/packages/parsers/src/ffBinaries.ts b/packages/parsers/src/ffBinaries.ts index 0945bc9b08..10321f578d 100644 --- a/packages/parsers/src/ffBinaries.ts +++ b/packages/parsers/src/ffBinaries.ts @@ -253,18 +253,20 @@ const endsAListLine = (text: string) => ["\n", "\r", "\u0000"].some((char) => te /** One `file` entry per path; a path holding what ends a list line (LF, CR, NUL) is refused. */ export function concatListText( - paths: readonly string[], + entries: readonly (string | { path: string; durationSeconds: number })[], options: { videoContainersOnly?: boolean } = {}, ): string { const entryOptions = options.videoContainersOnly ? "option format_whitelist mov,matroska\n" : ""; - return paths - .map((path) => { + return entries + .map((entry) => { + const path = typeof entry === "string" ? entry : entry.path; if (endsAListLine(path)) { throw new Error( `A file to join holds a line break or NUL in its path: ${JSON.stringify(path)}`, ); } - return `file '${path.replace(/'/g, "'\\''")}'\n${entryOptions}`; + const placed = typeof entry === "string" ? "" : `duration ${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 312a940108..eed25ecd9e 100644 --- a/packages/studio-server/src/helpers/proxyTranscoder.test.ts +++ b/packages/studio-server/src/helpers/proxyTranscoder.test.ts @@ -429,7 +429,7 @@ describe("resolveProxy on a long source", () => { const CLAPS = [2, 14.5, 15.2, 29.9, 30.4, 44.6, 47]; const FPS = 30; - const soundMinusFlashPerClap = (path: string): number[] => { + const soundMinusFlashPerClap = (path: string, claps: number[]): number[] => { let at = 0; let lit = false; const flashes: number[] = []; @@ -455,32 +455,33 @@ describe("resolveProxy on a long source", () => { const sounds = quiet.flatMap((match, index) => match[1] === "end" && quiet[index + 1]?.[1] === "start" ? [Number(match[2])] : [], ); - expect(flashes).toHaveLength(CLAPS.length); - expect(sounds).toHaveLength(CLAPS.length); + expect(flashes).toHaveLength(claps.length); + expect(sounds).toHaveLength(claps.length); return flashes.map((flash, index) => sounds[index]! - flash); }; it.skipIf(!realFfmpeg || !findFfBinary("ffprobe")).each([ - ["picture and sound together", 0, 0], - ["picture starting late", 0.4, 0], - ["sound starting late", 0, 0.4], + ["picture and sound together", 0, 0, String(FPS), LONG_SECONDS, CLAPS], + ["picture starting late", 0.4, 0, String(FPS), LONG_SECONDS, CLAPS], + ["sound starting late", 0, 0.4, String(FPS), LONG_SECONDS, CLAPS], + ["at 29.97 frames a second over two minutes", 0, 0, "30000/1001", 120, [2, 61.3, 118]], ])( "keeps every clap within a frame of the source across the seams, %s", - async (_case, pictureDelay, soundDelay) => { - const during = (seconds: number) => - CLAPS.map((clap) => `between(t,${clap},${clap + seconds})`).join("+"); + async (_case, pictureDelay, soundDelay, rate, seconds, claps) => { + const during = (length: number) => + claps.map((clap) => `between(t,${clap},${clap + length})`).join("+"); const { sourcePath, proxyPath } = await realCopyOf("claps.mp4", (path) => execFileSync(realFfmpeg!, [ ...["-v", "error", "-itsoffset", String(pictureDelay), "-f", "lavfi", "-i"], - `color=black:s=32x32:r=${FPS}:d=${LONG_SECONDS},drawbox=c=white:t=fill:enable='${during(2 / FPS)}'`, + `color=black:s=32x32:r=${rate}:d=${seconds},drawbox=c=white:t=fill:enable='${during(2 / FPS)}'`, ...["-itsoffset", String(soundDelay), "-f", "lavfi", "-i"], - `aevalsrc='(${during(0.03)})*sin(2*PI*1000*t)':s=48000:d=${LONG_SECONDS}`, + `aevalsrc='(${during(0.03)})*sin(2*PI*1000*t)':s=48000:d=${seconds}`, ...["-map", "0:v", "-map", "1:a", "-c:v", "libx264", "-pix_fmt", "yuv420p"], ...["-c:a", "aac", path], ]), ); - const source = soundMinusFlashPerClap(sourcePath); - soundMinusFlashPerClap(proxyPath).forEach((offset, index) => + const source = soundMinusFlashPerClap(sourcePath, claps); + soundMinusFlashPerClap(proxyPath, claps).forEach((offset, index) => expect(Math.abs(offset - source[index]!)).toBeLessThanOrEqual(1 / FPS), ); }, diff --git a/packages/studio-server/src/helpers/proxyTranscoder.ts b/packages/studio-server/src/helpers/proxyTranscoder.ts index 1d73e45a1e..cf3ea6ef74 100644 --- a/packages/studio-server/src/helpers/proxyTranscoder.ts +++ b/packages/studio-server/src/helpers/proxyTranscoder.ts @@ -662,9 +662,10 @@ async function encodeInChunks( writeFileSync( listPath, concatListText( - pieces.map((piece) => - JOIN_FROM_CACHE_FOLDER ? basename(piece) : piece.replace(/\\/g, "/"), - ), + pieces.map((piece, index) => ({ + path: JOIN_FROM_CACHE_FOLDER ? basename(piece) : piece.replace(/\\/g, "/"), + durationSeconds: ranges[index]!.lengthSeconds, + })), ), ); await concat(listPath, tempPath);