Open source CapCut alternative.
TypeScript
fix(site): pre-roll the next Cut clip live so playback crosses cuts hot
David Le561073f30f48
diff --git a/site/src/cut/hooks/usePlayback.ts b/site/src/cut/hooks/usePlayback.tsindex e78a37af..9a2c0b73 100644--- a/site/src/cut/hooks/usePlayback.ts+++ b/site/src/cut/hooks/usePlayback.ts@@ -49,6 +49,13 @@ const safeRate = (speed: number) => Math.min(16, Math.max(0.0625, speed));const WARM_HORIZON_S = 8;const WARM_MAX = 4;+// Pre-roll lead. Inside this many seconds of a clip's entrance, its element is+// played muted and undrawn so the decoder is already running across the+// in-point when the cut lands — the handoff play() then resumes hot instead of+// spinning a cold decoder up. Kept short so the pre-rolled picture arrives at+// the in-point right as the playhead reaches the cut.+const PREROLL_LEAD_S = 0.5;+const pauseEl = (el: MediaEl) => {if (!isImageEl(el) && !el.paused) el.pause();};@@ -156,9 +163,12 @@ class Engine {for (const span of spans) {if (span.start <= t) continue; // current or past — not ours to warmif (span.start > t + WARM_HORIZON_S) break;+ // The imminent clip inside the pre-roll window is `warmNext`'s to play+ // hot; a parking seek here would fight it, so leave it alone.+ if (span.start - t <= PREROLL_LEAD_S) continue;const el = this.videoFor(span.clip, span.asset); // creating it starts the fetch- // Only nudge a parked element back to its entrance frame — never yank the- // imminent clip that `warmNext` is priming (it's mid-play, so `!paused`).+ // Park a not-yet-imminent clip on its entrance frame so its file is+ // fetching and frame 0 is decoded before the pre-roll window reaches it.if (!isImageEl(el) && !el.seeking && el.paused) {const target = span.clip.in;if (Math.abs(el.currentTime - target) > 0.1) el.currentTime = target;@@ -186,42 +196,33 @@ class Engine {}}- // Pipeline pre-roll state for the imminent next clip. A decode-ahead element- // has its entrance frame decoded but its decoder+audio pipeline cold, so the- // handoff `play()` spins up (tens of ms) before the clock advances — the- // residual hitch at a cut even with decode-ahead. `warmNext` exercises the- // pipeline once, muted, in the second before the cut, then parks the element- // back on its entrance frame.- private primeState = new WeakMap<MediaEl, "priming" | "primed">();-- /** Warm the imminent next clip so its handoff is instant: hold its entrance- * frame decoded, then run the decoder+audio pipeline once (muted) and park it- * back on that frame. The real `play()` at the boundary resumes hot — the- * picture and audio start on the entrance frame with no spin-up, and nothing- * is skipped (unlike a running pre-roll, which would start the clip late). */- private warmNext(span: ClipSpan) {+ /** Pre-roll the imminent next clip so its handoff is hot. A decode-ahead+ * element sits paused on its entrance frame with a cold decode pipeline, so+ * the handoff `play()` has to spin the decoder up — and past a trimmed+ * in-point, decode forward from the prior keyframe — before the clock+ * advances: the residual hitch at a cut, and the freeze that parking the+ * element back on its frame never cured. Instead, in the moments before the+ * cut, play the element muted and undrawn from `lead` of source before its+ * entrance, so it is already running across the in-point — arriving there as+ * the playhead reaches the cut — and the real `play()` resumes hot with+ * nothing skipped. */+ private warmNext(span: ClipSpan, t: number) {const el = this.videoFor(span.clip, span.asset);if (isImageEl(el)) return; // a still needs no pipeline- const state = this.primeState.get(el);- if (state === "priming") {- // The muted play has spun the pipeline up (buffered forward, or the clock- // has ticked past the entrance frame): park it back and mark it hot.- if (el.readyState >= 3 || el.currentTime > span.clip.in + 0.02) {- el.pause();- el.currentTime = span.clip.in;- this.primeState.set(el, "primed");- }- return;- }- // Hold the decoded entrance frame until the pipeline is ready to prime.- if (!el.seeking && Math.abs(el.currentTime - span.clip.in) > 0.1) {- el.currentTime = span.clip.in;- }- if (state !== "primed" && el.readyState >= 2 && !el.seeking) {- this.primeState.set(el, "priming");- el.muted = true;- void el.play().catch(() => this.primeState.delete(el));- }+ // Farther out than the lead: leave it parked and buffering (warmAhead's job).+ if (t < span.start - PREROLL_LEAD_S) return;+ const speed = clipSpeed(span.clip);+ const rate = safeRate(speed);+ if (el.playbackRate !== rate) el.playbackRate = rate;+ el.muted = true; // silent until it becomes master and unmutes+ // Already rolling: let it run — it crosses `in` on its own as the cut lands.+ if (!el.paused) return;+ // Seat it `lead` of source before the entrance, then play forward so it+ // reaches `in` right as the playhead reaches the cut. Bounded at 0 for an+ // untrimmed clip, whose keyframe-0 start is already hot.+ const from = Math.max(0, span.clip.in - PREROLL_LEAD_S * speed);+ if (!el.seeking && Math.abs(el.currentTime - from) > 0.1) el.currentTime = from;+ if (el.readyState >= 2 && !el.seeking) void el.play().catch(() => {});}/** Seek/rate/play one clip's element toward its frame at timeline time `t`,@@ -375,8 +376,8 @@ class Engine {// Prime the next clip's decoder+audio pipeline shortly before its entrance// (the dissolve start, or the hard cut) so the handoff `play()` resumes hot// — no cold-start spin-up freezing the picture and playhead at the cut.- if (next && t < next.start && t >= next.start - 1) {- this.warmNext(next);+ if (next && t < next.start && t >= next.start - PREROLL_LEAD_S) {+ this.warmNext(next, t);keep.add(next.clip.id);}// Cross styles: once the incoming footprint starts, blend it in over the