Open source CapCut alternative.
TypeScript
fix(site): split a frame off a clip, and hand detect_cuts' flicker on [rebuild]
David Leaf8e38ade30c
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.tsindex 617186cb..29c3ea4a 100644--- a/site/src/cut/lib/aiTools.ts+++ b/site/src/cut/lib/aiTools.ts@@ -1746,6 +1746,7 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {frames: c.frames,change: c.change,box: { x: roundT(c.box.x), y: roundT(c.box.y), w: roundT(c.box.w), h: roundT(c.box.h) },+ ...(c.flicker ? { flicker: true as const } : {}),})),...pictureReport(area),...(keepsPicture ? { pictureKept: true as const } : {}),diff --git a/site/src/cut/lib/measureTools.test.ts b/site/src/cut/lib/measureTools.test.tsindex f08d762a..5dbb497b 100644--- a/site/src/cut/lib/measureTools.test.ts+++ b/site/src/cut/lib/measureTools.test.ts@@ -23,10 +23,12 @@ const WHITE: Rgb = [255, 255, 255];/** The picture at frame f, as a colour at each frame fraction. Red until 1s,* a five-frame blue shot, then green. On the green, an orange bar centred at* (0.5, 0.3), 0.1 tall, grows linearly from 0.1 to 0.5 wide over 1.5–2.0s; a- * white "title" block spans x 0.2–0.8, y 0.7–0.75. */+ * white "title" block spans x 0.2–0.8, y 0.7–0.75. From frame 165 to 174 a+ * corner square (x 0.8–0.95, y 0.05–0.2) strobes white and blue a frame each. */function colourAt(f: number, fx: number, fy: number): Rgb {if (f < 60) return RED;if (f < 65) return BLUE;+ if (f >= 165 && f < 175 && fx >= 0.8 && fx < 0.95 && fy >= 0.05 && fy < 0.2) return f % 2 ? WHITE : BLUE;const t = f / FPS;const p = Math.max(0, Math.min(1, (t - 1.5) / 0.5));const bw = 0.1 + 0.4 * p;@@ -188,6 +190,17 @@ test("track_camera reads a held shot as one picture that never moves", async ()expect(out.moves).toEqual([]);});+test("detect_cuts marks a strobe as one flicker change", async () => {+ const out = (await runAiTool("detect_cuts", { asset_id: "ref" })) as {+ changes: { frame: number; frames: number; flicker?: true; box: { x: number; y: number } }[];+ };+ const flicker = out.changes.filter((c) => c.flicker);+ expect(flicker).toHaveLength(1);+ expect(flicker[0].frame).toBeGreaterThanOrEqual(165);+ expect(flicker[0].frame + flicker[0].frames).toBeLessThanOrEqual(176);+ expect(flicker[0].box.x).toBeCloseTo(0.875, 1);+});+test("track_camera from a cut detect_cuts gave starts on that shot's first frame", async () => {// The cut lands on frame 65 (1.0833 s); detect_cuts hands it back to the// millisecond, which can sit either side of the frame's own start.diff --git a/site/src/cut/lib/splitEdit.test.ts b/site/src/cut/lib/splitEdit.test.tsindex 30f57572..4fdec02b 100644--- a/site/src/cut/lib/splitEdit.test.ts+++ b/site/src/cut/lib/splitEdit.test.ts@@ -175,6 +175,14 @@ describe("split_at", () => {expect(useEditor.getState().clips.filter((c) => c.track === 0)).toHaveLength(2);});+ test("a clip splits a single frame off its end, as a per-frame flash needs", async () => {+ useEditor.setState({ clips: [...useEditor.getState().clips, clip("top", 5.167, 6.3, 6.6, { track: 1 })] });+ await runAiTool("split_at", { t: 5.433, ids: ["top"] });+ const tops = useEditor.getState().clips.filter((c) => c.track === 1).sort((x, y) => x.start - y.start);+ expect(tops).toHaveLength(2);+ expect(tops[1].out - tops[1].in).toBeCloseTo(1 / 30, 2);+ });+test("a time on a clip edge says how to reach another item", async () => {useEditor.setState({ clips: [...useEditor.getState().clips, clip("top", 2, 30, 34, { track: 1 })] });await expect(runAiTool("split_at", { t: 4 })).rejects.toThrow("ids");diff --git a/site/src/cut/lib/timelineItems.ts b/site/src/cut/lib/timelineItems.tsindex d9fc7fbb..69b9ad0d 100644--- a/site/src/cut/lib/timelineItems.ts+++ b/site/src/cut/lib/timelineItems.ts@@ -134,6 +134,10 @@ export function pasteTimelineItems(s: ItemLists, items: TimelineClipboardItem[],return { next, selection };}+/** The shortest piece a split leaves: under one frame at 60 fps, so a clip+ * splits a frame at a time, never into a sliver no frame shows. */+const SPLIT_MIN_PIECE = 0.01;+/** Split selected items that cover the cut, in one document edit. */export function splitTimelineItems(s: ItemLists, selection: readonly Selection[], at: number, newId: () => string) {const keys = new Set(selection.flatMap((sel) => sel ? [itemKey(sel)] : []));@@ -145,7 +149,7 @@ export function splitTimelineItems(s: ItemLists, selection: readonly Selection[]const split = def.split;if (!split) continue;const items = def.list(next).flatMap((item) => {- if (!keys.has(`${kind}:${item.id}`) || at <= item.start + 0.05 || at >= item.start + def.duration(item) - 0.05) return [item];+ if (!keys.has(`${kind}:${item.id}`) || at <= item.start + SPLIT_MIN_PIECE || at >= item.start + def.duration(item) - SPLIT_MIN_PIECE) return [item];const [left, tail] = split(def.clone(item), at);const right = { ...tail, id: newId() };selected.push({ kind, id: right.id });