Skip to content

Open source CapCut alternative.

TypeScript

fix(site): refuse an overlay video over an empty main track [rebuild]

David Le366dab446f27
diff --git a/site/src/cut/components/Timeline.tools.ts b/site/src/cut/components/Timeline.tools.ts
index c9b4ac7b..3f4ec158 100644
--- a/site/src/cut/components/Timeline.tools.ts
+++ b/site/src/cut/components/Timeline.tools.ts
@@ -290,7 +290,7 @@ export const TIMELINE_TOOLS = [
description: "Frame region preset; leave out when passing region",
},
region: obj({
- x: num("Centre of the box, frame fractions (0.5 = the middle), as add_title and add_shape take x; the box may run past the frame, and the frame crops the overhang"),
+ x: num("Centre of the box, frame fractions (0.5 = the middle), as add_title and add_shape take x; the box may run past the frame, and the frame crops the overhang. A box known by its left and top edges centres at left + w/2, top + h/2"),
y: num("Centre of the box, frame fractions (0.5 = the middle)"),
w: num(`Width in frame fractions, 0.05..${REGION_MAX_SCALE}; past 1 the box runs off the frame`),
h: num(`Height in frame fractions, 0.05..${REGION_MAX_SCALE}`),
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index df5bee94..922053fe 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -3163,6 +3163,12 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {
const track = isNum(input.track) ? Math.max(1, Math.round(input.track)) : 1;
if (isNum(input.track) && Math.round(input.track) === 0)
throw new ToolError("Track 0 holds the timeline clips — use place_clip for it.");
+ // A stack grounds on its lowest row, so an overlay over an empty track 0
+ // would become the base clip itself.
+ if (track0Clips(s.clips).length === 0)
+ throw new ToolError(
+ "Track 0 is empty, and an overlay over nothing becomes the base clip itself. Put the clip it sits over on track 0 first (add_clip), then add this."
+ );
// Its length goes in with the add, so the landing makes room for the
// clip it will be: `end` when given, else an image stops where track 0
diff --git a/site/src/cut/lib/overlayEmptyTrackTool.test.ts b/site/src/cut/lib/overlayEmptyTrackTool.test.ts
new file mode 100644
index 00000000..91a9abb1
--- /dev/null
+++ b/site/src/cut/lib/overlayEmptyTrackTool.test.ts
@@ -0,0 +1,38 @@
+import { beforeEach, expect, test } from "bun:test";
+import { runAiTool } from "./aiTools";
+import { useEditor } from "./store";
+import { emptySubtitles } from "./types";
+
+/**
+ * An overlay over an empty timeline. A stack grounds on its lowest row, so a
+ * clip asked onto track 1 with nothing on track 0 became the base clip: a
+ * screen recording meant for a laptop's screen took the whole frame, the
+ * footage added next went in after it, and the call still said it had worked.
+ */
+
+beforeEach(() => {
+ useEditor.setState({
+ projectId: "p1",
+ aspect: "9:16",
+ clips: [],
+ overlays: [],
+ transitions: [],
+ audioClips: [],
+ subtitles: emptySubtitles(),
+ assets: [
+ { id: "desk", fileName: "desk.mov", name: "Desk", type: "video", duration: 60, url: "" },
+ { id: "rec", fileName: "rec.mp4", name: "Recording", type: "video", duration: 29, url: "" },
+ ],
+ });
+});
+
+test("an overlay with nothing on track 0 is refused, naming the clip to add first", async () => {
+ expect(runAiTool("add_overlay_video", { asset_id: "rec", start: 0, track: 1 })).rejects.toThrow("add_clip");
+ expect(useEditor.getState().clips).toHaveLength(0);
+});
+
+test("over a clip on track 0 it lands on the track asked", async () => {
+ await runAiTool("add_clip", { asset_id: "desk" });
+ const out = (await runAiTool("add_overlay_video", { asset_id: "rec", start: 0, track: 1 })) as { track: number };
+ expect(out.track).toBe(1);
+});
diff --git a/site/src/cut/lib/rotationTool.test.ts b/site/src/cut/lib/rotationTool.test.ts
index db12d222..dcf9b34e 100644
--- a/site/src/cut/lib/rotationTool.test.ts
+++ b/site/src/cut/lib/rotationTool.test.ts
@@ -20,8 +20,8 @@ beforeEach(async () => {
subtitles: emptySubtitles(),
assets: [{ id: "a1", fileName: "rec.mov", name: "Rec", type: "video", duration: 10, url: "" }],
});
- // The first lands on the empty main track, the second above it.
- await runAiTool("add_overlay_video", { asset_id: "a1", start: 0 });
+ // A clip on the main track, the overlay above it.
+ await runAiTool("add_clip", { asset_id: "a1" });
await runAiTool("add_overlay_video", { asset_id: "a1", start: 0 });
});