Open source CapCut alternative.
TypeScript
test(site): browser export eval renders the cut in a real Chrome
David Le59c3848a6b2a
diff --git a/.github/workflows/browser-export-eval.yml b/.github/workflows/browser-export-eval.ymlnew file mode 100644index 00000000..8a353376--- /dev/null+++ b/.github/workflows/browser-export-eval.yml@@ -0,0 +1,82 @@+name: Browser Export Eval++# Renders the export eval project through the browser pipeline — framePlan,+# FrameCompositor, WebCodecs, the mediabunny muxer, the audio mix — in a real+# Chrome against a hermetic dev server (scripts/eval-cut-export.ts --hermetic).+# Every API call is answered inside the browser context, so the server needs+# no database and boots on placeholder env values.++on:+ workflow_dispatch:+ push:+ branches: [main]+ paths:+ - site/src/cut/lib/**+ - site/packages/effects-kit/**+ - site/scripts/eval-cut-export.ts+ - .github/workflows/browser-export-eval.yml++permissions:+ contents: read++concurrency:+ group: browser-export-eval+ cancel-in-progress: true++jobs:+ eval:+ runs-on: ubuntu-latest+ timeout-minutes: 25+ defaults:+ run:+ working-directory: site+ steps:+ - uses: actions/checkout@v5++ - uses: actions/setup-node@v5+ with:+ node-version: 22+ cache: npm+ cache-dependency-path: site/package-lock.json++ - name: Install dependencies+ run: npm ci++ - name: Generate Prisma client+ run: npx prisma generate++ - name: Install ffmpeg+ run: |+ sudo apt-get update -qq+ sudo apt-get install -y -qq ffmpeg++ # Placeholder values: the hermetic eval answers every API call inside+ # the browser context, so nothing on the server ever reads these — they+ # only have to exist for the dev server to boot.+ - name: Write placeholder env+ run: |+ for key in BETTER_AUTH_SECRET GOOGLE_CLIENT_ID GOOGLE_CLIENT_SECRET \+ R2_ACCOUNT_ID R2_ACCESS_KEY_ID R2_SECRET_ACCESS_KEY \+ CUT_MEDIA_SIGNING_SECRET; do+ echo "$key=placeholder" >> .env+ done+ echo "DATABASE_URL=postgresql://placeholder:placeholder@localhost:5432/placeholder" >> .env+ echo "DIRECT_URL=postgresql://placeholder:placeholder@localhost:5432/placeholder" >> .env++ - name: Start dev server+ run: |+ npm run dev > /tmp/next-dev.log 2>&1 &+ for i in $(seq 1 60); do+ if curl -sf -o /dev/null http://localhost:3000; then exit 0; fi+ sleep 2+ done+ echo "dev server never answered" >&2+ tail -50 /tmp/next-dev.log >&2+ exit 1++ - name: Render the eval project in Chrome+ run: npm run eval:cut-export -- --hermetic++ - name: Dev server log on failure+ if: failure()+ run: tail -100 /tmp/next-dev.log\ No newline at end of filediff --git a/site/package.json b/site/package.jsonindex 9659509a..3ce27c3d 100644--- a/site/package.json+++ b/site/package.json@@ -27,7 +27,8 @@"test": "bun test src packages/effects-kit/src","test:genvideo": "bun run scripts/genvideo-selftest.ts","eval:genvideo": "bun run scripts/genvideo-eval.ts",- "eval:cut-perf": "bun run scripts/eval-cut-perf.ts"+ "eval:cut-perf": "bun run scripts/eval-cut-perf.ts",+ "eval:cut-export": "bun run scripts/eval-cut-export.ts"},"dependencies": {"@ai-sdk/react": "^4.0.15",diff --git a/site/scripts/eval-cut-export.ts b/site/scripts/eval-cut-export.tsnew file mode 100644index 00000000..25890693--- /dev/null+++ b/site/scripts/eval-cut-export.ts@@ -0,0 +1,365 @@+#!/usr/bin/env bun+/**+ * Browser export eval: the render path a browser-resident (and cloud) project+ * takes — framePlan, FrameCompositor, WebCodecs, the mediabunny muxer, the+ * audio mix — driven end to end in a real Chrome.+ *+ * A fixture project using the renderer's features (cover framing with zoom and+ * pan, a transition, playback speed, a grade, a keyframed pose, a+ * picture-in-picture track, a text element, burned-in captions, a music bed+ * with fades, whole-video fades, a background color) renders through the same+ * `renderProjectToMp4` the export dock calls, and the resulting MP4 is pulled+ * back out and ffprobed: right duration, right dimensions, both streams.+ *+ * Needs `next dev` on :3000. Spends no credits.+ *+ * npm run eval:cut-export [--headed] [--base URL] [--hermetic]+ *+ * `--hermetic` answers every API call inside the browser context — the doc+ * GET with an empty project, the rest with empty stubs — so the run needs no+ * database and no real account. CI runs this mode against a `next dev` booted+ * on placeholder env values; the page shell is the only thing the server+ * serves.+ */++import { chromium, type Browser, type Page } from "playwright";+import { spawn } from "node:child_process";+import { existsSync } from "node:fs";+import { mkdir, readFile, writeFile } from "node:fs/promises";+import path from "node:path";++const SITE = path.resolve(import.meta.dir, "..");+const OUT = path.resolve(SITE, "..", "dist", "cut-export");+const STOCK = path.join(SITE, "public", "cut-stock-video");++const args = process.argv.slice(2);+const arg = (name: string) => {+ const i = args.indexOf(name);+ return i >= 0 ? args[i + 1] : undefined;+};+const has = (name: string) => args.includes(name);+const BASE = arg("--base") ?? "http://localhost:3000";+const HERMETIC = has("--hermetic");+/** The dev-only account the API bypass authenticates as. */+const DEV_USER = "donkey-dev-auth-bypass";+/** The project id a hermetic run opens; its doc is answered in-context. */+const HERMETIC_PROJECT = "cut-export-eval";++const CLIP_S = 3;+/** How far the probed file may drift from the doc's own duration. */+const DURATION_TOLERANCE_S = 0.5;++function run(cmd: string, cmdArgs: string[]): Promise<string> {+ return new Promise((resolve, reject) => {+ const p = spawn(cmd, cmdArgs);+ let out = "";+ let err = "";+ p.stdout.on("data", (d) => (out += d.toString()));+ p.stderr.on("data", (d) => (err += d.toString()));+ p.on("error", reject);+ p.on("close", (code) => {+ if (code !== 0) reject(new Error(`${cmd} exited ${code}: ${err.slice(-800)}`));+ resolve(out);+ });+ });+}++/** Fixture clips re-encoded from the bundled stock video, with a sine tone as+ * audio so the mix has something real to carry, plus a music bed. */+async function buildFixtures(): Promise<void> {+ await mkdir(OUT, { recursive: true });+ const sources = [+ "animal-dog-sprint.mp4",+ "animal-flock-sky.mp4",+ ].filter((f) => existsSync(path.join(STOCK, f)));+ if (sources.length === 0) throw new Error(`no stock clips found in ${STOCK}`);++ for (let i = 0; i < 2; i++) {+ const dst = path.join(OUT, `clip-${i}.mp4`);+ if (existsSync(dst)) continue;+ await run("ffmpeg", [+ "-y", "-loglevel", "error",+ "-i", path.join(STOCK, sources[i % sources.length]),+ "-f", "lavfi", "-i", `sine=frequency=${440 * (i + 1)}:sample_rate=44100:duration=${CLIP_S}`,+ "-t", String(CLIP_S),+ "-map", "0:v:0", "-map", "1:a:0",+ "-c:v", "libx264", "-preset", "veryfast", "-r", "30",+ "-vf", "scale=1280:-2", "-pix_fmt", "yuv420p",+ "-c:a", "aac",+ dst,+ ]);+ }+ const music = path.join(OUT, "music.m4a");+ if (!existsSync(music)) {+ await run("ffmpeg", [+ "-y", "-loglevel", "error",+ "-f", "lavfi", "-i", "sine=frequency=220:sample_rate=44100:duration=8",+ "-c:a", "aac",+ music,+ ]);+ }+}++/** A browser with the fixture media served from disk and the session answered. */+async function launch(): Promise<{ browser: Browser; page: Page }> {+ const browser = await chromium.launch({+ channel: "chrome",+ headless: !has("--headed"),+ args: ["--autoplay-policy=no-user-gesture-required"],+ });+ const context = await browser.newContext({+ extraHTTPHeaders: { "x-donkey-dev-auth-bypass": "1" },+ viewport: { width: 1600, height: 1000 },+ });+ if (HERMETIC) {+ // Registered first, so the specific routes below win: everything else+ // under /api answers as an empty stub and the server is never asked. The+ // engine's port answers nothing at all, so the residency probe resolves+ // the project to the cloud the way a machine without the app would.+ await context.route("**/api/**", (route, request) => {+ if (new URL(request.url()).port === "41417") return route.abort();+ return route.fulfill({ status: 200, contentType: "application/json", body: "{}" });+ });+ // "Nothing saved yet" — the onboarding gate reads this and steps aside.+ await context.route("**/api/account/onboarding", (route) =>+ route.fulfill({ status: 200, contentType: "application/json", body: "null" })+ );+ await context.route(+ (u) => /\/api\/(cut|cut-cloud)\/(projects|export-jobs|library)$/.test(u.pathname),+ (route) =>+ route.fulfill({ status: 200, contentType: "application/json", body: "[]" })+ );+ await context.route(+ (u) => new RegExp(`/api/(cut|cut-cloud)/projects/${HERMETIC_PROJECT}$`).test(u.pathname),+ (route) =>+ route.fulfill({+ status: 200,+ contentType: "application/json",+ headers: { "x-cut-doc-version": "1" },+ body: JSON.stringify({+ name: "cut-export eval",+ assets: [],+ clips: [],+ audioClips: [],+ overlays: [],+ }),+ })+ );+ }+ await context.route("**/api/auth/get-session*", async (route) => {+ const stamp = new Date(0).toISOString();+ const user = {+ id: DEV_USER,+ name: "cut-export eval",+ email: "cut-export@localhost",+ emailVerified: true,+ createdAt: stamp,+ updatedAt: stamp,+ image: null,+ };+ await route.fulfill({+ status: 200,+ contentType: "application/json",+ body: JSON.stringify({+ user,+ session: {+ id: "cut-export-session",+ userId: DEV_USER,+ token: "cut-export",+ expiresAt: new Date(Date.now() + 864e5).toISOString(),+ createdAt: stamp,+ updatedAt: stamp,+ },+ }),+ });+ });+ await context.route("**/__cutexport/*", async (route, request) => {+ const name = path.basename(new URL(request.url()).pathname);+ const body = await readFile(path.join(OUT, name));+ const type = name.endsWith(".m4a") ? "audio/mp4" : "video/mp4";+ await route.fulfill({ status: 200, contentType: type, body });+ });+ const page = await context.newPage();+ page.on("pageerror", (e) => console.log(`[pageerror] ${String(e).slice(0, 200)}`));+ page.on("crash", () => console.log("[crash] the page went down"));+ return { browser, page };+}++async function open(page: Page, projectId: string): Promise<void> {+ await page.goto(`${BASE}/app/p/${projectId}`, { waitUntil: "domcontentloaded", timeout: 120_000 });+ await page.waitForFunction(+ () => {+ const w = window as unknown as {+ __cutDev?: { useEditor: { getState(): { loaded: boolean } } };+ };+ return !!w.__cutDev && w.__cutDev.useEditor.getState().loaded;+ },+ undefined,+ { timeout: 120_000 }+ );+}++/** Render the fixture doc in the tab and hand the MP4 bytes back. */+async function renderInPage(page: Page): Promise<{ b64: string; duration: number }> {+ return page.evaluate(+ async ({ clipS }) => {+ const dev = (window as unknown as {+ __cutDev: {+ renderProjectToMp4: (+ doc: unknown,+ settings: unknown,+ opts: { resolve: (a: { url: string }) => string }+ ) => Promise<{ file: Blob; discard(): Promise<void> }>;+ projectDuration: (doc: unknown) => number;+ };+ }).__cutDev;++ const asset = (id: string, file: string, type: string, duration: number) => ({+ id,+ fileName: file,+ name: id,+ type,+ url: `/__cutexport/${file}`,+ duration,+ ...(type === "video" ? { width: 1280, height: 720 } : {}),+ });+ const doc = {+ aspect: "16:9",+ background: "#102030",+ fadeIn: 0.3,+ fadeOut: 0.3,+ assets: [+ asset("v0", "clip-0.mp4", "video", clipS),+ asset("v1", "clip-1.mp4", "video", clipS),+ asset("m0", "music.m4a", "audio", 8),+ ],+ clips: [+ {+ id: "c0", assetId: "v0", track: 0, start: 0, in: 0, out: clipS,+ muted: false, volume: 0.8,+ fit: "fill", zoom: 1.4, panX: 0.3, panY: -0.2,+ speed: 1.25,+ transition: 0.5, transitionStyle: "crossfade",+ },+ {+ id: "c1", assetId: "v1", track: 0, start: clipS / 1.25, in: 0, out: clipS,+ muted: false,+ fit: "fit",+ grade: { brightness: 5, contrast: 8, saturation: -10, temperature: 12, hue: 10 },+ kf: [+ { t: 0, x: 0.5, y: 0.5, scale: 1, rotation: 0, opacity: 1 },+ { t: 2, x: 0.6, y: 0.4, scale: 0.85, rotation: 12, opacity: 1 },+ ],+ },+ {+ id: "c2", assetId: "v0", track: 1, start: 1, in: 0, out: 1.5,+ muted: true,+ frame: { x: 0.55, y: 0.05, w: 0.4, h: 0.4 },+ fit: "fill", rotation: 5, opacity: 0.9,+ },+ ],+ audioClips: [+ {+ id: "a0", assetId: "m0", start: 0, in: 0, out: 5,+ volume: 0.5, fadeIn: 0.3, fadeOut: 0.5, lane: 0,+ },+ ],+ overlays: [+ {+ id: "t0", kind: "text", text: "Everything test",+ start: 0.5, end: 3, x: 0.5, y: 0.2, lane: 0,+ size: 64, font: "sf", weight: 700, color: "#FFFFFF",+ shadow: true, plate: true,+ },+ ],+ subtitles: {+ cues: [+ { id: "s0", start: 0.3, end: 1.2, text: "every feature" },+ { id: "s1", start: 1.4, end: 2.2, text: "in one render" },+ ],+ showOnVideo: true,+ showOnTimeline: false,+ },+ };+ const settings = { width: 1280, height: 720, fps: 24, crf: 24, preset: "veryfast" };++ const duration = dev.projectDuration(doc);+ const rendered = await dev.renderProjectToMp4(doc, settings, {+ resolve: (a) => a.url,+ });+ const bytes = new Uint8Array(await rendered.file.arrayBuffer());+ await rendered.discard();+ let bin = "";+ const CHUNK = 0x8000;+ for (let i = 0; i < bytes.length; i += CHUNK) {+ bin += String.fromCharCode(...bytes.subarray(i, i + CHUNK));+ }+ return { b64: btoa(bin), duration };+ },+ { clipS: CLIP_S }+ );+}++async function main(): Promise<void> {+ await buildFixtures();+ console.log(`[fixtures] ready in ${OUT}`);++ const { browser, page } = await launch();+ let projectId = HERMETIC_PROJECT;+ if (!HERMETIC) {+ const res = await fetch(`${BASE}/api/cut/projects?u=${DEV_USER}`, {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ name: "cut-export eval" }),+ });+ if (!res.ok) throw new Error(`create project failed: ${res.status} (is next dev running?)`);+ projectId = ((await res.json()) as { id: string }).id;+ }+ console.log(`[ready] ${BASE}/app/p/${projectId}${HERMETIC ? " (hermetic)" : ""}`);++ await open(page, projectId);+ const t0 = Date.now();+ const { b64, duration } = await renderInPage(page);+ await browser.close();++ const outPath = path.join(OUT, "export.mp4");+ await writeFile(outPath, Buffer.from(b64, "base64"));+ console.log(`[render] ${((Date.now() - t0) / 1000).toFixed(1)}s → ${outPath}`);++ const probed = JSON.parse(+ await run("ffprobe", [+ "-v", "error",+ "-show_entries", "format=duration",+ "-show_entries", "stream=codec_type,width,height",+ "-of", "json",+ outPath,+ ])+ ) as {+ format: { duration: string };+ streams: { codec_type: string; width?: number; height?: number }[];+ };++ const failures: string[] = [];+ const got = Number(probed.format.duration);+ if (!(Math.abs(got - duration) <= DURATION_TOLERANCE_S)) {+ failures.push(`duration ${got.toFixed(2)}s, doc says ${duration.toFixed(2)}s`);+ }+ const video = probed.streams.find((s) => s.codec_type === "video");+ if (video?.width !== 1280 || video?.height !== 720) {+ failures.push(`picture is ${video?.width}x${video?.height}, settings say 1280x720`);+ }+ if (!probed.streams.some((s) => s.codec_type === "audio")) {+ failures.push("no audio stream");+ }++ if (failures.length) {+ console.error(`[FAIL] ${failures.join("; ")}`);+ process.exit(1);+ }+ console.log(`[ok] ${got.toFixed(2)}s, 1280x720, video+audio`);+}++main().catch((e) => {+ console.error(e);+ process.exit(1);+});diff --git a/site/src/cut/lib/devHooks.ts b/site/src/cut/lib/devHooks.tsindex bf04ae33..80ea064e 100644--- a/site/src/cut/lib/devHooks.ts+++ b/site/src/cut/lib/devHooks.ts@@ -7,12 +7,13 @@* the editor root; a no-op in production builds.*/+import { renderProjectToMp4 } from "./exportRender";import { useGenerate } from "./generate";import { useGenScene } from "./genScene";import { enrichAsset, importFileToProject } from "./media";import { awaitingFrame, startTrace, stopTrace } from "./perfTrace";import { playheadAt } from "./playhead";-import { useEditor } from "./store";+import { projectDuration, useEditor } from "./store";export function installDevHooks(): void {if (process.env.NODE_ENV === "production" || typeof window === "undefined") return;@@ -23,6 +24,10 @@ export function installDevHooks(): void {importFileToProject,enrichAsset,playheadAt,+ // The export eval renders a doc through the tab's own pipeline and reads+ // the bytes back out.+ renderProjectToMp4,+ projectDuration,};// The perf eval arms and reads the frame trace through here. Off until// `start()` is called, so an ordinary dev session records nothing.