Skip to content

Open source CapCut alternative.

TypeScript

fix(site): keep clip-edge reads off the link while the cut plays

David Led2521d67e2bb
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index 8cc13bf4..d3712f34 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -68,7 +68,7 @@ import { laneHidden, subtitleLaneCount } from "@/cut/lib/subtitles";
import { formatTime, formatTimecode } from "@/cut/lib/time";
import { EFFECT_LABELS, hasSpeedCurve, headSrc, retimeOf, SPEED_CURVE_MAX, SPEED_CURVE_MIN, tailSrc, type EffectId, type Retime, type SpeedNode } from "@donkeycut/effects-kit";
import { BLOCK_COLOR } from "@/cut/lib/blockSource";
-import { isLinkedAssetType, assetIsSilent, emptySubtitles, fontStack, IMAGE_CLIP_SECONDS, isAudioTransition, clipName, isTextOverlay, overlayName, SHAPE_LABELS, TRANSITION_STYLE_LABELS, transitionBarStart, transitionDefaultSeconds, XBAR_MAGNET_PX, type ShapeKind } from "@/cut/lib/types";
+import { isLinkedAssetType, assetIsSilent, previewUrl, emptySubtitles, fontStack, IMAGE_CLIP_SECONDS, isAudioTransition, clipName, isTextOverlay, overlayName, SHAPE_LABELS, TRANSITION_STYLE_LABELS, transitionBarStart, transitionDefaultSeconds, XBAR_MAGNET_PX, type ShapeKind } from "@/cut/lib/types";
import type { AudioClip, ClipSpan, ColorGrade, MediaAsset, Overlay, Selection, StickerOverlay, SubtitleCue, TimelineTransition, TransitionBoundaryKind, TransitionStyle, VideoClip } from "@/cut/lib/types";
import { isLottieAsset } from "@/cut/lib/lottieAssets";
import { gradeCssApprox, normalizeGrade } from "@donkeycut/effects-kit";
@@ -4457,7 +4457,7 @@ function GhostClip({
useEffect(() => {
if (!seedless) return;
let live = true;
- void requestEdgeFrame("drop-ghost:head", asset.url, 0, undefined, { background: true }).then(
+ void requestEdgeFrame("drop-ghost:head", previewUrl(asset), 0, undefined, { background: true }).then(
(src) => {
if (live && src) onTileFrame();
}
@@ -4522,7 +4522,7 @@ function seedThumb(
edges?: { start: string | null; end: string | null }
): string[] {
if (asset.type !== "video") return [];
- const seed = edges?.start ?? peekEdgeFrame(asset.url, filmIn);
+ const seed = edges?.start ?? peekEdgeFrame(previewUrl(asset), filmIn);
return seed ? [seed] : [];
}
@@ -4640,7 +4640,7 @@ function filmstripFrames(
tileH,
minTileW,
cuts: asset.sceneCuts,
- exactFrame: asset.type === "video" ? (t) => peekEdgeFrame(asset.url, t) : undefined,
+ exactFrame: asset.type === "video" ? (t) => peekEdgeFrame(previewUrl(asset), t) : undefined,
view,
});
// The window may start past the clip's first tile or end before its last;
@@ -4687,7 +4687,7 @@ function useTileFrames(
const wanted = tiles.length > 0;
useEffect(() => {
const box = boxRef.current;
- const url = asset?.type === "video" ? asset.url : null;
+ const url = asset?.type === "video" ? previewUrl(asset) : null;
if (!box || !url || !wanted) return;
const scroller = box.closest("[data-tl-scroll]") as HTMLElement | null;
let live = true;
@@ -4747,7 +4747,7 @@ function useTileFrames(
* thumb shows instead) until the capture lands; a changed edge time falls back
* immediately so a trim drag never shows a stale exact frame. */
function useEdgeFrame(asset: MediaAsset | undefined, time: number, slot: string) {
- const url = asset?.type === "video" ? asset.url : null;
+ const url = asset?.type === "video" ? previewUrl(asset) : null;
const id = url ? `${url}#${time.toFixed(2)}` : "";
const cached = url ? peekEdgeFrame(url, time) : null;
const [frame, setFrame] = useState<{ id: string; src: string } | null>(null);
diff --git a/site/src/cut/components/TransitionsPanel.tsx b/site/src/cut/components/TransitionsPanel.tsx
index adbbf0ec..e83e00ef 100644
--- a/site/src/cut/components/TransitionsPanel.tsx
+++ b/site/src/cut/components/TransitionsPanel.tsx
@@ -16,6 +16,7 @@ import {
TRANSITION_STYLE_GROUPS,
TRANSITION_STYLE_LABELS,
overlayAnimStyle,
+ previewUrl,
transitionStyleOfAnim,
type AnimStyle,
type MediaAsset,
@@ -192,7 +193,7 @@ function useCutFrame(
slot: string,
side: { asset: MediaAsset; srcT: number } | null
): string | null {
- const url = side?.asset.type === "video" ? side.asset.url : null;
+ const url = side?.asset.type === "video" ? previewUrl(side.asset) : null;
const time = side?.srcT ?? 0;
const id = url ? `${url}#${time.toFixed(2)}` : "";
const cached = url ? peekEdgeFrame(url, time, TILE_FRAME_H) : null;
diff --git a/site/src/cut/lib/edgeFramesPlay.test.ts b/site/src/cut/lib/edgeFramesPlay.test.ts
new file mode 100644
index 00000000..0332db16
--- /dev/null
+++ b/site/src/cut/lib/edgeFramesPlay.test.ts
@@ -0,0 +1,29 @@
+import { expect, test } from "bun:test";
+import { stubModule } from "@/lib/testing/stubModule";
+
+// The timeline follows the playhead while the cut plays, and every clip the
+// scroll brings into view asks for its edge frames. Those reads opened each
+// clip's file just as the play opened it, and on a fast cut they took the
+// link from the picture. An edge capture now waits until the play stops.
+const opened: string[] = [];
+await stubModule<typeof import("./mediaRead")>("./mediaRead", import.meta.url, {
+ openMediaShared: ((url: string) => {
+ opened.push(url);
+ return { input: {} as never, release: () => {} };
+ }) as never,
+ videoTrackOf: (async () => null) as never,
+});
+const { requestEdgeFrame } = await import("./media");
+const { useEditor } = await import("./store");
+
+const tick = () => new Promise((r) => setTimeout(r, 10));
+
+test("an edge capture asked for during a play opens nothing until it stops", async () => {
+ useEditor.setState({ playing: true });
+ const landed = requestEdgeFrame("clip:head", "https://m/a.mp4.light.mp4", 1);
+ await tick();
+ expect(opened).toEqual([]);
+ useEditor.setState({ playing: false });
+ expect(await landed).toBeNull();
+ expect(opened).toEqual(["https://m/a.mp4.light.mp4"]);
+});
diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.ts
index e1b06beb..8549338d 100644
--- a/site/src/cut/lib/media.ts
+++ b/site/src/cut/lib/media.ts
@@ -2968,6 +2968,12 @@ async function pumpEdgeFrames() {
idle.cancel();
edgeWake = null;
}
+ // A clip-edge capture waits out a play too. The edges of the clips the
+ // scroll brings into view open each clip's file while the play is
+ // opening the same clips a moment later, and on a fast cut those reads
+ // took the link from the picture and stalled it. A paused timeline
+ // still gets its edges ahead of the strip tiles.
+ if (edgeQueue.size > 0) await idleWait().done;
const queue = edgeQueue.size ? edgeQueue : edgeBackQueue;
const next = queue.entries().next();
if (next.done) break;