Skip to content

Open source CapCut alternative.

TypeScript

feat(site): pin clip filmstrip edges to the exact in/out frames

David Le68bcd19be3c9
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index 39a9cc44..d78a63a1 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -34,7 +34,7 @@ import { originalSettings, type ExportDoc } from "@/cut/lib/exportClient";
import { useExports } from "@/cut/lib/exportStore";
import { isDragActive, startDrag, subscribeDragActive } from "@/cut/lib/drag";
import { CLIP_GAP, startLaneMove, startLaneTrim, type LaneDrag } from "@/cut/lib/laneTracks";
-import { ensurePeaks, importImage, importStockMusic, importStockVideo, revealMedia } from "@/cut/lib/media";
+import { ensurePeaks, importImage, importStockMusic, importStockVideo, peekEdgeFrame, requestEdgeFrame, revealMedia } from "@/cut/lib/media";
import { track0Clips, trackGapAt, clipLen, clipSpeed, footprints, getClipSpans, nextFreeStart, overlayLayers, projectDuration, rippleInsert, TIMELINE_H_MAX, useEditor } from "@/cut/lib/store";
import type { VideoTrackPlacement } from "@/cut/lib/store";
import { subtitleLaneCount } from "@/cut/lib/subtitles";
@@ -1646,10 +1646,17 @@ function ClipView({
// Frames start where the box does: skip the source seconds the left dissolve
// consumed so the filmstrip stays aligned under the inset edge.
const filmIn = clip.in + leftXf * speed;
+ const filmOut = filmIn + (w / pps) * speed;
+ const startFrame = useEdgeFrame(asset, filmIn, `${clip.id}:in`);
+ const endFrame = useEdgeFrame(asset, filmOut, `${clip.id}:out`);
const filmstrip = useMemo(
- () => filmstripFrames(asset, filmIn, w, pps, speed, VIDEO_H - 4, 26),
- [asset, filmIn, w, pps, speed]
+ () =>
+ filmstripFrames(asset, filmIn, w, pps, speed, VIDEO_H - 4, 26, {
+ start: startFrame,
+ end: endFrame,
+ }),
+ [asset, filmIn, w, pps, speed, startFrame, endFrame]
);
// The move gesture is the shared lane behavior (parting, snapping); its
@@ -1857,7 +1864,9 @@ function ClipMenu({
/** Sample a clip's filmstrip tiles across its drawn width. Tiles keep the
* asset's aspect until the tile cap would leave the tail of a long clip bare;
- * past that they widen so the capped count still spans the whole box. */
+ * past that they widen so the capped count still spans the whole box. The
+ * first and last tiles pin to the segment's exact boundary frames once
+ * captured; middle tiles keep the nearest pre-sampled thumb. */
function filmstripFrames(
asset: MediaAsset | undefined,
filmIn: number,
@@ -1865,14 +1874,15 @@ function filmstripFrames(
pps: number,
speed: number,
tileH: number,
- minTileW: number
+ minTileW: number,
+ edges?: { start: string | null; end: string | null }
) {
if (!asset?.thumbs?.length || !asset.thumbStep) return [];
const aspect = (asset.width ?? 16) / Math.max(1, asset.height ?? 9);
const natural = Math.max(minTileW, Math.round(tileH * aspect));
const count = Math.max(1, Math.min(120, Math.ceil(w / natural)));
const imgW = Math.max(natural, w / count);
- return Array.from({ length: count }, (_, k) => {
+ const frames = Array.from({ length: count }, (_, k) => {
const timeAt = filmIn + ((k * imgW + imgW / 2) / pps) * speed;
const idx = Math.min(
asset.thumbs!.length - 1,
@@ -1880,6 +1890,32 @@ function filmstripFrames(
);
return { src: asset.thumbs![idx], left: k * imgW, width: imgW };
});
+ if (edges?.start) frames[0] = { ...frames[0], src: edges.start };
+ if (edges?.end && frames.length > 1) {
+ frames[frames.length - 1] = { ...frames[frames.length - 1], src: edges.end };
+ }
+ return frames;
+}
+
+/** The exact source frame at a clip edge. Returns null (the nearest sampled
+ * 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 id = url ? `${url}#${time.toFixed(2)}` : "";
+ const cached = url ? peekEdgeFrame(url, time) : null;
+ const [frame, setFrame] = useState<{ id: string; src: string } | null>(null);
+ useEffect(() => {
+ if (!url || cached) return;
+ let live = true;
+ void requestEdgeFrame(slot, url, time).then((src) => {
+ if (live && src) setFrame({ id, src });
+ });
+ return () => {
+ live = false;
+ };
+ }, [url, time, slot, id, cached]);
+ return cached ?? (frame?.id === id ? frame.src : null);
}
/** A clip box's thumbnail strip, washed with the clip's color grade: the same
@@ -2165,12 +2201,19 @@ function OverlayClipView({
// Frames start where the box does: skip the source seconds the left
// dissolve consumed so the filmstrip stays aligned under the inset edge.
const filmIn = clip.in + leftXf * speed;
+ const filmOut = filmIn + (w / pps) * speed;
// Same filmstrip as a track-0 clip so an overlay reads as a video, not a
// featureless bar — sampled across the clip's trimmed span.
+ const startFrame = useEdgeFrame(asset, filmIn, `${clip.id}:in`);
+ const endFrame = useEdgeFrame(asset, filmOut, `${clip.id}:out`);
const filmstrip = useMemo(
- () => filmstripFrames(asset, filmIn, w, pps, speed, OVERLAY_H - 4, 24),
- [asset, filmIn, speed, w, pps]
+ () =>
+ filmstripFrames(asset, filmIn, w, pps, speed, OVERLAY_H - 4, 24, {
+ start: startFrame,
+ end: endFrame,
+ }),
+ [asset, filmIn, speed, w, pps, startFrame, endFrame]
);
if (!asset) return null;
diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.ts
index b240b8ca..48517b48 100644
--- a/site/src/cut/lib/media.ts
+++ b/site/src/cut/lib/media.ts
@@ -903,6 +903,117 @@ async function makeThumbs(url: string, duration: number) {
return { thumbs, thumbStep };
}
+// Exact edge frames: a clip's first and last filmstrip tiles show the true
+// frames at its in/out points. Asset thumbs are fixed-interval midpoint
+// samples, so edges are captured on demand at the precise source time. Each
+// clip edge is a "slot" whose newest request supersedes queued ones (trim
+// drags stay cheap); one serial loop performs the seeks on a small pool of
+// shared per-URL video elements.
+const EDGE_CACHE_CAP = 300;
+const EDGE_POOL_CAP = 4;
+
+type EdgeRequest = {
+ url: string;
+ time: number;
+ key: string;
+ resolvers: ((src: string | null) => void)[];
+};
+
+const edgeCache = new Map<string, string>();
+const edgeQueue = new Map<string, EdgeRequest>();
+const edgePool = new Map<string, Promise<HTMLVideoElement>>();
+let edgePumping = false;
+
+function edgeKey(url: string, time: number) {
+ return `${url}#${time.toFixed(2)}`;
+}
+
+/** Synchronous cache read — the frame if a matching capture already landed. */
+export function peekEdgeFrame(url: string, time: number): string | null {
+ return edgeCache.get(edgeKey(url, time)) ?? null;
+}
+
+/** Capture the frame at `time`, latest-wins per `slot` (a clip edge). Resolves
+ * with the frame, or null when superseded by a newer request or on a failed
+ * read. */
+export function requestEdgeFrame(slot: string, url: string, time: number): Promise<string | null> {
+ const key = edgeKey(url, time);
+ const hit = edgeCache.get(key);
+ if (hit) return Promise.resolve(hit);
+ return new Promise((resolve) => {
+ const prev = edgeQueue.get(slot);
+ if (prev?.key === key) {
+ prev.resolvers.push(resolve);
+ } else {
+ prev?.resolvers.forEach((r) => r(null));
+ edgeQueue.set(slot, { url, time, key, resolvers: [resolve] });
+ }
+ void pumpEdgeFrames();
+ });
+}
+
+function edgeVideo(url: string): Promise<HTMLVideoElement> {
+ const hit = edgePool.get(url);
+ if (hit) {
+ // Re-insert to refresh recency; the pool evicts oldest-first.
+ edgePool.delete(url);
+ edgePool.set(url, hit);
+ return hit;
+ }
+ const loading = loadVideoMeta(url);
+ edgePool.set(url, loading);
+ while (edgePool.size > EDGE_POOL_CAP) {
+ const [oldUrl, old] = edgePool.entries().next().value!;
+ edgePool.delete(oldUrl);
+ old
+ .then((v) => {
+ v.removeAttribute("src");
+ v.load();
+ })
+ .catch(() => {});
+ }
+ return loading;
+}
+
+async function pumpEdgeFrames() {
+ if (edgePumping) return;
+ edgePumping = true;
+ try {
+ for (;;) {
+ const next = edgeQueue.entries().next();
+ if (next.done) break;
+ const [slot, req] = next.value;
+ edgeQueue.delete(slot);
+ let src = edgeCache.get(req.key) ?? null;
+ if (!src) {
+ try {
+ const v = await edgeVideo(req.url);
+ const max = Math.max(0, (Number.isFinite(v.duration) ? v.duration : req.time) - 0.05);
+ await seekTo(v, Math.max(0, Math.min(req.time, max)));
+ const aspect = v.videoWidth / Math.max(1, v.videoHeight);
+ const w = Math.max(64, Math.round(THUMB_H * aspect));
+ const canvas = document.createElement("canvas");
+ canvas.width = w;
+ canvas.height = THUMB_H;
+ const ctx = canvas.getContext("2d")!;
+ ctx.imageSmoothingQuality = "high";
+ ctx.drawImage(v, 0, 0, w, THUMB_H);
+ src = canvas.toDataURL("image/jpeg", 0.92);
+ edgeCache.set(req.key, src);
+ while (edgeCache.size > EDGE_CACHE_CAP) {
+ edgeCache.delete(edgeCache.keys().next().value!);
+ }
+ } catch {
+ src = null;
+ }
+ }
+ req.resolvers.forEach((r) => r(src));
+ }
+ } finally {
+ edgePumping = false;
+ }
+}
+
function seekTo(v: HTMLVideoElement, t: number): Promise<void> {
return new Promise((resolve) => {
const done = () => {