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.
This commit is contained in:
Miguel Angel Simon Sierra
2026-11-19 20:44:32 -05:00
parent 0a38c58458
commit 21e951668d
4 changed files with 59 additions and 43 deletions
+2 -2
View File
@@ -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",
+2 -1
View File
@@ -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("");
@@ -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",
@@ -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<void> {
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",