Open source CapCut alternative.
TypeScript
import { mkdir, mkdtemp, rm, stat } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import type { CloudDocSnapshot } from "../lib/headless/docSession";
import { buildDocExportSpec, isDocExportPreset } from "../lib/headless/docExport";
import { runExport, type ExportSpec, type RenderHandle } from "../server/exportPipeline";
import { storeCardArtifacts } from "./cardJob";
import { prisma, registerObjectIn, storageTransaction, type ClaimedJob } from "./db";
import { deleteObjects, downloadToFile, exportKey, mediaKey, mimeFor, uploadFile } from "./r2";
import { overlayKey } from "../server/cloud/r2";
import { STORAGE_FULL } from "../lib/operationFailure";
import { UserError } from "../lib/userError";
import { isTabExport, specMediaFiles, STEMS_MIME, stemsArchiveName } from "../lib/exportDelivery";
import { runnerSession } from "./session";
import type { DocExportAudio } from "../lib/exportPresets";
/** The stored spec of an export/preview CutRenderJob: the engine export spec
* plus the R2 keys of the browser-rendered overlay PNGs (title/caption stills
* and sub_blank.png). The queueing route (cloud/jobs.ts) stores exactly this
* and puts the already-deduped export name on the row's outName column. */
export interface ExportJobSpec {
spec: ExportSpec;
/** A render asked for by a client that cannot build a spec — the phone. The
* row carries the size and nothing else; the worker opens the project doc
* and builds the spec itself. */
fromDoc?: { preset?: string; snapshot: CloudDocSnapshot } & DocExportAudio;
overlays?: { name: string; key: string }[];
/** "overlays": the cut's media came up with the job — a browser-resident
* project borrowing this worker. Its output is the job's scratch too: it
* lands in the same namespace, is never a media object, and dies when the
* tab dismisses the job or the sweep reaches it. */
mediaFrom?: "overlays";
/** HLS only: whether the client burned captions into this render, decided by
* what the share grants. Recorded on the ladder so a share that later stops
* granting Subtitles is refused the render rather than served cue text it
* cannot strip out of the pixels. */
burnedSubtitles?: boolean;
/** The most bytes this render's output may land, stamped by the queueing
* route from what the account's storage quota leaves. A render that outgrows
* it fails here rather than uploading past the cap. */
maxBytes?: number;
}
/** The job's overlay-PNG R2 keys, restricted to the job owner's own overlay
* namespace — the queueing route already rejects foreign keys, and this keeps
* the worker safe against rows written any other way. */
export function overlayKeysOf(job: ClaimedJob): string[] {
const overlays = (job.spec as ExportJobSpec | null)?.overlays ?? [];
const prefix = `cut/${job.userId}/overlays/`;
return overlays
.map((o) => o?.key)
.filter((k): k is string => typeof k === "string" && k.startsWith(prefix));
}
/**
* Run `render` against a fresh work dir and remove the dir once the job is
* completely finished, derived artifacts and all. The cleanup lives out here
* on purpose: inside the run, a `return somePromise` would hand back work that
* still needs those files while the `finally` deleted them out from under it.
*/
export async function inWorkDir<T>(render: (work: string) => Promise<T>): Promise<T> {
const work = await mkdtemp(path.join(os.tmpdir(), "cut-worker-"));
try {
return await render(work);
} finally {
void rm(work, { recursive: true, force: true });
}
}
/**
* Pull everything the render reads into a project-shaped work dir: the clips,
* overlay videos and audio the spec names, plus the browser-rendered overlay
* PNGs. Returns the media dir the pipeline resolves file names against.
*
* Shared by every job that runs the export pipeline — the mp4 renders here and
* the HLS ladder in hlsJob.ts — so they cannot drift on what a staged project
* looks like.
*/
export async function stageJobMedia(
job: ClaimedJob,
body: ExportJobSpec,
projectId: string,
work: string,
handle: RenderHandle
): Promise<string> {
// The pipeline reads overlay/caption PNGs from handle.tmpDir by base name
// and writes its encode intermediate there too — mirror the engine's
// uploaded-PNG staging.
handle.tmpDir = path.join(work, "overlays");
const mediaDir = path.join(work, "media");
await Promise.all([
mkdir(handle.tmpDir, { recursive: true }),
mkdir(mediaDir, { recursive: true }),
]);
const spec = body.spec;
const mediaFiles = new Set(specMediaFiles(spec));
const overlayPrefix = `cut/${job.userId}/overlays/`;
for (const o of body.overlays ?? []) {
if (!o.key.startsWith(overlayPrefix)) throw new Error("Invalid overlay key.");
}
// A media file that came up with the job — a browser-resident project's
// clip — is read from its upload; the rest are the cloud project's own.
const uploaded = new Map((body.overlays ?? []).map((o) => [o.name, o.key]));
await Promise.all([
...[...mediaFiles].map((f) =>
downloadToFile(uploaded.get(f) ?? mediaKey(job.userId, projectId, f), path.join(mediaDir, path.basename(f)))
),
...(body.overlays ?? [])
.filter((o) => !mediaFiles.has(o.name))
.map((o) => downloadToFile(o.key, path.join(handle.tmpDir, path.basename(o.name)))),
]);
return mediaDir;
}
/**
* Run one export, preview, or card job: stage a project-shaped work dir
* (media/ + overlay PNGs in the pipeline tmp dir), render through the engine's
* shared pipeline, and land the result back in R2 — the mp4 for an export or
* hover proxy, the derived still and animation for a card. Returns what the
* job row records.
*/
export async function runExportJob(
job: ClaimedJob,
handle: RenderHandle
): Promise<{ outputKey: string; outName: string }> {
const stored = job.spec as ExportJobSpec;
const projectId = job.projectId ?? stored.spec?.projectId;
if (!projectId) throw new Error("Export job has no project.");
if (stored.fromDoc && !stored.fromDoc.snapshot?.doc) {
throw new UserError("render_snapshot_missing", "This render has no captured document. Queue the export again.");
}
// A tab render's claim carries no spec: the tab that made it draws it.
if (isTabExport(stored)) {
throw new UserError("render_in_tab", "This export renders in the browser tab that started it.");
}
if (!stored.fromDoc && (!stored.spec || !Array.isArray(stored.spec.clips))) {
throw new Error("Malformed export spec.");
}
return inWorkDir(async (work) => {
// The pipeline reads stills out of this dir; a doc-built spec paints its
// own straight into it, so it exists before the spec does.
handle.tmpDir = path.join(work, "overlays");
await mkdir(handle.tmpDir, { recursive: true });
const body: ExportJobSpec = stored.fromDoc
? {
...stored,
spec: (await buildDocExportSpec(
runnerSession(job),
projectId,
isDocExportPreset(stored.fromDoc.preset) ? stored.fromDoc.preset : "original",
handle.tmpDir,
stored.fromDoc.snapshot,
job.kind === "preview" ? "preview" : "export",
{ loudness: stored.fromDoc.loudness, stems: stored.fromDoc.stems }
)) as ExportSpec,
}
: stored;
const spec = body.spec;
const mode: "export" | "preview" | "card" =
job.kind === "preview" || spec.target === "preview"
? "preview"
: job.kind === "card" || spec.target === "card"
? "card"
: "export";
const preview = mode === "preview";
const mediaDir = await stageJobMedia(job, body, projectId, work, handle);
const outName = mode === "export" ? job.outName?.trim() || "export.mp4" : `${mode}.mp4`;
handle.outPath = path.join(work, "out", outName);
await mkdir(path.dirname(handle.outPath), { recursive: true });
// Stems ride the export they were asked with, under the name claimed with it.
const stemsName = mode === "export" && spec.stemPlan?.length ? stemsArchiveName(outName) : null;
// A borrowed render hands back only the picture; its tab mixes the stems.
if (stemsName && body.mediaFrom === "overlays") throw new Error("A borrowed render cannot carry stems.");
if (stemsName) handle.stemsPath = path.join(work, "out", stemsName);
await runExport(handle, spec, (file) => path.join(mediaDir, path.basename(file)));
// A card's rendered clip is scratch: only the still and the animation it
// derives are stored, so the mp4 never reaches R2.
if (mode === "card") return storeCardArtifacts(job, handle, projectId);
// The project may have been deleted (its jobs canceled) between the
// watcher's last tick and now; registering the output then would charge
// storage nothing can ever free.
const live = await prisma.cutRenderJob.findUnique({
where: { id: job.id },
select: { state: true },
});
if (live?.state !== "running") {
throw new UserError("render_canceled", "Canceled.");
}
const mime = mimeFor(outName);
if (body.mediaFrom === "overlays") {
const key = overlayKey(job.userId, job.id, outName);
await uploadFile(key, handle.outPath, mime);
return { outputKey: key, outName };
}
const key = preview
? `cut/${job.userId}/projects/${projectId}/previews/${job.id}.mp4`
: exportKey(job.userId, projectId, outName);
// A retained output is charged to the account, so it is held to the room
// the account had when the render was queued. The file is still on the
// worker's disk here: nothing goes up, and nothing is charged.
if (!preview && typeof stored.maxBytes === "number") {
const size =
(await stat(handle.outPath)).size + (handle.stemsPath ? (await stat(handle.stemsPath)).size : 0);
if (size > stored.maxBytes) {
throw new UserError("storage_quota_exceeded", STORAGE_FULL);
}
}
// The file and, when asked for, its stems: each its own object and row,
// charged at its own size.
const files = [
{ key, path: handle.outPath, fileName: outName, mime, kind: preview ? "preview" : "export" },
...(stemsName
? [{ key: exportKey(job.userId, projectId, stemsName), path: handle.stemsPath!, fileName: stemsName, mime: STEMS_MIME, kind: "export" }]
: []),
] as const;
const uploaded = files.map((f) => f.key);
try {
const sizes: number[] = [];
for (const f of files) sizes.push(await uploadFile(f.key, f.path, f.mime));
await storageTransaction(async (tx) => {
for (const [i, f] of files.entries()) {
await registerObjectIn(tx, {
userId: job.userId,
projectId,
r2Key: f.key,
fileName: f.fileName,
mime: f.mime,
bytes: sizes[i],
kind: f.kind,
});
}
});
} catch (err) {
// The file is up and no row owns it. The sweep walks rows, so nothing
// would ever find these bytes again — and a storage wall that leaves the
// bytes behind walls the account in tighter with every retry.
await deleteObjects(uploaded).catch(() => {});
throw err;
}
if (preview) {
await prisma.$transaction(async (tx) => {
const project = await tx.cutProject.findUnique({ where: { id: projectId }, select: { previewKey: true } });
if (!project) return;
const newest = await tx.cutRenderJob.findFirst({
where: { userId: job.userId, projectId, kind: "preview" },
orderBy: [{ createdAt: "desc" }, { id: "desc" }], select: { id: true },
});
if (newest?.id !== job.id) return;
// A newer completion changes the pointer, so this write cannot replace it.
await tx.cutProject.updateMany({
where: { id: projectId, userId: job.userId, previewKey: project.previewKey },
data: { previewKey: key },
});
});
}
return { outputKey: key, outName };
});
}