Skip to content

Open source CapCut alternative.

TypeScript
3.6 KB
Raw Blame
import { runLibraryUploadJob } from "@/cut/worker/libraryUploadJob";
import type { LibraryAsset } from "@/cut/server/library";
import { mkdtemp, rm } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import type { RenderHandle } from "../server/exportPipeline";
import { convertModeIn } from "../lib/convertMode";
import { convertToMp4, mp4NameFor } from "../server/convert";
import { probeDuration } from "../server/frames";
import { dedupeName } from "../server/cloud/util";
import { prisma, registerObject, unregisterObjects, type ClaimedJob } from "./db";
import { deleteObjects, downloadToFile, mediaKey, uploadFile } from "./r2";

/** What a convert job records in CutRenderJob.result — the same shape the
 * engine's synchronous convert route returns. */
export interface ConvertResult {
  fileName: string;
  duration: number;
  width?: number;
  height?: number;
  transcodedVideo: boolean;
  transcodedAudio: boolean;
  /** The project's file was already what this would produce. */
  unchanged?: boolean;
}

/**
 * Run one convert job: pull the project's media file out of R2, rewrite it as
 * MP4 with the same code the engine runs on a Mac, and land the result in the
 * project's media prefix under a name deduped against its existing objects.
 * The original is left alone — the client swaps its asset over and deletes
 * what it no longer points at.
 */
export async function runConvertJob(
  job: ClaimedJob,
  handle: RenderHandle,
  isCanceled: () => boolean
): Promise<ConvertResult | LibraryAsset> {
  if ((job.spec as { target?: string } | null)?.target === "library") {
    return runLibraryUploadJob(job, handle, isCanceled);
  }
  const { file, maxHeight, mode: asked } = (job.spec ?? {}) as { file?: string; maxHeight?: number; mode?: unknown };
  const mode = convertModeIn(asked) ?? "fit";
  if (!file) throw new Error("Convert job has no file.");
  const projectId = job.projectId;
  if (!projectId) throw new Error("Convert job has no project.");

  const tmp = await mkdtemp(path.join(os.tmpdir(), "cut-convert-"));
  handle.tmpDir = tmp;
  try {
    const src = path.join(tmp, path.basename(file));
    await downloadToFile(mediaKey(job.userId, projectId, file), src);
    if (isCanceled()) throw new Error("Conversion canceled.");

    const rows = await prisma.cutMediaObject.findMany({
      where: { projectId, kind: "media" },
      select: { fileName: true },
    });
    const fileName = dedupeName(mp4NameFor(file), new Set(rows.map((r) => r.fileName)));
    const out = path.join(tmp, fileName);
    handle.outPath = out;

    const total = await probeDuration(src);
    const outcome = await convertToMp4(handle, src, out, {
      maxHeight,
      mode,
      onProgress: (seconds) => {
        if (total > 0) handle.progress = Math.min(0.99, seconds / total);
      },
    });
    if (isCanceled()) throw new Error("Conversion canceled.");
    // Already the file this would produce: the object in R2 stays as it is.
    if (outcome.unchanged) return { fileName: file, duration: total, ...outcome };

    const key = mediaKey(job.userId, projectId, fileName);
    try {
      const bytes = await uploadFile(key, out, "video/mp4");
      await registerObject({
        userId: job.userId,
        projectId,
        r2Key: key,
        fileName,
        mime: "video/mp4",
        bytes,
        kind: "media",
      });
    } catch (err) {
      await unregisterObjects(job.userId, [key]).catch(() => {});
      await deleteObjects([key]);
      throw err;
    }
    return { fileName, duration: await probeDuration(out), ...outcome };
  } finally {
    void rm(tmp, { recursive: true, force: true });
  }
}