Open source CapCut alternative.
TypeScript
feat(site): media probes and uploads run off the DOM
David Le9061d6a228af
diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.tsindex a8b0173f..c0f2a88b 100644--- a/site/src/cut/lib/media.ts+++ b/site/src/cut/lib/media.ts@@ -19,6 +19,7 @@ import {UnreadableMediaError,videoTrackOf,} from "./mediaRead";+import { decodeRasterImage } from "./raster";import { useEditor } from "./store";import type { AssetType, AudioClip, MediaAsset, ProjectSummary, StoredAsset, VideoClip, WatchKeepReason } from "./types";import { IMAGE_CLIP_SECONDS, mediaUrl } from "./types";@@ -163,15 +164,26 @@ export async function presignedUpload(return signed.key;}-/** PUT a blob to a presigned R2 URL. XHR rather than fetch: it is the only way- * to watch the bytes leave, which is what an upload running behind the editor- * has to report. */+/** PUT a blob to a presigned R2 URL. XHR because it is the only way to watch+ * the bytes leave, which is what an upload running behind the editor has to+ * report. A headless runtime has no XHR and nobody watching; fetch covers it. */export function putSigned(url: string,file: Blob,mime?: string,opts?: { onProgress?: (fraction: number) => void; signal?: AbortSignal }): Promise<void> {+ if (typeof XMLHttpRequest === "undefined") {+ return fetch(url, {+ method: "PUT",+ headers: { "Content-Type": mime ?? (file.type || "application/octet-stream") },+ body: file,+ signal: opts?.signal,+ }).then((res) => {+ if (!res.ok) throw new Error("Upload failed.");+ opts?.onProgress?.(1);+ });+ }return new Promise((resolve, reject) => {if (opts?.signal?.aborted) return reject(new DOMException("Upload cancelled.", "AbortError"));const xhr = new XMLHttpRequest();@@ -318,6 +330,13 @@ async function probeMedia(type: AssetType, src: string | Blob): Promise<ProbedMe// Images have no intrinsic duration; the timeline clip carries its length.// Stills are the one kind read through the browser's image decoder rather// than the container reader, which does not do them.+ if (typeof Image === "undefined") {+ // Headless runtime: decode through the raster seam (skia in the worker).+ const blob = typeof src === "string" ? await (await fetch(src)).blob() : src;+ const img = await decodeRasterImage(blob);+ if (!img) throw new UnreadableMediaError();+ return { type, duration: 0, width: img.width, height: img.height };+ }const url = typeof src === "string" ? src : URL.createObjectURL(src);try {const dims = await loadImageMeta(url);@@ -431,7 +450,9 @@ export async function importFileToProject(const fileName = await uploadProjectMediaTo(backend, projectId, file, file.name);const url = mediaUrl(projectId, fileName, backend);- const meta = await probeMedia(type, url);+ // Probe the bytes in hand: the same file the upload just wrote, with no+ // second network read — which also lets a headless process import media.+ const meta = await probeMedia(type, file);return {id: uid(),fileName,diff --git a/site/src/cut/lib/mediaRead.ts b/site/src/cut/lib/mediaRead.tsindex 8613a2ca..fdcae2df 100644--- a/site/src/cut/lib/mediaRead.ts+++ b/site/src/cut/lib/mediaRead.ts@@ -83,12 +83,21 @@ export async function withMedia<T>(src: string | Blob, fn: (input: Input) => Pro}}+// A headless runtime (the turn runner) has no page, and there the+// decodability gate is not a gate: nothing plays in that process, rendering+// goes through ffmpeg, and the probe only reads container metadata — which+// parses fine. Tracks pass headless; every browser keeps the strict check,+// including one without WebCodecs, where canDecode answers false and the+// undecodable-import guard still refuses footage the page can't play.+const headless = () => typeof document === "undefined";+/** The primary video track, or null when the file has no readable one — either* no video at all, or video in a codec this browser can't decode. Callers that* need to tell those apart ask `hasUndecodableVideo`. */export async function videoTrackOf(input: Input): Promise<InputVideoTrack | null> {const track = await input.getPrimaryVideoTrack();- return track && (await track.canDecode()) ? track : null;+ if (!track) return null;+ return headless() || (await track.canDecode()) ? track : null;}/** True when the file carries video this browser cannot decode.@@ -98,6 +107,7 @@ export async function videoTrackOf(input: Input): Promise<InputVideoTrack | null* readable video track, and calling *that* audio drops the user's footage onto* the timeline as a waveform with no explanation. */export async function hasUndecodableVideo(input: Input): Promise<boolean> {+ if (headless()) return false;const track = await input.getPrimaryVideoTrack();return !!track && !(await track.canDecode());}@@ -105,7 +115,8 @@ export async function hasUndecodableVideo(input: Input): Promise<boolean> {/** The primary audio track, or null — same decodability rule as video. */export async function audioTrackOf(input: Input): Promise<InputAudioTrack | null> {const track = await input.getPrimaryAudioTrack();- return track && (await track.canDecode()) ? track : null;+ if (!track) return null;+ return headless() || (await track.canDecode()) ? track : null;}/** Read what a file is: how long, whether it carries picture or sound, and at@@ -252,6 +263,9 @@ export async function decodeAudioSpan(* of float samples to draw a strip a few hundred pixels wide.*/export async function audioPeaks(src: string | Blob, buckets: number): Promise<number[]> {+ // Peaks are a real decode; a headless runtime skips them and the waveform+ // enriches the next time a browser holds the asset.+ if (typeof AudioDecoder === "undefined") return [];const input = openMedia(src);try {const track = await audioTrackOf(input);