Open source CapCut alternative.
TypeScript
fix(site): let a black opening frame outrank the poster
David Le94f6e8bdf3bb
diff --git a/evals/cut-perf.latest-report.json b/evals/cut-perf.latest-report.jsonindex e3da30e6..e9be48c9 100644--- a/evals/cut-perf.latest-report.json+++ b/evals/cut-perf.latest-report.json@@ -53,8 +53,8 @@"scrub": {"p50": 15.8,"p95": 32.5,- "mean": 18.43,- "max": 32.6,+ "mean": 18.42,+ "max": 32.7,"n": 41},"longTasks": {@@ -64,7 +64,7 @@"max": 0,"n": 0},- "liveSamples": 67,+ "liveSamples": 36,"machine": "desktop"},{@@ -81,10 +81,10 @@},"frameMs": 16.7,"scrub": {- "p50": 15.6,- "p95": 16.1,- "mean": 15.94,- "max": 32.5,+ "p50": 15.7,+ "p95": 16.6,+ "mean": 16.28,+ "max": 31.9,"n": 41},"longTasks": {@@ -94,7 +94,7 @@"max": 0,"n": 0},- "liveSamples": 74,+ "liveSamples": 56,"machine": "desktop"},{@@ -111,10 +111,10 @@},"frameMs": 16.7,"scrub": {- "p50": 15.6,+ "p50": 15.5,"p95": 16,- "mean": 15.86,- "max": 32.4,+ "mean": 14.83,+ "max": 32,"n": 41},"longTasks": {@@ -124,7 +124,7 @@"max": 0,"n": 0},- "liveSamples": 96,+ "liveSamples": 76,"machine": "desktop"},{@@ -135,16 +135,16 @@"late": {"p50": 0,"p95": 0,- "mean": 0.01,- "max": 1,+ "mean": 0.04,+ "max": 2,"n": 81},"frameMs": 16.7,"scrub": {- "p50": 15.5,- "p95": 16.2,- "mean": 15.06,- "max": 30.7,+ "p50": 15.3,+ "p95": 16,+ "mean": 15.31,+ "max": 51.8,"n": 81},"longTasks": {@@ -165,16 +165,16 @@"late": {"p50": 0,"p95": 1,- "mean": 0.15,- "max": 1,+ "mean": 0.17,+ "max": 2,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 15.8,- "p95": 32.6,- "mean": 18.22,- "max": 33.4,+ "p50": 15.7,+ "p95": 32.3,+ "mean": 18.42,+ "max": 49.2,"n": 41},"longTasks": {@@ -184,7 +184,7 @@"max": 0,"n": 0},- "liveSamples": 56,+ "liveSamples": 58,"machine": "desktop"},{@@ -195,16 +195,16 @@"late": {"p50": 0,"p95": 1,- "mean": 0.17,- "max": 1,+ "mean": 0.2,+ "max": 2,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 15.9,- "p95": 32.1,- "mean": 18.36,- "max": 33.7,+ "p50": 15.5,+ "p95": 32.4,+ "mean": 18.3,+ "max": 48.3,"n": 41},"longTasks": {@@ -214,7 +214,7 @@"max": 0,"n": 0},- "liveSamples": 64,+ "liveSamples": 63,"machine": "desktop"},{@@ -225,16 +225,16 @@"late": {"p50": 1,"p95": 1,- "mean": 0.88,- "max": 1,+ "mean": 0.95,+ "max": 2,"n": 41},"frameMs": 16.7,"scrub": {- "p50": 32.2,- "p95": 33.1,- "mean": 30.23,- "max": 33.4,+ "p50": 32.1,+ "p95": 33.2,+ "mean": 31.18,+ "max": 64.6,"n": 41},"longTasks": {@@ -271,7 +271,7 @@"pass": true,"notes": [],"drops": 0,- "presented": 1035,+ "presented": 1038,"boundaryDrops": 0,"longTasks": {"p50": 0,@@ -303,8 +303,8 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {"p50": 0,@@ -326,13 +326,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 3240,+ "presented": 3236,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 3240+ "n": 3236},"stallShare": 0,"decay": {@@ -340,8 +340,8 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {"p50": 0,@@ -363,13 +363,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 225,+ "presented": 222,"lag": {"p50": 0.02,"p95": 0.03,- "mean": 0.01,+ "mean": 0.02,"max": 0.03,- "n": 225+ "n": 222},"stallShare": 0,"decay": {@@ -377,8 +377,8 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {"p50": 0,@@ -402,7 +402,7 @@"drops": 0,"presented": 3444,"lag": {- "p50": 0.03,+ "p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,@@ -414,8 +414,8 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {"p50": 0,@@ -451,8 +451,8 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {"p50": 0,@@ -488,15 +488,15 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {- "p50": 0,- "p95": 0,- "mean": 0,- "max": 0,- "n": 0+ "p50": 66,+ "p95": 66,+ "mean": 66,+ "max": 66,+ "n": 1},"liveSamples": 10,"liveSources": 1,@@ -525,15 +525,15 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {- "p50": 0,- "p95": 0,- "mean": 0,- "max": 0,- "n": 0+ "p50": 64,+ "p95": 64,+ "mean": 62,+ "max": 64,+ "n": 2},"liveSamples": 10,"liveSources": 1,@@ -551,9 +551,9 @@"presented": 3444,"lag": {"p50": 0.01,- "p95": 0.02,+ "p95": 0.01,"mean": 0.01,- "max": 0.02,+ "max": 0.01,"n": 3444},"stallShare": 0,@@ -562,8 +562,8 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {"p50": 0,@@ -576,7 +576,7 @@"liveSources": 1,"keptSources": 1,"warmMb": 0,- "memMb": 144,+ "memMb": 143,"machine": "desktop"},{@@ -599,8 +599,8 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {"p50": 0,@@ -622,13 +622,13 @@"pass": true,"notes": [],"drops": 0,- "presented": 1620,+ "presented": 1618,"lag": {"p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 1620+ "n": 1618},"stallShare": 0,"decay": {@@ -636,8 +636,8 @@"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {"p50": 0,@@ -691,36 +691,36 @@"bucket": "playback","pass": true,"notes": [],- "drops": 2,+ "drops": 3,"presented": 1094,"lag": {- "p50": 0.01,+ "p50": 0.02,"p95": 0.03,"mean": 0.02,"max": 0.03,- "n": 1092+ "n": 1091},"stallShare": 0,"decay": {- "first": 0.005,+ "first": 0.008,"last": 0},"clock": {- "lead": 0.04,- "reported": 0.04+ "lead": 0.22,+ "reported": 0.22},"longTasks": {- "p50": 136,- "p95": 136,- "mean": 98.5,- "max": 136,+ "p50": 177,+ "p95": 177,+ "mean": 119.5,+ "max": 177,"n": 2},"liveSamples": 10,"liveSources": 1,"keptSources": 1,"warmMb": 0,- "memMb": 209,+ "memMb": 218,"machine": "desktop"}]diff --git a/site/src/cut/components/Preview.tsx b/site/src/cut/components/Preview.tsxindex cef38adb..75c24f46 100644--- a/site/src/cut/components/Preview.tsx+++ b/site/src/cut/components/Preview.tsx@@ -17,10 +17,11 @@ import {import { clipLen, projectDuration, useEditor } from "@/cut/lib/store";import { copyStageFrame, primeStageFrame, stageHasFrame } from "@/cut/lib/stageFrame";import {- capturePoster,capturePosterOnPicture,paintPoster,+ posterStillWanted,readPoster,+ stageDrawMark,} from "@/cut/lib/posterCache";import { resizePreviewSurface, setPreviewCanvas } from "@/cut/lib/previewCanvas";import { hdrPreviewState, subscribeHdrPreviewState } from "@/cut/lib/hdrPresent";@@ -76,12 +77,13 @@ function useCachedFirstFrame(const canvas = canvasRef.current;if (!canvas || !epoch || !projectId) return;let alive = true;+ const mark = stageDrawMark();void readPoster("frame", projectId).then((data) => {// Only if the engine has not beaten us to it — a warm decoder paints- // within the same frame, and the real picture outranks a stored one.- if (alive && data && !capturePoster("frame", projectId, canvas)) {- void paintPoster(canvas, data);- }+ // within the same frame, and the real picture outranks a stored one,+ // even an all-black one at a gap the cut opens on.+ const wanted = () => alive && posterStillWanted(mark);+ if (data && wanted()) void paintPoster(canvas, data, wanted);});// An HDR preview's poster reads off the presented picture: the composite// canvas beneath holds an HLG signal, which is not a picture to keep.diff --git a/site/src/cut/hooks/usePlayback.ts b/site/src/cut/hooks/usePlayback.tsindex 6bc73aa5..7a4d49d1 100644--- a/site/src/cut/hooks/usePlayback.ts+++ b/site/src/cut/hooks/usePlayback.ts@@ -2,7 +2,7 @@import { useEffect, type RefObject } from "react";-import { noteStagePicture } from "@/cut/lib/posterCache";+import { noteStageDraw, noteStagePicture } from "@/cut/lib/posterCache";import {getClipSpans,overlayLayers,@@ -981,6 +981,7 @@ class Engine {this.stalledAt = 0;if (s.buffering) useEditor.setState({ buffering: false });this.comp.drawLayer(MISSING_FRAME, undefined, true, 1, 0);+ noteStageDraw();if (s.playing) {useEditor.setState({ playing: false });this.writeHead(0);@@ -1050,6 +1051,7 @@ class Engine {// readback of it costs nothing anyone is watching; a play's first// frame is read back once the play stops and the frame is redrawn.if (!playing && masterFrame.kind === "ready") noteStagePicture();+ noteStageDraw();}this.phase("draw");diff --git a/site/src/cut/lib/posterCache.test.ts b/site/src/cut/lib/posterCache.test.tsnew file mode 100644index 00000000..ab6e972d--- /dev/null+++ b/site/src/cut/lib/posterCache.test.ts@@ -0,0 +1,20 @@+import { describe, expect, test } from "bun:test";+import { noteStageDraw, posterStillWanted, stageDrawMark } from "./posterCache";++/** The opening poster stands in only until the stage draws. A cut that opens+ * on a gap draws an all-black frame, and reading the canvas for a picture+ * took that for "nothing yet": the poster — a frame from elsewhere in the cut+ * — was painted over the true opening and stayed until the next redraw. */++describe("the opening poster", () => {+ test("is wanted while the stage has drawn nothing since the open", () => {+ const mark = stageDrawMark();+ expect(posterStillWanted(mark)).toBe(true);+ });++ test("is dropped once the stage draws, even a frame with no picture in it", () => {+ const mark = stageDrawMark();+ noteStageDraw();+ expect(posterStillWanted(mark)).toBe(false);+ });+});diff --git a/site/src/cut/lib/posterCache.ts b/site/src/cut/lib/posterCache.tsindex eb6d98f4..32833ceb 100644--- a/site/src/cut/lib/posterCache.ts+++ b/site/src/cut/lib/posterCache.ts@@ -97,6 +97,23 @@ export function capturePoster(}}+// Every frame the stage draws, picture or not. The opening poster stands in+// only until the first one: a cut that opens on a gap draws an all-black+// frame, which no pixel test can tell from a stage that has drawn nothing.+let stageDraws = 0;++/** The stage drew a frame of the cut. */+export function noteStageDraw(): void {+ stageDraws++;+}++/** Where the stage's draws stand now; `posterStillWanted` compares against it. */+export const stageDrawMark = (): number => stageDraws;++/** Whether a poster read since `mark` may still be painted: only while the+ * stage has drawn nothing of its own. */+export const posterStillWanted = (mark: number): boolean => stageDraws === mark;+// The stage says when it has drawn a real picture, so the preview's poster// is taken from a frame known to be there. Asking on a timer instead meant a// pixel readback of the stage every few hundred milliseconds for the first@@ -168,13 +185,14 @@ export function capturePosterWhenReady(}/** Draw a stored poster over a canvas, contained and centred so a project whose- * aspect has changed since is letterboxed rather than stretched. */-export function paintPoster(canvas: HTMLCanvasElement, data: string): Promise<void> {+ * aspect has changed since is letterboxed rather than stretched. `wanted` is+ * asked again once the image decodes: the stage may have drawn meanwhile. */+export function paintPoster(canvas: HTMLCanvasElement, data: string, wanted?: () => boolean): Promise<void> {return new Promise((resolve) => {const img = new Image();img.onload = () => {const ctx = canvas.getContext("2d");- if (ctx) {+ if (ctx && (!wanted || wanted())) {const scale = Math.min(canvas.width / img.width, canvas.height / img.height);const w = img.width * scale;const h = img.height * scale;