Open source CapCut alternative.
TypeScript
fix(site): preview sources and filmstrips recover after a media read outage
David Lea8061f6d7bef
diff --git a/site/src/cut/lib/frameSource.ts b/site/src/cut/lib/frameSource.tsindex 2e9281f5..867d3444 100644--- a/site/src/cut/lib/frameSource.ts+++ b/site/src/cut/lib/frameSource.ts@@ -66,11 +66,18 @@ const SAME = 1e-4;* second of playback — long enough that crossing a cut and coming back finds* the decoder still open. */const EVICT_GRACE = 90;-/** A failed open tries again this much later, growing per attempt, and gives- * up after this many. A network blip heals on its own; a file that is truly- * unreadable stops costing anything after a few seconds. */+/** A failed open tries again this much later, growing per attempt over the+ * first `RETRIES` tries, so a network blip heals within seconds. */const RETRY_MS = 1000;const RETRIES = 3;+/** Past the quick tries, a failed source keeps itself willing on this+ * cadence. An open error is the healable kind of failure — a network stall, a+ * signed URL that expired mid-session — and the heal costs one open attempt+ * per interval, paid only while something still draws the clip: the timer+ * clears the mark and nudges a redraw, and the next ask is what reopens. A+ * file with no track this browser can decode stays latched; that verdict is+ * durable and books no timer. */+const FAIL_RECHECK_MS = 10_000;/** A clip's picture at an instant, with where it came from. */export interface SourceFrame {@@ -345,15 +352,19 @@ export class ClipFrameSource {* An open failed. That is usually a moment — a network blip, a signed URL a* few seconds past its window — so the source tells the link keeper (which* re-mints an expired URL; the pool then swaps this source out under the new- * one) and books itself another try. Only a file that keeps failing stays- * marked unreadable, and only after it has had its chances.+ * one) and books itself another try: quick ones first, then on a slow+ * cadence for as long as the failure holds. The cadence carries the case the+ * swap cannot: inside a signing window a re-mint returns the identical URL+ * string, so no swap ever comes, and the cadence is what bounds a+ * mid-session outage to seconds of black once reads work again.*/private fail(): void {this.unreadable = true;this.opening = null;void import("./mediaLinks").then((m) => m.reportMediaUrlError(this.asset.url));- if (this.closed || this.attempts >= RETRIES || typeof window === "undefined") return;+ if (this.closed || typeof window === "undefined") return;this.attempts++;+ const wait = this.attempts <= RETRIES ? RETRY_MS * this.attempts : FAIL_RECHECK_MS;this.retryTimer = window.setTimeout(() => {this.retryTimer = 0;if (this.closed) return;@@ -361,7 +372,7 @@ export class ClipFrameSource {// Nothing asks a failed source for frames, so nothing would notice it is// willing again without a nudge.this.onFrame();- }, RETRY_MS * this.attempts);+ }, wait);}private open(): Promise<void> {@@ -384,6 +395,9 @@ export class ClipFrameSource {// file's rotation, so a phone clip arrives upright at preview size and// no caller has to know it was ever sideways.this.sink = frameSink(track, { height: this.height }, POOL);+ // A clean open ends any failure streak: the next outage starts from+ // the quick retries again.+ this.attempts = 0;} catch {input.dispose();this.fail();@@ -405,6 +419,7 @@ export class ClipFrameSource {height: bitmap.height,timestamp: 0,};+ this.attempts = 0;this.onFrame();} catch {this.fail();diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.tsindex 13c500d8..d3fe0a7a 100644--- a/site/src/cut/lib/media.ts+++ b/site/src/cut/lib/media.ts@@ -1427,6 +1427,34 @@ function setStripFailed(assetId: string, failed: boolean) {for (const fn of stripListeners) fn();}+// A failed enrich books itself more tries. Filmstrip and waveform reads drop+// out together with the preview's when media reads fail, and the sweep that+// would try again runs only on project open — so a blip at open used to leave+// every clip blank for the whole session. The tries are few and well spaced;+// an asset that gains its thumbs or peaks stops the clock, and one that keeps+// failing goes quiet after the last try.+const ENRICH_RETRY_BASE_MS = 30_000;+const ENRICH_RETRIES = 3;+const enrichRetries = new Map<string, number>();+const enrichRetryTimers = new Map<string, ReturnType<typeof setTimeout>>();++function scheduleEnrichRetry(assetId: string) {+ if (typeof window === "undefined") return;+ if (enrichRetryTimers.has(assetId)) return;+ const n = enrichRetries.get(assetId) ?? 0;+ if (n >= ENRICH_RETRIES) return;+ enrichRetries.set(assetId, n + 1);+ const timer = setTimeout(() => {+ enrichRetryTimers.delete(assetId);+ const asset = useEditor.getState().assets.find((a) => a.id === assetId);+ if (!asset) return;+ const done = asset.type === "audio" ? !!asset.peaks?.length : !!asset.thumbs?.length;+ if (done) return;+ void enrichAsset(asset);+ }, ENRICH_RETRY_BASE_MS * 3 ** n);+ enrichRetryTimers.set(assetId, timer);+}+/** Generate filmstrip thumbnails / waveform peaks and merge them into the* store. Safe to call repeatedly; skips assets that are already enriched.* `src` overrides where the frames are read from — an import still uploading@@ -1452,9 +1480,11 @@ export async function enrichAsset(asset: MediaAsset, src = asset.url) {useEditor.getState().updateAsset(asset.id, { thumbs, thumbStep });writeCachedStrip(key, { thumbs, thumbStep, duration: asset.duration, at: Date.now() });}+ enrichRetries.delete(asset.id);} else if (asset.type === "audio" && !asset.peaks?.length) {const peaks = await makePeaks(src);useEditor.getState().updateAsset(asset.id, { peaks });+ enrichRetries.delete(asset.id);}} catch (err) {// Thumbnails and waveforms are decorative; editing works without them.@@ -1462,6 +1492,7 @@ export async function enrichAsset(asset: MediaAsset, src = asset.url) {// logged at error level, which carries it into PostHog error tracking// (console.error autocapture) with the file's shape beside the cause.if (asset.type === "video") setStripFailed(asset.id, true);+ if (asset.type !== "image") scheduleEnrichRetry(asset.id);console.error(`[cut] enrich failed for ${asset.fileName}`,{ type: asset.type, durationSec: Math.round(asset.duration) },