Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: bound video edit transcoding

Richie McIlroy223058ca8475
diff --git a/apps/media-server/src/__tests__/lib/media-edit.test.ts b/apps/media-server/src/__tests__/lib/media-edit.test.ts
index 8f79c93b6..12234a74d 100644
--- a/apps/media-server/src/__tests__/lib/media-edit.test.ts
+++ b/apps/media-server/src/__tests__/lib/media-edit.test.ts
@@ -100,7 +100,7 @@ describe("media edit helpers", () => {
expect(args).toContain("[a]");
});
- test("builds one transcode graph for many edit ranges", () => {
+ test("builds a bounded multi-input transcode graph", () => {
const args = buildTranscodeEditArgs(
"/input.mp4",
[
@@ -114,14 +114,25 @@ describe("media edit helpers", () => {
);
const filter = args[args.indexOf("-filter_complex") + 1];
- expect(args.filter((value) => value === "-i")).toHaveLength(1);
+ expect(args.filter((value) => value === "-i")).toHaveLength(3);
+ expect(args.filter((value) => value === "-ss")).toHaveLength(3);
expect(filter).toContain("concat=n=3:v=1:a=1[v][a]");
- expect(filter).toContain(
- "[0:v:0]fps=60,trim=start=1.200:end=2.000,setpts=PTS-STARTPTS[v1]",
- );
- expect(filter).toContain(
- "[0:a:0]atrim=start=2.300:end=3.000,asetpts=PTS-STARTPTS[a2]",
- );
+ expect(filter).toContain("[1:v:0]fps=60,setpts=PTS-STARTPTS[v1]");
+ expect(filter).toContain("[2:a:0]asetpts=PTS-STARTPTS[a2]");
+ });
+
+ test("rejects unbounded transcode graphs", () => {
+ expect(() =>
+ buildTranscodeEditArgs(
+ "/input.mp4",
+ Array.from({ length: 5 }, (_, index) => ({
+ start: index,
+ end: index + 0.5,
+ })),
+ "/output.mp4",
+ true,
+ ),
+ ).toThrow("Transcode batches must contain 1-4 ranges");
});
});
@@ -133,8 +144,11 @@ describe("renderEditedVideo integration tests", () => {
const editedFile = await renderEditedVideo({
inputPath: TEST_VIDEO_WITH_AUDIO,
keepRanges: [
- { start: 0, end: 0.4 },
- { start: 0.55, end: 0.95 },
+ { start: 0.08, end: 0.16 },
+ { start: 0.24, end: 0.32 },
+ { start: 0.4, end: 0.48 },
+ { start: 0.56, end: 0.64 },
+ { start: 0.72, end: 0.8 },
],
metadata,
onProgress: (progress) => {
diff --git a/apps/media-server/src/lib/media-edit.ts b/apps/media-server/src/lib/media-edit.ts
index eb36625ab..c475a7c62 100644
--- a/apps/media-server/src/lib/media-edit.ts
+++ b/apps/media-server/src/lib/media-edit.ts
@@ -1,7 +1,8 @@
+import { writeFile } from "node:fs/promises";
import { file, spawn } from "bun";
import type { VideoMetadata } from "./job-manager";
import { registerSubprocess, terminateProcess } from "./subprocess";
-import { createTempFile } from "./temp-files";
+import { createTempFile, type TempFileHandle } from "./temp-files";
export type EditRange = {
start: number;
@@ -22,6 +23,7 @@ const MIN_RANGE_DURATION = 0.05;
const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000;
const MAX_TIMEOUT_MS = 60 * 60 * 1000;
const DEFAULT_OUTPUT_FPS = 30;
+const MAX_TRANSCODE_RANGES_PER_BATCH = 4;
function roundTime(value: number) {
return Math.round(value * 1000) / 1000;
@@ -153,13 +155,16 @@ export function buildTranscodeEditArgs(
hasAudio: boolean,
fps = DEFAULT_OUTPUT_FPS,
) {
- const filters = ranges.flatMap((range, index) => {
- const videoFilter = `[0:v:0]fps=${getOutputFps(fps)},trim=start=${formatTime(range.start)}:end=${formatTime(range.end)},setpts=PTS-STARTPTS[v${index}]`;
+ if (ranges.length === 0 || ranges.length > MAX_TRANSCODE_RANGES_PER_BATCH) {
+ throw new Error(
+ `Transcode batches must contain 1-${MAX_TRANSCODE_RANGES_PER_BATCH} ranges`,
+ );
+ }
+
+ const filters = ranges.flatMap((_, index) => {
+ const videoFilter = `[${index}:v:0]fps=${getOutputFps(fps)},setpts=PTS-STARTPTS[v${index}]`;
if (!hasAudio) return [videoFilter];
- return [
- videoFilter,
- `[0:a:0]atrim=start=${formatTime(range.start)}:end=${formatTime(range.end)},asetpts=PTS-STARTPTS[a${index}]`,
- ];
+ return [videoFilter, `[${index}:a:0]asetpts=PTS-STARTPTS[a${index}]`];
});
const inputs = ranges
.map((_, index) => `[v${index}]${hasAudio ? `[a${index}]` : ""}`)
@@ -170,8 +175,14 @@ export function buildTranscodeEditArgs(
"ffmpeg",
"-hide_banner",
"-y",
- "-i",
- inputPath,
+ ...ranges.flatMap((range) => [
+ "-ss",
+ formatTime(range.start),
+ "-t",
+ formatTime(getRangeDuration(range)),
+ "-i",
+ inputPath,
+ ]),
"-filter_complex",
[...filters, concat].join(";"),
"-map",
@@ -191,6 +202,27 @@ export function buildTranscodeEditArgs(
];
}
+function buildConcatArgs(listPath: string, outputPath: string) {
+ return [
+ "ffmpeg",
+ "-hide_banner",
+ "-y",
+ "-f",
+ "concat",
+ "-safe",
+ "0",
+ "-i",
+ listPath,
+ "-map",
+ "0",
+ "-c",
+ "copy",
+ "-movflags",
+ "+faststart",
+ outputPath,
+ ];
+}
+
async function drainStream(stream: ReadableStream<Uint8Array> | null) {
if (!stream) return;
const reader = stream.getReader();
@@ -256,6 +288,8 @@ async function runFfmpegCommand(
timeoutMs: number,
abortSignal?: AbortSignal,
) {
+ abortSignal?.throwIfAborted();
+
const proc = registerSubprocess(
spawn({
cmd: args,
@@ -299,27 +333,25 @@ async function runFfmpegCommand(
}
}
-async function renderTranscodedEdit(
- inputPath: string,
- keepRanges: EditRange[],
- hasAudio: boolean,
- fps: number | undefined,
+function concatFileLine(path: string) {
+ return `file '${path.replaceAll("'", "'\\''")}'`;
+}
+
+async function concatSegments(
+ segmentFiles: TempFileHandle[],
timeoutMs: number,
- onProgress?: ProgressCallback,
abortSignal?: AbortSignal,
) {
+ const concatList = await createTempFile(".txt");
const outputFile = await createTempFile(".mp4");
try {
- onProgress?.(5, "Preparing edit...");
+ await writeFile(
+ concatList.path,
+ `${segmentFiles.map((segment) => concatFileLine(segment.path)).join("\n")}\n`,
+ );
await runFfmpegCommand(
- buildTranscodeEditArgs(
- inputPath,
- keepRanges,
- outputFile.path,
- hasAudio,
- fps,
- ),
+ buildConcatArgs(concatList.path, outputFile.path),
timeoutMs,
abortSignal,
);
@@ -329,11 +361,83 @@ async function renderTranscodedEdit(
throw new Error("FFmpeg produced empty edited output");
}
- onProgress?.(75, "Edit prepared");
return outputFile;
} catch (error) {
await outputFile.cleanup();
throw error;
+ } finally {
+ await concatList.cleanup();
+ }
+}
+
+function createTranscodeBatches(ranges: EditRange[]) {
+ const batches: EditRange[][] = [];
+ for (
+ let index = 0;
+ index < ranges.length;
+ index += MAX_TRANSCODE_RANGES_PER_BATCH
+ ) {
+ batches.push(ranges.slice(index, index + MAX_TRANSCODE_RANGES_PER_BATCH));
+ }
+ return batches;
+}
+
+function getRemainingTimeoutMs(startedAt: number, timeoutMs: number) {
+ const remainingMs = Math.ceil(timeoutMs - (performance.now() - startedAt));
+ if (remainingMs <= 0) {
+ throw new Error(`Operation timed out after ${timeoutMs}ms`);
+ }
+ return remainingMs;
+}
+
+async function renderTranscodedEdit(
+ inputPath: string,
+ keepRanges: EditRange[],
+ hasAudio: boolean,
+ fps: number | undefined,
+ timeoutMs: number,
+ onProgress?: ProgressCallback,
+ abortSignal?: AbortSignal,
+) {
+ const batches = createTranscodeBatches(keepRanges);
+ const batchFiles: TempFileHandle[] = [];
+ const startedAt = performance.now();
+
+ try {
+ onProgress?.(5, "Preparing edit...");
+ for (const [index, batch] of batches.entries()) {
+ const batchFile = await createTempFile(".mp4");
+ batchFiles.push(batchFile);
+ await runFfmpegCommand(
+ buildTranscodeEditArgs(inputPath, batch, batchFile.path, hasAudio, fps),
+ getRemainingTimeoutMs(startedAt, timeoutMs),
+ abortSignal,
+ );
+ onProgress?.(
+ 5 + ((index + 1) / batches.length) * 65,
+ "Preparing edit...",
+ );
+ }
+
+ if (batchFiles.length === 1) {
+ const outputFile = batchFiles[0];
+ if (!outputFile || (await file(outputFile.path).size) === 0) {
+ throw new Error("FFmpeg produced empty edited output");
+ }
+ batchFiles.length = 0;
+ onProgress?.(75, "Edit prepared");
+ return outputFile;
+ }
+
+ const outputFile = await concatSegments(
+ batchFiles,
+ getRemainingTimeoutMs(startedAt, timeoutMs),
+ abortSignal,
+ );
+ onProgress?.(75, "Edit prepared");
+ return outputFile;
+ } finally {
+ await Promise.all(batchFiles.map((batchFile) => batchFile.cleanup()));
}
}