Open source CapCut alternative.
TypeScript
feat(site): blend smoothed slow motion in the preview
David Le2271545015ed
diff --git a/evals/cut-perf.latest-report.json b/evals/cut-perf.latest-report.jsonindex 00f39443..7beb7359 100644--- a/evals/cut-perf.latest-report.json+++ b/evals/cut-perf.latest-report.json@@ -9,6 +9,8 @@"outputLatencyS": 0,"netKbps": 0,"rttMs": 0,+ "software": false,+ "decode": 1,"lagP95S": 0.05,"stallShare": 0.002,"lateShare": 0.02,@@ -43,16 +45,16 @@"late": {"p50": 0,"p95": 1,- "mean": 0.2,- "max": 2,+ "mean": 0.17,+ "max": 1,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 15.7,- "p95": 33.5,- "mean": 18.89,- "max": 47.8,+ "p50": 15.8,+ "p95": 31.6,+ "mean": 18.23,+ "max": 32.2,"n": 41},"longTasks": {@@ -62,7 +64,7 @@"max": 0,"n": 0},- "liveSamples": 48,+ "liveSamples": 46,"machine": "desktop"},{@@ -73,16 +75,16 @@"late": {"p50": 0,"p95": 1,- "mean": 0.15,- "max": 2,+ "mean": 0.12,+ "max": 1,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 15.9,+ "p50": 15.7,"p95": 32.4,- "mean": 18.11,- "max": 48.2,+ "mean": 17.45,+ "max": 33.1,"n": 41},"longTasks": {@@ -92,7 +94,7 @@"max": 0,"n": 0},- "liveSamples": 69,+ "liveSamples": 53,"machine": "desktop"},{@@ -103,16 +105,16 @@"late": {"p50": 0,"p95": 0,- "mean": 0.05,+ "mean": 0.02,"max": 1,"n": 41},"frameMs": 16.7,"scrub": {"p50": 15.7,- "p95": 16.6,- "mean": 16.25,- "max": 32.2,+ "p95": 16,+ "mean": 15.9,+ "max": 31.6,"n": 41},"longTasks": {@@ -122,7 +124,7 @@"max": 0,"n": 0},- "liveSamples": 101,+ "liveSamples": 75,"machine": "desktop"},{@@ -133,16 +135,16 @@"late": {"p50": 0,"p95": 0,- "mean": 0.04,- "max": 2,+ "mean": 0.02,+ "max": 1,"n": 81},"frameMs": 16.7,"scrub": {- "p50": 15.6,- "p95": 16.6,- "mean": 15.75,- "max": 44.2,+ "p50": 15.8,+ "p95": 16.1,+ "mean": 15.94,+ "max": 32,"n": 81},"longTasks": {@@ -152,7 +154,7 @@"max": 0,"n": 0},- "liveSamples": 48,+ "liveSamples": 32,"machine": "desktop"},{@@ -169,9 +171,9 @@},"frameMs": 16.7,"scrub": {- "p50": 15.9,- "p95": 31.8,- "mean": 18.15,+ "p50": 15.8,+ "p95": 32.5,+ "mean": 18.4,"max": 32.7,"n": 41},@@ -182,7 +184,7 @@"max": 0,"n": 0},- "liveSamples": 65,+ "liveSamples": 63,"machine": "desktop"},{@@ -199,10 +201,10 @@},"frameMs": 16.7,"scrub": {- "p50": 15.8,- "p95": 32.4,- "mean": 18.37,- "max": 33,+ "p50": 16,+ "p95": 32.7,+ "mean": 18.45,+ "max": 33.5,"n": 41},"longTasks": {@@ -212,7 +214,7 @@"max": 0,"n": 0},- "liveSamples": 54,+ "liveSamples": 44,"machine": "desktop"},{@@ -223,16 +225,16 @@"late": {"p50": 1,"p95": 1,- "mean": 0.9,+ "mean": 0.95,"max": 1,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 32.2,- "p95": 32.9,- "mean": 30.51,- "max": 33.1,+ "p50": 32,+ "p95": 33.6,+ "mean": 31.26,+ "max": 34.4,"n": 41},"longTasks": {@@ -287,13 +289,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 808,+ "presented": 810,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 808+ "n": 810},"stallShare": 0,"decay": {@@ -324,13 +326,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3240,+ "presented": 3238,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 3240+ "n": 3238},"stallShare": 0,"decay": {@@ -361,13 +363,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 222,+ "presented": 223,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.01,"max": 0.03,- "n": 222+ "n": 223},"stallShare": 0,"decay": {@@ -398,13 +400,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 1029,+ "presented": 1027,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 1029+ "n": 1027},"stallShare": 0,"decay": {@@ -435,13 +437,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 1027,+ "presented": 1028,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 1027+ "n": 1028},"stallShare": 0,"decay": {@@ -474,7 +476,7 @@"drops": 0,"presented": 3444,"lag": {- "p50": 0.02,+ "p50": 0.01,"p95": 0.03,"mean": 0.02,"max": 0.03,@@ -512,7 +514,7 @@"presented": 3444,"lag": {"p50": 0.01,- "p95": 0.02,+ "p95": 0.01,"mean": 0.01,"max": 0.02,"n": 3444@@ -583,13 +585,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3443,+ "presented": 3444,"lag": {- "p50": 0.02,+ "p50": 0.01,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 3443+ "n": 3444},"stallShare": 0,"decay": {@@ -611,7 +613,7 @@"liveSources": 1,"keptSources": 1,"warmMb": 0,- "memMb": 134,+ "memMb": 125,"machine": "desktop"},{@@ -619,18 +621,18 @@"bucket": "playback","pass": true,"notes": [],- "drops": 0,- "presented": 3439,+ "drops": 1,+ "presented": 3427,"lag": {"p50": 0.01,"p95": 0.01,"mean": 0.01,- "max": 0.02,- "n": 3439+ "max": 0.1,+ "n": 3427},"stallShare": 0,"decay": {- "first": 0,+ "first": 0.001,"last": 0},"clock": {@@ -638,11 +640,11 @@"reported": 0.04},"longTasks": {- "p50": 0,- "p95": 0,- "mean": 0,- "max": 0,- "n": 0+ "p50": 257,+ "p95": 257,+ "mean": 257,+ "max": 257,+ "n": 1},"liveSamples": 10,"liveSources": 1,@@ -694,13 +696,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 1620,+ "presented": 1622,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 1620+ "n": 1622},"stallShare": 0,"decay": {@@ -757,6 +759,43 @@"max": 0.11,"n": 32}+ },+ {+ "name": "play-4k-from-library",+ "bucket": "playback",+ "pass": true,+ "notes": [],+ "drops": 3,+ "presented": 1095,+ "lag": {+ "p50": 0.02,+ "p95": 0.03,+ "mean": 0.01,+ "max": 0.03,+ "n": 1092+ },+ "stallShare": 0,+ "decay": {+ "first": 0.008,+ "last": 0+ },+ "clock": {+ "lead": 0.04,+ "reported": 0.04+ },+ "longTasks": {+ "p50": 137,+ "p95": 137,+ "mean": 136.5,+ "max": 137,+ "n": 2+ },+ "liveSamples": 10,+ "liveSources": 1,+ "keptSources": 1,+ "warmMb": 0,+ "memMb": 218,+ "machine": "desktop"}]}diff --git a/site/src/cut/hooks/usePlayback.ts b/site/src/cut/hooks/usePlayback.tsindex e633f214..956b191e 100644--- a/site/src/cut/hooks/usePlayback.ts+++ b/site/src/cut/hooks/usePlayback.ts@@ -9,7 +9,8 @@ import {projectDuration,useEditor,} from "@/cut/lib/store";-import { headSrc, matteLumaToAlpha, retimeOf, srcSpan } from "@donkeycut/effects-kit";+import { headSrc, matteLumaToAlpha, retimeOf, smoothsAt, srcSpan } from "@donkeycut/effects-kit";+import { blendInto, SYNTH_EDGE, synthWeight } from "@/cut/lib/frameSynth";import { playheadAt, previewAt, setPlayhead, subscribePlayhead } from "@/cut/lib/playhead";import { assetIsSilent, clipCovers, projectFadeSeconds, rectOf } from "@/cut/lib/types";import type { ClipSpan, MediaAsset, VideoClip } from "@/cut/lib/types";@@ -220,6 +221,10 @@ class Engine {* deleted clips never pile up canvases. */private matteAlpha = new Map<string, { ts: number; canvas: RasterSurface }>();private static readonly MATTE_ALPHA_MAX = 8;+ /** Per clip, the canvas its smoothed slow motion is blended on. Bounded+ * like the mattes: the clips drawn longest ago give theirs up. */+ private smoothed = new Map<string, RasterSurface>();+ private static readonly SMOOTHED_MAX = 4;private unsubscribe: () => void;private unwatch: () => void;@@ -256,6 +261,7 @@ class Engine {this.pool.closeAll();this.behind.clear();this.matteAlpha.clear();+ this.smoothed.clear();this.mixer.releaseCaches();// The canvas keeps showing the frame it last painted, so nothing is// blank while the tab is away. The wake is for the return: it books the@@ -468,7 +474,7 @@ class Engine {// to show goes missing. The clip's own span bounds the answer: two clips// split from one file share a source, and a held frame from across the// split would show the other scene at a paused playhead.- const frame = src.frameAt(st, span.clip.in, span.clip.out);+ const frame = this.smoothFrame(span, t, st, src);// The picture on track 0 is the one being watched, so it is the one the// machine is judged on — see the meter in `perfTrace`.if (master && playing) {@@ -482,6 +488,38 @@ class Engine {return src.failed ? MISSING_FRAME : PENDING_FRAME;}+ /**+ * The clip's picture at source time `st`, blended between the two source+ * frames around it where the clip smooths its slow motion and the rate is+ * under 1×. The blend is the preview's stand-in for the export's motion+ * estimate: two draws, inside the frame budget. Everywhere else, and at+ * either edge of the pair, the source frame shows as is.+ */+ private smoothFrame(span: ClipSpan, t: number, st: number, src: ClipFrameSource) {+ const { clip } = span;+ if (!smoothsAt(clip, retimeOf(clip), t - span.start)) return src.frameAt(st, clip.in, clip.out);+ const pair = src.pairAt(st, clip.in, clip.out);+ if (!pair) return null;+ if (!pair.b) return pair.a;+ const w = synthWeight(st, pair.a.timestamp, pair.b.timestamp);+ if (w <= SYNTH_EDGE) return pair.a;+ if (w >= 1 - SYNTH_EDGE) return pair.b;+ const { width, height } = pair.a;+ let canvas = this.smoothed.get(clip.id);+ // Re-insert on every touch so the cap below drops the clip drawn longest ago.+ this.smoothed.delete(clip.id);+ if (!canvas) canvas = createRasterCanvas(width, height);+ if (canvas.width !== width) canvas.width = width;+ if (canvas.height !== height) canvas.height = height;+ this.smoothed.set(clip.id, canvas);+ if (this.smoothed.size > Engine.SMOOTHED_MAX) {+ const oldest = this.smoothed.keys().next().value;+ if (oldest !== undefined) this.smoothed.delete(oldest);+ }+ blendInto(canvas, pair.a.image, pair.b.image, w);+ return { image: canvas as CanvasImageSource, width, height, timestamp: pair.a.timestamp };+ }+/** Open and start the decoders for clips about to arrive — on track 0 and* the overlay tracks alike — so a cut or an overlay's entrance lands on a* ring that is already full. */diff --git a/site/src/cut/lib/frameSource.ts b/site/src/cut/lib/frameSource.tsindex c5584a58..69a1ddb5 100644--- a/site/src/cut/lib/frameSource.ts+++ b/site/src/cut/lib/frameSource.ts@@ -472,6 +472,17 @@ export class FrameRing<T extends Timed> {);}+ /** The first frame held after timestamp `ts`, bounded above by `to`: the+ * other side of the pair a smoothed frame is made from. */+ after(ts: number, to = Infinity): T | null {+ let pick: T | null = null;+ for (const i of this.items) {+ if (i.timestamp <= ts + SAME || i.timestamp > to + SAME) continue;+ if (!pick || i.timestamp < pick.timestamp) pick = i;+ }+ return pick;+ }+/** Whether any frame held starts at or before `t`. */hasAtOrBefore(t: number): boolean {return this.items.some((i) => i.timestamp <= t + SAME);@@ -844,6 +855,28 @@ export class ClipFrameSource {return c ? frameOfCanvas(c) : null;}+ /**+ * The frames either side of source time `t`, for a clip smoothing its slow+ * motion: `a` is what `frameAt` gives, `b` the next sharp frame held after+ * it, or null when the walk has not landed it yet — then `a` shows on its+ * own, as it would unsmoothed. A coarse frame never makes a pair: blending+ * toward a half-size picture is a step down, not a step between.+ */+ pairAt(t: number, from = -Infinity, to = Infinity): { a: SourceFrame; b: SourceFrame | null } | null {+ const a = this.frameAt(t, from, to);+ if (!a) return null;+ if (this.still || a.timestamp > t + SAME) return { a, b: null };+ const fine = this.back?.fine;+ // The frame `frameAt` gave has to be a sharp one: the coarse spread is+ // half size, and it only stands in where no sharp frame is near.+ const sharp = FrameRing.nearer(t, this.ring.at(t, from, to), fine?.at(t, from, to) ?? null);+ if (!sharp || Math.abs(sharp.timestamp - a.timestamp) > SAME) return { a, b: null };+ let next = this.ring.after(a.timestamp, to);+ const other = fine?.after(a.timestamp, to) ?? null;+ if (other && (!next || other.timestamp < next.timestamp)) next = other;+ return { a, b: next ? frameOfCanvas(next) : null };+ }+/** Whether a frame covering `t` exactly is already held. Coarse frames do* not count: they are a picture near `t`, at half size. */hasExact(t: number): boolean {