mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
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.
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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("");
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user