Open source CapCut alternative.
TypeScript
fix(cut): give project cards the timeline's length [rebuild]
David Le88955f8d7e92
diff --git a/site/src/cut/lib/backend/browser/opfs.ts b/site/src/cut/lib/backend/browser/opfs.tsindex 4668e4d5..ff1ab461 100644--- a/site/src/cut/lib/backend/browser/opfs.ts+++ b/site/src/cut/lib/backend/browser/opfs.ts@@ -19,6 +19,7 @@// storage access, because this file rides along in the engine bundle.import { registerBlobFile } from "./registry";import { isDeliveryName } from "../../exportDelivery";+import { timelineEnd } from "../../timelineEnd";import type { ProjectDoc, ProjectFolder, ProjectSummary } from "../../types";import { currentEngineUser } from "../../api";@@ -235,7 +236,7 @@ export async function summarize(id: string, doc: ProjectDoc): Promise<ProjectSumname: doc.name,createdAt: doc.createdAt,updatedAt: doc.updatedAt,- duration: track0.reduce((sum, c) => sum + Math.max(0, c.out - c.in), 0),+ duration: timelineEnd(doc),clipCount: track0.length,assetCount: doc.assets.length,previewFile: previewAsset?.fileName,diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.tsindex 5a7c94df..001ba6cf 100644--- a/site/src/cut/lib/store.ts+++ b/site/src/cut/lib/store.ts@@ -1,6 +1,7 @@"use client";import { noteProjectRevision } from "./projectRevision";import { freeItemName } from "./itemName";+import { clipLen, timelineEnd } from "./timelineEnd";import { EMPTY_GUIDE_LINES, sanitizeGuideLines, sanitizeGuides, type GuideId, type GuideLines } from "./guides";import {@@ -5809,11 +5810,7 @@ export function clipSpeed(c: VideoClip) {return s > 0 ? s : 1;}-/** Timeline footprint of a clip: its source span through its rate, or through- * its speed curve when it carries one (see retimeOf). */-export function clipLen(c: VideoClip | AudioClip) {- return Math.max(MIN_LEN, retimeOf(c).len);-}+export { clipLen };/** The longest fade a clip takes: half its footprint, so a fade in and a* fade out never overlap. Tenths of a second, like the controls that set it. */@@ -6781,14 +6778,7 @@ export function projectDuration(s: {durClips = s.clips;durAudio = s.audioClips;durOverlays = s.overlays;- let end = 0;- // Anything on any row extends the timeline: a layer or soundtrack running- // past track 0's end is still reachable, and so is a title or sticker left- // standing after the last clip — it plays over black.- for (const c of s.clips) end = Math.max(end, c.start + clipLen(c));- for (const a of s.audioClips) end = Math.max(end, a.start + clipLen(a));- for (const o of s.overlays) end = Math.max(end, o.end);- durValue = Math.max(0, end);+ durValue = timelineEnd(s);return durValue;}diff --git a/site/src/cut/lib/timelineEnd.ts b/site/src/cut/lib/timelineEnd.tsnew file mode 100644index 00000000..72f9bf99--- /dev/null+++ b/site/src/cut/lib/timelineEnd.ts@@ -0,0 +1,24 @@+import { retimeOf } from "@donkeycut/effects-kit";+import { CLIP_MIN_SECONDS, type AudioClip, type Overlay, type VideoClip } from "./types";++/** Timeline footprint of a clip: its source span through its rate, or through+ * its speed curve when it carries one (see retimeOf). */+export function clipLen(c: VideoClip | AudioClip) {+ return Math.max(CLIP_MIN_SECONDS, retimeOf(c).len);+}++/** Where a timeline ends. Anything on any row extends it: a layer or+ * soundtrack running past track 0's end is still reachable, and so is a title+ * or sticker left standing after the last clip — it plays over black. Lists a+ * stored doc lacks count as empty. */+export function timelineEnd(s: {+ clips?: VideoClip[];+ audioClips?: AudioClip[];+ overlays?: Overlay[];+}): number {+ let end = 0;+ for (const c of s.clips ?? []) end = Math.max(end, c.start + clipLen(c));+ for (const a of s.audioClips ?? []) end = Math.max(end, a.start + clipLen(a));+ for (const o of s.overlays ?? []) end = Math.max(end, o.end);+ return Math.max(0, end);+}diff --git a/site/src/cut/server/cloud/projects.ts b/site/src/cut/server/cloud/projects.tsindex 5d69ace3..6fa80336 100644--- a/site/src/cut/server/cloud/projects.ts+++ b/site/src/cut/server/cloud/projects.ts@@ -2,6 +2,7 @@// docs and metadata in Postgres, media bytes in R2. Every query scopes by userId.import { sanitizeGuideLines, sanitizeGuides } from "@/cut/lib/guides";import { resolveParent, subtreeOf } from "@/cut/lib/folderTree";+import { timelineEnd } from "@/cut/lib/timelineEnd";import { isTimelineId, normalizeAspect, sanitizeTimelines, type ProjectDoc, type ProjectFolder, type ProjectSummary } from "@/cut/lib/types";import type { Prisma } from "@/generated/prisma/client";import { prisma } from "@/lib/prisma";@@ -63,7 +64,7 @@ function summarize(row: ProjectRow, sizeBytes: number): ProjectSummary {const clips = Array.isArray(doc.clips) ? doc.clips : [];const assets = Array.isArray(doc.assets) ? doc.assets : [];const track0 = clips.filter((c) => (c.track ?? 0) === 0);- const duration = track0.reduce((sum, c) => sum + Math.max(0, c.out - c.in), 0);+ const duration = timelineEnd(doc);const firstClip = track0[0];const firstClipAsset = firstClip ? assets.find((a) => a.id === firstClip.assetId) : undefined;const previewAsset =diff --git a/site/src/cut/server/projectSummary.test.ts b/site/src/cut/server/projectSummary.test.tsnew file mode 100644index 00000000..f8252109--- /dev/null+++ b/site/src/cut/server/projectSummary.test.ts@@ -0,0 +1,31 @@+import { afterAll, expect, test } from "bun:test";+import { mkdtemp, rm } from "node:fs/promises";+import os from "node:os";+import path from "node:path";+import type { ProjectDoc } from "@/cut/lib/types";++// The engine's data root is read per call, so a temp root set before the+// module loads keeps the test off the user's own Movies folder.+const root = await mkdtemp(path.join(os.tmpdir(), "cut-summary-test-"));+process.env.DONKEY_CUT_DATA_DIR = root;+const projects = await import("./projects");+afterAll(async () => { await rm(root, { recursive: true, force: true }); });++test("a project card's length is where its timeline ends", async () => {+ // Two 1 s clips with a 2 s gap between them, the second at double speed,+ // and a title standing after both: the timeline runs to 6 s.+ const { id } = await projects.createProject("Card Length");+ const doc = (await projects.readProject(id)) as ProjectDoc;+ const clip = { assetId: "a", in: 0, out: 1, track: 0 };+ await projects.writeProject(id, {+ ...doc,+ clips: [+ { ...clip, id: "c1", start: 0 },+ { ...clip, id: "c2", start: 3, out: 2, speed: 2 },+ ],+ overlays: [{ id: "t", kind: "text", start: 5, end: 6 }],+ } as unknown as ProjectDoc);++ const card = (await projects.listProjects()).find((p) => p.id === id);+ expect(card?.duration).toBeCloseTo(6, 5);+});diff --git a/site/src/cut/server/projects.ts b/site/src/cut/server/projects.tsindex 230e6fa6..78288ff5 100644--- a/site/src/cut/server/projects.ts+++ b/site/src/cut/server/projects.ts@@ -3,6 +3,7 @@ import { isDeliveryName } from "../lib/exportDelivery";import { cp, mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises";import path from "node:path";import { resolveParent, settleParents, subtreeOf } from "@/cut/lib/folderTree";+import { timelineEnd } from "@/cut/lib/timelineEnd";import type { Aspect, ProjectDoc, ProjectFolder, ProjectSummary } from "@/cut/lib/types";import { cutDataRoot } from "./dataDir";import { assertLocalRuntime } from "./local-only";@@ -421,7 +422,7 @@ async function summarize(id: string, doc: ProjectDoc): Promise<ProjectSummary> {// the summary's duration, count, or poster. Legacy docs carry no `track` on// clips — those are all track 0.const track0 = doc.clips.filter((c) => (c.track ?? 0) === 0);- const duration = track0.reduce((sum, c) => sum + Math.max(0, c.out - c.in), 0);+ const duration = timelineEnd(doc);// Preview: the first clip's source video, else any video asset.const firstClip = track0[0];const firstClipAsset = firstClip