Open source CapCut alternative.
TypeScript
fix(site): media reads hold on small sources and async metadata
David Le181b55725b6f
diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.tsindex 4ddd5e38..81c82a47 100644--- a/site/src/cut/lib/media.ts+++ b/site/src/cut/lib/media.ts@@ -1028,10 +1028,13 @@ export async function makeStillFrame(sourceUrl: string): Promise<WatchFrames> {if (!ctx) throw new Error("Could not read the image.");ctx.imageSmoothingQuality = "high";ctx.drawImage(img.source, 0, 0, w, h);+ // A picture read through an element (a host that sends no CORS header)+ // taints the canvas, and the encode is where that surfaces.+ const image = await rasterCanvasToDataUrl(canvas, "image/jpeg", SHEET_QUALITY).catch(() => {+ throw new Error("Could not read the image.");+ });return {- frames: [- { t: 0, image: await rasterCanvasToDataUrl(canvas, "image/jpeg", SHEET_QUALITY), via: "first" },- ],+ frames: [{ t: 0, image, via: "first" }],candidates: 1,sceneChanges: [],coveredTo: 0,@@ -1143,7 +1146,7 @@ export async function sampleWatchFrames(const copy = createRasterCanvas(cw, ch);const cctx = copy.getContext("2d") as CanvasRenderingContext2D | null;if (!cctx) throw new Error("Could not sample the video.");- cctx.drawImage(next.value.canvas as CanvasImageSource, 0, 0);+ cctx.drawImage(next.value.canvas as CanvasImageSource, 0, 0, cw, ch);held.set(candTimes.length, copy);}candTimes.push(times[i]);diff --git a/site/src/cut/lib/mediaRead.ts b/site/src/cut/lib/mediaRead.tsindex 005f43fc..c64a8615 100644--- a/site/src/cut/lib/mediaRead.ts+++ b/site/src/cut/lib/mediaRead.ts@@ -193,10 +193,21 @@ export function frameSink(function capToTrack(track: InputVideoTrack, size?: FrameSize): FrameSize | undefined {if (!size) return size;+ // A backing that resolves its metadata asynchronously — HLS, a segmented+ // input — refuses the synchronous read. The sink is sync, so those sources+ // decode at the size asked for.+ let tw: number;+ let th: number;+ try {+ tw = track.displayWidth;+ th = track.displayHeight;+ } catch {+ return size;+ }return {...size,- ...(size.width !== undefined ? { width: Math.min(size.width, track.displayWidth) } : {}),- ...(size.height !== undefined ? { height: Math.min(size.height, track.displayHeight) } : {}),+ ...(size.width !== undefined ? { width: Math.min(size.width, tw) } : {}),+ ...(size.height !== undefined ? { height: Math.min(size.height, th) } : {}),};}diff --git a/site/src/cut/lib/raster.ts b/site/src/cut/lib/raster.tsindex 1cc9c024..dae2ef79 100644--- a/site/src/cut/lib/raster.ts+++ b/site/src/cut/lib/raster.ts@@ -97,8 +97,8 @@ function elementImage(url: string): Promise<RasterImage | null> {if (typeof document === "undefined") return Promise.resolve(null);return new Promise((resolve) => {const img = new Image();- img.onload = () =>- resolve({ source: img, width: img.naturalWidth || 512, height: img.naturalHeight || 512 });+ // The intrinsic size as reported: 0 is how a caller learns it is unknown.+ img.onload = () => resolve({ source: img, width: img.naturalWidth, height: img.naturalHeight });img.onerror = () => resolve(null);img.src = url;});