Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

TL3j: the project's media added at the playhead, as CapCut's media panel adds it

shinyobjectze658ea89ea5c
diff --git a/docs/timeline.feature b/docs/timeline.feature
index 416aeac3..d68b06f6 100644
--- a/docs/timeline.feature
+++ b/docs/timeline.feature
@@ -281,6 +281,18 @@ Feature: The timeline SDK (0.1.5): CapCut's timeline, on the composition, in the
Then Cmd+Z and Cmd+Shift+Z step through every operation, the person's and the agent's (E5a), one history
And a drag is one step however many frames it crossed
+ Scenario: TL3j. The project's media, added at the playhead
+ Given the project's footage, stills and sounds (the files its Source page's Assets tab lists)
+ When I open Media in the bar and press a file's +, as CapCut's media panel adds one
+ Then it is a clip at the playhead as long as its file (a still 3 s, CapCut's default): footage and stills on
+ the main track, a sound on a sound track
+ And in a composition it is a sequence holding a <Video>, an <Img> or an <Audio> of the file, named by its path
+ in the composition's media folder (a file elsewhere in the project copied into media/added/ first, since a
+ render reads that folder alone); on the main track with the magnet on, the clips after it move on by its
+ length and it is drawn under what was over them
+ And in a film cut here it goes over the picture on an overlay track (the film's own cut is its scenes) and
+ plays there, a sound plays where it is placed, and Send tells the agent which file went where
+
Rule: TL4. The magnet, snapping and linkage
Scenario: TL4a. The main track's magnet (P)
Given the magnet is on
diff --git a/sdk/timeline/src/ops.ts b/sdk/timeline/src/ops.ts
index 52dc9f14..4bcf5765 100644
--- a/sdk/timeline/src/ops.ts
+++ b/sdk/timeline/src/ops.ts
@@ -348,9 +348,9 @@ function halves(c: Clip, at: Frames, id: string): [Clip, Clip] {
/** `next` made whole after `op` changed `was`: the main track closed (the magnet), followers carried (linkage),
* tracks in time order, empty made tracks gone, transitions only on cuts that are still there. */
function settle(was: Timeline, next: Timeline, op: Op): Timeline {
- const named = new Set("ids" in op && op.ids ? op.ids : "id" in op && typeof op.id === "string" ? [op.id] : [])
+ const named = new Set(op.op === "add" ? [op.clip.id] : "ids" in op && op.ids ? op.ids : "id" in op && typeof op.id === "string" ? [op.id] : [])
let t = next
- if (t.magnet) t = withTracks(t, (k) => (isMain(k) ? { ...k, clips: close(k.clips, op.op === "move" ? was : undefined, named) } : k))
+ if (t.magnet) t = withTracks(t, (k) => (isMain(k) ? { ...k, clips: close(k.clips, op.op === "move" || op.op === "add" ? was : undefined, named) } : k))
if (was.linkage && t.linkage && op.op !== "option") t = follow(was, t, op, named)
t = { ...t, tracks: t.tracks.filter((k) => k.clips.length || was.tracks.some((o) => o.id === k.id)).map((k) => ({ ...k, clips: [...k.clips].sort((a, b) => a.start - b.start) })) }
const cut = (x: Transition) => {
@@ -362,9 +362,11 @@ function settle(was: Timeline, next: Timeline, op: Op): Timeline {
/** The main track's clips with no gap, from frame 0, in the order of their middles. A clip being moved takes its
* place by the edge it leads with, so it swaps with a neighbour once that edge is dragged past the neighbour's
- * middle (TL4a), as CapCut's main track does. */
+ * middle (TL4a), as CapCut's main track does. A clip added goes in at the cut nearest where it was put: before
+ * the clip it lands in when that is nearer its start than its end (CapCut's insert at the playhead, TL3j). */
function close(clips: Clip[], was: Timeline | undefined, named: Set<string>): Clip[] {
const key = (c: Clip) => {
+ if (was && named.has(c.id) && !find(was, c.id)) return c.start
const from = was && named.has(c.id) ? find(was, c.id)?.clip.start : undefined
if (from === undefined || from === c.start) return c.start + c.length / 2
return c.start > from ? end(c) : c.start
diff --git a/sdk/timeline/src/read.ts b/sdk/timeline/src/read.ts
index 572d3219..4b404ea5 100644
--- a/sdk/timeline/src/read.ts
+++ b/sdk/timeline/src/read.ts
@@ -123,6 +123,10 @@ export type SeqEdit =
/** a <Video>'s own sound taken out (TL4c): the <Video> muted, and an <Audio> of the same file, from the same place
* at the same pace and level, in a new sequence from `at` for `frames` after the composition's last */
| { op: "extract"; i: number; at: number; frames: number }
+ /** a file of the project's put on the timeline from `at` for `frames` (TL3j): a new sequence holding a <Video>, an
+ * <Img> or an <Audio> of it. In the code it goes after sequence `after` or before sequence `before` (the main
+ * track's clips either side of it, so it is drawn under what is over them), else after the composition's last */
+ | { op: "media"; at: number; frames: number; kind: "video" | "image" | "audio"; file: string; after?: number; before?: number }
/** The transitions a composition's <Transition> draws (sdk/video transition.tsx). */
export const CODE_KINDS = ["fade", "slide", "wipe", "zoom"]
@@ -332,10 +336,34 @@ export function seqEdits(t: Timeline, op: Op): SeqEdit[] | { why: string } {
const amount = typeof op.clip.props?.amount === "number" ? Math.min(1, Math.max(0, op.clip.props.amount)) : 1
return [{ op: "effect", at: Math.max(0, Math.round(op.clip.start)), frames: Math.max(1, Math.round(op.clip.length)), kind, amount, ...(parts && { parts }) }]
}
+ // a file of the project's (TL3j): a new sequence of its own holding it. On the main track with the magnet on,
+ // the clips after it move on first, and it goes in the code beside the clip before it (or the one after, at
+ // the start), so what is over them stays over it
+ const file = op.clip.props?.asset
+ if (typeof file === "string" && file) {
+ const kind = op.clip.kind
+ if (kind !== "video" && kind !== "image" && kind !== "audio") return { why: "a file goes on the timeline as footage, a still or a sound" }
+ const tried = apply(t, op)
+ if (!tried.ok) return { why: tried.why }
+ const now = find(tried.timeline, op.clip.id)
+ if (!now) return { why: "the file found no track to go on" }
+ const media: SeqEdit = { op: "media", at: now.clip.start, frames: now.clip.length, kind, file }
+ if (now.track.kind !== "main") return [media]
+ const moves: SeqEdit[] = []
+ for (const c of now.track.clips) {
+ if (c.id === op.clip.id) continue
+ const was = find(t, c.id)?.clip, i = seqOf(c.id)
+ if (!was || i === undefined) return { why: "the main track holds something that isn't one of the composition's sequences" }
+ if (was.start !== c.start || was.length !== c.length) moves.push({ op: "move", i, from: c.start, to: end(c) })
+ }
+ const k = now.track.clips.findIndex((c) => c.id === op.clip.id)
+ const before = now.track.clips[k - 1], after = now.track.clips[k + 1]
+ return [...moves, { ...media, ...(before ? { after: seqOf(before.id) } : after ? { before: seqOf(after.id) } : {}) }]
+ }
// a recording made here (its file on this Mac): a new sequence of its own, holding an <Audio>
if (op.clip.kind === "audio" && typeof op.clip.props?.file === "string" && op.clip.props.file) return [{ op: "voice", at: Math.max(0, Math.round(op.clip.start)), frames: Math.max(1, Math.round(op.clip.length)), file: op.clip.props.file }]
// words put on the picture: a new sequence of its own
- if (op.clip.kind !== "text") return { why: "only words, effects and recordings are added to a composition here: footage and sound are its code's to place" }
+ if (op.clip.kind !== "text") return { why: "only words, effects, recordings and the project's files are added to a composition here" }
const style = textStyleOf(op.clip.props?.style)
return [{ op: "text", at: Math.max(0, Math.round(op.clip.start)), frames: Math.max(1, Math.round(op.clip.length)), text: (op.clip.name ?? "Text").trim() || "Text", ...(style && { style }) }]
}
diff --git a/sdk/timeline/test/ops.test.ts b/sdk/timeline/test/ops.test.ts
index 542e43a9..6583621e 100644
--- a/sdk/timeline/test/ops.test.ts
+++ b/sdk/timeline/test/ops.test.ts
@@ -147,6 +147,16 @@ describe("TL4: the magnet and linkage", () => {
expect(spans(done(film(), { op: "move", ids: ["c"], by: -35 }), "main")).toEqual(["a 0-30", "c 30-60", "b 60-120"])
})
+ test("a clip added to the main track goes in at the cut nearest where it was put, the later clips moving on (TL3j)", () => {
+ const add = (start: number, len: number) => spans(done(film(), { op: "add", kind: "main", clip: clip("n", start, len) }), "main")
+ // at 50, inside b (30-90) and nearer its start: in before b
+ expect(add(50, 20)).toEqual(["a 0-30", "n 30-50", "b 50-110", "c 110-140"])
+ // at 70, nearer b's end: after it
+ expect(add(70, 20)).toEqual(["a 0-30", "b 30-90", "n 90-110", "c 110-140"])
+ // at 0, however long it is: first
+ expect(add(0, 40)).toEqual(["n 0-40", "a 40-70", "b 70-130", "c 130-160"])
+ })
+
test("with the magnet off a gap is allowed, and a clip dropped over another goes to a track above", () => {
const off = done(film(), { op: "option", set: { magnet: false } })
expect(spans(done(off, { op: "remove", ids: ["a"] }), "main")).toEqual(["b 30-90", "c 90-120"])
diff --git a/sdk/timeline/test/read.test.ts b/sdk/timeline/test/read.test.ts
index e0b5cd43..af52a159 100644
--- a/sdk/timeline/test/read.test.ts
+++ b/sdk/timeline/test/read.test.ts
@@ -59,7 +59,7 @@ describe("TL0c: a timeline is read from a composition", () => {
expect(seqEdits(t, { op: "set", id: "seq-1", set: { name: " " } })).toEqual({ why: "a text clip says something: delete it to take its words off" })
expect(seqEdits(t, { op: "set", id: "seq-0", set: { name: "x" } })).toEqual({ why: "a sequence's name is its own code's: only a text clip's words change here" })
expect(seqEdits(t, { op: "add", kind: "text", clip: { id: "new", kind: "text", name: "Sale ends Friday", start: 45, length: 90 } })).toEqual([{ op: "text", at: 45, frames: 90, text: "Sale ends Friday" }])
- expect(seqEdits(t, { op: "add", kind: "video", clip: { id: "new", kind: "video", start: 0, length: 30 } })).toEqual({ why: "only words, effects and recordings are added to a composition here: footage and sound are its code's to place" })
+ expect(seqEdits(t, { op: "add", kind: "video", clip: { id: "new", kind: "video", start: 0, length: 30 } })).toEqual({ why: "only words, effects, recordings and the project's files are added to a composition here" })
// a recording made at the timeline (TL8b) is its file, placed
expect(seqEdits(t, { op: "add", kind: "sound", clip: { id: "voice-1", kind: "audio", name: "Voice 1", props: { file: "/p/attachments/voice-1.wav" }, start: 30, length: 75 } })).toEqual([{ op: "voice", at: 30, frames: 75, file: "/p/attachments/voice-1.wav" }])
})
@@ -124,6 +124,28 @@ describe("TL0c: a timeline is read from a composition", () => {
expect(fromSequences([{ ...media[media.length - 1], muted: true }], 30).tracks[0].clips[0].muted).toBeUndefined()
})
+ test("a file of the project's is a new sequence holding it; on the magnet's main track the clips after it move on and it goes in beside the one before it (TL3j)", () => {
+ const t = fromSequences(rows, 30)
+ const add = (kind: string, track: string, start: number, length: number, asset = "/p/media/beach.mp4") => seqEdits(t, { op: "add", kind: track, clip: { id: "m", kind, name: "beach.mp4", start, length, ...(kind !== "image" && { in: 0, source: length }), props: { asset } } })
+ // at frame 100, nearer Second scene's start than its end: in before it, which moves on by its length
+ expect(add("video", "main", 100, 60)).toEqual([
+ { op: "move", i: 3, from: 150, to: 240 },
+ { op: "media", at: 90, frames: 60, kind: "video", file: "/p/media/beach.mp4", after: 0 },
+ ])
+ // at frame 0: first, before First scene, every clip moving on
+ expect(add("image", "main", 0, 90, "/p/media/card.png")).toEqual([
+ { op: "move", i: 0, from: 90, to: 180 },
+ { op: "move", i: 3, from: 180, to: 270 },
+ { op: "media", at: 0, frames: 90, kind: "image", file: "/p/media/card.png", before: 0 },
+ ])
+ // a sound: on a sound track where it was put, nothing moving
+ expect(add("audio", "sound", 40, 75, "/p/media/bed.wav")).toEqual([{ op: "media", at: 40, frames: 75, kind: "audio", file: "/p/media/bed.wav" }])
+ // no magnet: the footage goes over the picture where it was put
+ const gapped = fromSequences([{ ...rows[0], to: 80 }, rows[1]], 30)
+ expect(seqEdits(gapped, { op: "add", kind: "overlay", clip: { id: "m", kind: "video", start: 100, length: 60, in: 0, props: { asset: "/p/media/beach.mp4" } } })).toEqual([{ op: "media", at: 100, frames: 60, kind: "video", file: "/p/media/beach.mp4" }])
+ expect(add("text", "main", 0, 30)).toEqual({ why: "a file goes on the timeline as footage, a still or a sound" })
+ })
+
test("a transition on a cut is the part before going out and the part after coming in, and is read back from them", () => {
const t = fromSequences(rows, 30)
expect(seqEdits(t, { op: "transition", transition: { id: "x-seq-0", kind: "slide", after: "seq-0", length: 15 } })).toEqual([