Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): project readers resolve live urls through one helper

David Ledaa7cd986a8d
diff --git a/site/src/cut/lib/exportClient.ts b/site/src/cut/lib/exportClient.ts
index 6a682777..b07e610c 100644
--- a/site/src/cut/lib/exportClient.ts
+++ b/site/src/cut/lib/exportClient.ts
@@ -10,7 +10,7 @@ import { exportsDir, readFileAt, saveExport } from "./backend/browser/opfs";
import { holdRegistered, registerBlobFile, releaseRegistered } from "./backend/browser/registry";
import { quotaErrorMessage } from "./backend/cloud";
import { downloadFromUrl } from "./download";
-import { ClipReader, renderProjectToMp4 } from "./exportRender";
+import { renderProjectToMp4 } from "./exportRender";
import { putSigned } from "./media";
import { renderRemovalPieces } from "./removalVideo";
import { createRasterCanvas, rasterCanvasToPng } from "./raster";
@@ -19,6 +19,7 @@ import { captionStyle, cueOverlay, cueWordFrames, laneCues, laneHidden, subtitle
import { chromaAlphaInto, isMaskAnimated, isOverlayAnimated, matteLumaToAlpha, normalizeGrade, paintMaskLuma, paintStrokeInk } from "@donkeycut/effects-kit";
import { renderElementFrames, renderElementPng } from "./textRender";
import { clipCovers, clipKeyed, clipPosed, clipPoseAt, clipZoom, contentRect, frameOf, isStickerOverlay, isTextOverlay, laneOf, overlayAnimStyle, projectBackground, rectOf, regionPx, removalActive, shadowInk, subjectMasked } from "./types";
+import { liveReader } from "./liveReader";
import type {
Aspect,
AudioClip,
@@ -337,7 +338,7 @@ function removalShadowSilhouette(
if (r!.mode === "chroma") {
const key = r!.chroma;
if (!key) return null;
- const reader = new ClipReader(asset, () => asset.url);
+ const reader = liveReader(asset);
return {
at: async (tLocal) => {
const frame = await reader.frameAt(still ? 0 : clip.in + tLocal * speed);
@@ -363,7 +364,7 @@ function removalShadowSilhouette(
}
const matte = r!.matte ? assets.find((a) => a.id === r!.matte!.assetId) : undefined;
if (!matte) return null;
- const reader = new ClipReader(matte, () => matte.url);
+ const reader = liveReader(matte);
const mdur = Math.max(0.1, matte.duration || 0.1);
return {
at: async (tLocal) => {
diff --git a/site/src/cut/lib/liveReader.ts b/site/src/cut/lib/liveReader.ts
new file mode 100644
index 00000000..ba81de45
--- /dev/null
+++ b/site/src/cut/lib/liveReader.ts
@@ -0,0 +1,20 @@
+"use client";
+
+/**
+ * The one way to open a reader on a project asset. The URL resolves against
+ * the open doc on every open — and again on a re-open — so a link re-minted
+ * while a long job runs (an import landing in project storage, a signed link
+ * refreshed ahead of expiry) is picked up rather than the job dying on the
+ * snapshot's expired one.
+ */
+
+import { ClipReader } from "./exportRender";
+import { useEditor } from "./store";
+import type { MediaAsset } from "./types";
+
+export function liveReader(asset: MediaAsset): ClipReader {
+ return new ClipReader(
+ asset,
+ () => useEditor.getState().assets.find((a) => a.id === asset.id)?.url ?? asset.url
+ );
+}
diff --git a/site/src/cut/lib/maskVideo.ts b/site/src/cut/lib/maskVideo.ts
index a3424b20..631432f8 100644
--- a/site/src/cut/lib/maskVideo.ts
+++ b/site/src/cut/lib/maskVideo.ts
@@ -28,6 +28,7 @@ import type { ExportDoc } from "./exportClient";
import { createRasterCanvas } from "./raster";
import { getClipSpans } from "./store";
import { clipCovers, frameOf, isEffectOverlay, isTextOverlay, rectOf, subjectMasked, type ClipSpan, type MediaAsset } from "./types";
+import { liveReader } from "./liveReader";
/** Encoded mask rate — the server's fps filter duplicates frames up to the
* output rate, and a person moves little in 1/15s. */
@@ -100,7 +101,7 @@ export async function renderSubjectMask(
const readers = new Map<string, ClipReader>();
const readerFor = (asset: MediaAsset) => {
let r = readers.get(asset.id);
- if (!r) readers.set(asset.id, (r = new ClipReader(asset, () => asset.url)));
+ if (!r) readers.set(asset.id, (r = liveReader(asset)));
return r;
};
diff --git a/site/src/cut/lib/removal/keyColor.ts b/site/src/cut/lib/removal/keyColor.ts
index eb2edf3b..cc265e79 100644
--- a/site/src/cut/lib/removal/keyColor.ts
+++ b/site/src/cut/lib/removal/keyColor.ts
@@ -1,9 +1,9 @@
"use client";
-import { ClipReader } from "../exportRender";
import { sampleClipFrameData } from "../previewCanvas";
import { createRasterCanvas } from "../raster";
import type { MediaAsset, VideoClip } from "../types";
+import { liveReader } from "../liveReader";
const SAMPLE_W = 96;
const SAMPLE_H = 54;
@@ -45,7 +45,7 @@ export function suggestKeyColor(clipId: string): string | null {
* for callers with no live decoder on the clip (the chat tool, a clip away
* from the playhead). Null when the picture can't be read. */
export async function clipKeyColor(asset: MediaAsset, clip: VideoClip): Promise<string | null> {
- const reader = new ClipReader(asset, () => asset.url);
+ const reader = liveReader(asset);
try {
const frame = await reader.frameAt(asset.type === "image" ? 0 : Math.max(0, clip.in));
if (frame.kind !== "ready") return null;
diff --git a/site/src/cut/lib/removal/localBake.ts b/site/src/cut/lib/removal/localBake.ts
index 79309fb4..c175ee3e 100644
--- a/site/src/cut/lib/removal/localBake.ts
+++ b/site/src/cut/lib/removal/localBake.ts
@@ -12,9 +12,9 @@
import { MATTE_BITRATE, MATTE_FPS, MATTE_SHORT } from "@donkeycut/effects-kit";
import { openCanvasVideo, scaledEvenSize } from "../canvasVideo";
import { personSegmenter, segmentSubjectAlpha } from "../cutout";
-import { ClipReader } from "../exportRender";
import { createRasterCanvas } from "../raster";
import type { MediaAsset, VideoClip } from "../types";
+import { liveReader } from "../liveReader";
export class MatteBakeCancelled extends Error {
constructor() {
@@ -42,7 +42,7 @@ export async function localBakeMatte(
const seconds = Math.max(1 / MATTE_FPS, to - from);
const frames = still ? 1 : Math.max(1, Math.ceil(seconds * MATTE_FPS));
- const reader = new ClipReader(asset, () => asset.url);
+ const reader = liveReader(asset);
try {
const first = await reader.frameAt(from);
if (first.kind !== "ready") throw new Error("The clip's picture could not be read.");
diff --git a/site/src/cut/lib/removalVideo.ts b/site/src/cut/lib/removalVideo.ts
index ec06a723..eda2fd54 100644
--- a/site/src/cut/lib/removalVideo.ts
+++ b/site/src/cut/lib/removalVideo.ts
@@ -13,10 +13,10 @@
import { matteLumaToAlpha } from "@donkeycut/effects-kit";
import { openCanvasVideo, scaledEvenSize } from "./canvasVideo";
import { FrameCompositor, type Frame } from "./composite";
-import { ClipReader } from "./exportRender";
import { createRasterCanvas, decodeRasterImageUrl, type RasterSurface } from "./raster";
import { clipSpeed } from "./store";
import type { MediaAsset, VideoClip } from "./types";
+import { liveReader } from "./liveReader";
export interface RemovalPieces {
rgb: Blob;
@@ -79,8 +79,8 @@ export async function renderRemovalPieces(
const frames = Math.max(1, Math.ceil(dur * opts.fps));
const baked = clip.removal!.matte;
- const reader = new ClipReader(asset, () => asset.url);
- const matteReader = matteAsset ? new ClipReader(matteAsset, () => matteAsset.url) : null;
+ const reader = liveReader(asset);
+ const matteReader = matteAsset ? liveReader(matteAsset) : null;
try {
const first = await reader.frameAt(still ? 0 : clip.in);
if (first.kind !== "ready") throw new Error("The removal clip's picture could not be read.");