Open source CapCut alternative.
TypeScript
fix(site): a phone's library upload carries its length and size
David Lefa438ff3bcf0
diff --git a/apps/ios/Donkey/Controllers/CutCloudClient.swift b/apps/ios/Donkey/Controllers/CutCloudClient.swiftindex 240cb95b..0b595951 100644--- a/apps/ios/Donkey/Controllers/CutCloudClient.swift+++ b/apps/ios/Donkey/Controllers/CutCloudClient.swift@@ -205,13 +205,17 @@ extension CutCloudClient: CloudSyncServicing {} else {nil}+ // The row carries the footage's length and size: the desktop cuts+ // clips to them and sizes the card from them. A camera-roll import+ // arrives unmeasured, so the file itself answers here.+ let measured = await Self.measure(upload)var meta: [String: Any] = ["name": upload.name,"type": upload.type,- "duration": upload.duration,+ "duration": measured.duration,"origin": upload.origin,]- if let width = upload.width, let height = upload.height {+ if let width = measured.width, let height = measured.height {meta["width"] = widthmeta["height"] = height}@@ -241,6 +245,39 @@ extension CutCloudClient: CloudSyncServicing {return try decode(PresignResponse.self, from: data)}+ private struct Measured {+ var duration: TimeInterval+ var width: Int?+ var height: Int?+ }++ /// What the upload already says about its media, with the file filling in+ /// whatever is missing: a video's length and its displayed size (rotation+ /// applied), a photo's pixel size.+ private static func measure(_ upload: LibraryUpload) async -> Measured {+ var out = Measured(duration: upload.duration, width: upload.width, height: upload.height)+ let needsSize = out.width == nil || out.height == nil+ if upload.type == "video" {+ let asset = AVURLAsset(url: upload.fileURL)+ if out.duration <= 0,+ let seconds = try? await asset.load(.duration).seconds,+ seconds.isFinite, seconds > 0 {+ out.duration = seconds+ }+ if needsSize,+ let track = try? await asset.loadTracks(withMediaType: .video).first,+ let (natural, transform) = try? await track.load(.naturalSize, .preferredTransform) {+ let rect = CGRect(origin: .zero, size: natural).applying(transform)+ out.width = Int(abs(rect.width).rounded())+ out.height = Int(abs(rect.height).rounded())+ }+ } else if needsSize, let image = UIImage(contentsOfFile: upload.fileURL.localPath) {+ out.width = Int((image.size.width * image.scale).rounded())+ out.height = Int((image.size.height * image.scale).rounded())+ }+ return out+ }+/// The recording's thumbnail goes up beside the movie so the desktop card/// paints without decoding video. Best-effort: the upload stands without it.private func uploadPoster(_ poster: Data, besides fileName: String) async throws -> String {diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swiftindex 40d71e0e..e16ea426 100644--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift@@ -780,6 +780,8 @@ public final class SyncEngine {name: fileName,type: isVideo ? "video" : "image",duration: 0,+ width: item.localWidth,+ height: item.localHeight,origin: "inspiration",resume: true)diff --git a/site/src/cut/lib/library.ts b/site/src/cut/lib/library.tsindex 3d04b245..468e18ce 100644--- a/site/src/cut/lib/library.ts+++ b/site/src/cut/lib/library.ts@@ -27,7 +27,9 @@ import {listedResidencies,type Residency,} from "./residency";+import { probeMediaFile } from "./mediaRead";import { storedMediaUrl } from "./mediaSync";+import { reportSwallowed } from "./report";import { useEditor } from "./store";import { playheadAt } from "./playhead";import type {@@ -720,6 +722,21 @@ export async function importLibraryAsset(// The project's media takes the name the shelf shows, so a phone// recording lands on the timeline under its title.const name = lib.title || lib.name;+ // A row uploaded unmeasured — a phone's camera-roll import carries no+ // length or size — is read here, before the clip is cut from it, so the+ // clip takes the footage's true length and the row learns it.+ let { duration, width, height } = lib;+ if (lib.type === "video" && !(duration > 0)) {+ const probe = await probeMediaFile(libraryMediaUrl(lib.fileName, lib.residency));+ duration = probe.duration;+ if (probe.width && probe.height) {+ width = probe.width;+ height = probe.height;+ }+ repairLibraryAssetMeta(lib, { duration, width, height }).catch((err: unknown) =>+ reportSwallowed(`[cut] library meta repair failed for ${lib.fileName}`, err),+ );+ }// On this Mac the engine lands the file with a clone, and it is landed// before this returns: the asset registers at the project's own address// and plays from there. Registered on the shelf's address first, the@@ -738,9 +755,9 @@ export async function importLibraryAsset(fileName,name,type: lib.type,- duration: lib.duration,- width: lib.width,- height: lib.height,+ duration,+ width,+ height,});}return importRemote(@@ -750,9 +767,9 @@ export async function importLibraryAsset(name,fileName: lib.fileName,type: lib.type,- duration: lib.duration,- width: lib.width,- height: lib.height,+ duration,+ width,+ height,server: sameShelf(lib.residency),},(opts) =>@@ -766,6 +783,24 @@ export async function importLibraryAsset();}+/** Write the length and size a probe read off a file back onto the shelf row+ * that was uploaded without them, so the card's time pill and the drop+ * preview show the footage's real length from the next listing on. The+ * browser shelf and the engine measure at ingest; only the cloud shelf takes+ * unmeasured rows, from the phone. */+async function repairLibraryAssetMeta(+ lib: LibraryAsset,+ meta: { duration: number; width?: number; height?: number },+): Promise<void> {+ if (lib.residency !== "cloud" || !(meta.duration > 0)) return;+ const res = await cloudBackend.fetch(`/api/cut/library/${encodeURIComponent(lib.id)}`, {+ method: "PUT",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify(meta),+ });+ if (!res.ok) throw new Error((await apiJson<unknown>(res)).error ?? "Could not update the library asset.");+}+/** Copy a library asset into the open project and add it to the timeline at* the playhead. */export async function addLibraryAssetToProject(diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.tsindex 2de4fe7e..2b979261 100644--- a/site/src/cut/lib/media.ts+++ b/site/src/cut/lib/media.ts@@ -756,6 +756,13 @@ export function importRemote(if (init.type === "video") {void probeMediaFile(init.url).then((m) => {+ if (!(init.duration > 0)) {+ useEditor.getState().repairAssetDuration(asset.id, {+ duration: m.duration,+ width: m.width,+ height: m.height,+ });+ }if (m.hasVideo) {useEditor.getState().updateAsset(asset.id, { width: m.width, height: m.height });}@@ -1683,6 +1690,20 @@ export async function enrichAsset(asset: MediaAsset, src = asset.url) {// A play is long enough to leave the project in it. Nothing this sweep// writes belongs to whatever is open now.if (useEditor.getState().projectId !== openProject) return;+ // Footage stored without a length — a shelf row that was never measured+ // — reads its own before anything is swept from it: a zero-length sweep+ // has no tiles to place and no buckets to fill, and the clips cut from+ // it open to the footage.+ if (!(asset.duration > 0)) {+ const probe = await probeMediaFile(held ?? src);+ useEditor.getState().repairAssetDuration(asset.id, {+ duration: probe.duration,+ width: probe.width,+ height: probe.height,+ });+ asset = useEditor.getState().assets.find((a) => a.id === asset.id) ?? asset;+ if (!(asset.duration > 0)) throw new UnreadableMediaError("The video has no length.");+ }// The clip box draws the sound under the picture, so a video enriches// waveform peaks alongside its filmstrip. The two are separate reads of// the file, so the peaks decode starts here and runs beside the strip'sdiff --git a/site/src/cut/lib/store.test.ts b/site/src/cut/lib/store.test.tsindex bcac4d13..aaece8b8 100644--- a/site/src/cut/lib/store.test.ts+++ b/site/src/cut/lib/store.test.ts@@ -2017,3 +2017,36 @@ describe("speed curves", () => {expect(s.clips[0].speedCurve?.map((n) => n[0])).toEqual([0, 2]);});});++describe("repairAssetDuration", () => {+ test("gives an unmeasured video its length and opens its zero-length clips to the footage", () => {+ const a = asset(0);+ const other = asset(4);+ useEditor.setState({+ assets: [a, other],+ clips: [+ vclip({ id: "empty", assetId: a.id, start: 0, in: 0, out: 0 }),+ vclip({ id: "next", assetId: other.id, start: 3, in: 0, out: 2 }),+ vclip({ id: "upper", assetId: a.id, track: 1, start: 0, in: 0, out: 0 }),+ ],+ });+ s().repairAssetDuration(a.id, { duration: 10, width: 1080, height: 1920 });+ const repaired = s().assets.find((x) => x.id === a.id)!;+ expect(repaired.duration).toBe(10);+ expect(repaired.width).toBe(1080);+ expect(repaired.height).toBe(1920);+ // Held short of the next clip on track 0; the whole footage on an open track.+ expect(clipById("empty").out).toBe(3);+ expect(clipById("upper").out).toBe(10);+ expect(clipById("next").out).toBe(2);+ expectLaneSound(videoLane(0));+ });++ test("leaves a measured asset and its clips alone", () => {+ const a = asset(4);+ useEditor.setState({ assets: [a], clips: [vclip({ id: "c", assetId: a.id, out: 2 })] });+ s().repairAssetDuration(a.id, { duration: 10 });+ expect(s().assets[0].duration).toBe(4);+ expect(clipById("c").out).toBe(2);+ });+});diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.tsindex ddf1f196..1faadf5f 100644--- a/site/src/cut/lib/store.ts+++ b/site/src/cut/lib/store.ts@@ -381,6 +381,15 @@ export interface EditorState {setBackground: (hex: string) => void;addAsset: (asset: MediaAsset) => void;updateAsset: (id: string, patch: Partial<MediaAsset>) => void;+ /** Give a video asset stored without a length the duration and size a probe+ * read off its file, and open every zero-length clip cut from it to the+ * footage — up to the next clip on its track, so nothing overlaps. A doc+ * repair: no history checkpoint, and an asset that already has a length is+ * left alone. */+ repairAssetDuration: (+ id: string,+ meta: { duration: number; width?: number; height?: number }+ ) => void;/** Swap asset URLs in place (fileName -> url), e.g. after re-minting an* expired signed batch. Runtime-only — the stored projection omits url —* and allowed in read-only shared views. */@@ -2331,6 +2340,39 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {}));},+ repairAssetDuration: (id, meta) => {+ const st = get();+ const asset = st.assets.find((a) => a.id === id);+ if (!asset || asset.type !== "video" || asset.duration > 0 || !(meta.duration > 0)) return;+ if (st.readOnly) return;+ const size = meta.width && meta.height ? { width: meta.width, height: meta.height } : {};+ set((s) => {+ const empty = (c: VideoClip) => c.assetId === id && c.out - c.in <= 0;+ let clips = s.clips;+ if (s.clips.some(empty)) {+ clips = s.clips.map((c) => {+ if (!empty(c)) return c;+ // The whole footage, held short of the next clip on the track.+ const next = s.clips.reduce(+ (acc, o) =>+ o.id !== c.id && o.track === c.track && o.start > c.start + 1e-9+ ? Math.min(acc, o.start)+ : acc,+ Infinity+ );+ const room = next === Infinity ? meta.duration : next - c.start;+ return { ...c, in: 0, out: Math.max(MIN_LEN, Math.min(meta.duration, room)) };+ });+ }+ return {+ assets: s.assets.map((a) =>+ a.id === id ? { ...a, duration: meta.duration, ...size } : a+ ),+ ...(clips !== s.clips ? { clips } : {}),+ };+ });+ },+applyMediaUrls: (urls) => {hydrating = true;try {diff --git a/site/src/cut/server/cloud/library.ts b/site/src/cut/server/cloud/library.tsindex ec99a61a..850e83e3 100644--- a/site/src/cut/server/cloud/library.ts+++ b/site/src/cut/server/cloud/library.ts@@ -571,6 +571,45 @@ export const libraryCloud = {}},+ /** Fill in what a row was uploaded without: the duration and pixel size the+ * page read from the file the first time it cut a clip from the asset. A+ * value the row already holds stays. */+ async updateMeta(userId: string, id: string, req: Request) {+ try {+ const body = (await req.json()) as {+ duration?: number;+ width?: number;+ height?: number;+ };+ const asset = await prisma.cutLibraryAsset.findFirst({+ where: { id, userId, deletedAt: null },+ });+ if (!asset) throw new Error("Library asset not found.");+ const obj = await prisma.cutMediaObject.findUnique({+ where: { id: asset.mediaObjectId },+ });+ if (!obj) throw new Error("Library asset not found.");+ const positive = (v: unknown) =>+ typeof v === "number" && Number.isFinite(v) && v > 0 ? v : undefined;+ const meta = { ...((asset.meta ?? {}) as AssetMeta) };+ const duration = positive(body.duration);+ const width = positive(body.width);+ const height = positive(body.height);+ if (duration !== undefined && !positive(meta.duration)) meta.duration = duration;+ if (width !== undefined && height !== undefined && !positive(meta.width)) {+ meta.width = width;+ meta.height = height;+ }+ await prisma.cutLibraryAsset.update({+ where: { id },+ data: { meta: meta as unknown as Prisma.InputJsonValue },+ });+ return Response.json(assetView({ ...asset, meta }, obj));+ } catch (e) {+ return caught(e, "Could not update the library asset.");+ }+ },+/** Move an item — asset or template — into a folder (or `null` to ungroup). */async move(userId: string, req: Request) {try {diff --git a/site/src/cut/server/cloud/routes.ts b/site/src/cut/server/cloud/routes.tsindex bca19119..f9dc845b 100644--- a/site/src/cut/server/cloud/routes.ts+++ b/site/src/cut/server/cloud/routes.ts@@ -87,6 +87,7 @@ const CUT_CLOUD_ROUTES: CloudRoute[] = [{ method: "PUT", path: "/api/cut-cloud/library/folders/:id", handler: (r, u, p) => libraryCloud.updateFolder(u, p.id, r) },{ method: "DELETE", path: "/api/cut-cloud/library/folders/:id", handler: (_r, u, p) => libraryCloud.deleteFolder(u, p.id) },{ method: "GET", path: "/api/cut-cloud/library/media/:file", handler: (r, u, p) => libraryCloud.serveMedia(u, p.file, wantsDownload(r)) },+ { method: "PUT", path: "/api/cut-cloud/library/:id", handler: (r, u, p) => libraryCloud.updateMeta(u, p.id, r) },{ method: "DELETE", path: "/api/cut-cloud/library/:id", handler: (_r, u, p) => libraryCloud.remove(u, p.id) },{ method: "GET", path: "/api/cut-cloud/export-jobs", handler: (_r, u) => jobsCloud.exportFeed(u) },