Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
feat(timeline): the insertion layer — a clip reads a list of parts, not a file
EtienneLescot612e1c68ddf3
diff --git a/electron/media/extensionClip.ts b/electron/media/extensionClip.tsindex 5769a1aa7..09dc4f503 100644--- a/electron/media/extensionClip.ts+++ b/electron/media/extensionClip.ts@@ -89,7 +89,10 @@ export function extensionClipArgs(spec: ExtensionClipSpec, outPath: string): str}/** Deterministic, and carries what it was generated from: a word whose text changed asks for- * a different name, so the old file is never mistaken for the new one. */+ * a different name, so the old file is never mistaken for the new one.+ *+ * The duration comes from `extensionDurationSec` — the ONE rule for how long an added word+ * takes — so the file a part asks for and the file this writes are named by the same fact. */export function extensionClipName(wordId: string, durationSec: number): string {return `${wordId}_${Math.round(durationSec * 1000)}.mp4`;}diff --git a/src/lib/ai-edition/timeline/clip-parts.test.ts b/src/lib/ai-edition/timeline/clip-parts.test.tsnew file mode 100644index 000000000..92d5fa955--- /dev/null+++ b/src/lib/ai-edition/timeline/clip-parts.test.ts@@ -0,0 +1,107 @@+// The one property everything above this module depends on: the parts are contiguous, they+// start where the clip starts, and no stored source coordinate moved to achieve it.++import { describe, expect, it } from "vitest";+import type { AxcutClip, AxcutWord } from "../schema";+import { clipParts, extensionDurationSec, partsLengthSec } from "./clip-parts";++const CLIP: AxcutClip = {+ id: "c1",+ assetId: "a1",+ sourceStartSec: 0,+ sourceEndSec: 10,+ timelineStartSec: 0,+ timelineEndSec: 10,+ wordRefs: [],+ origin: "user",+ reason: "",+};++const word = (over: Partial<AxcutWord> & { id: string; startSec: number }): AxcutWord => ({+ segmentId: "s1",+ endSec: over.startSec,+ text: "hello",+ ...over,+});++const added = (id: string, at: number, text: string): AxcutWord =>+ word({ id, startSec: at, text, source: "synth" });++describe("clipParts", () => {+ it("is one recording part when nothing was added", () => {+ expect(clipParts(CLIP, [word({ id: "w1", startSec: 1 })])).toEqual([+ {+ kind: "recording",+ timelineStartSec: 0,+ timelineEndSec: 10,+ sourceStartSec: 0,+ sourceEndSec: 10,+ },+ ]);+ });++ it("splits the recording where the word was added, extension between the halves", () => {+ const parts = clipParts(CLIP, [added("s1", 4, "really")]);+ expect(parts.map((p) => p.kind)).toEqual(["recording", "extension", "recording"]);+ expect(parts[0]).toMatchObject({ sourceStartSec: 0, sourceEndSec: 4, timelineEndSec: 4 });+ // "really" is 6 chars at 15/s.+ expect(parts[1]).toMatchObject({ kind: "extension", wordId: "s1", timelineStartSec: 4 });+ expect(parts[1].timelineEndSec).toBeCloseTo(4.4, 6);+ // The SOURCE window of the second half is untouched — it resumes where it left off.+ expect(parts[2]).toMatchObject({ sourceStartSec: 4, sourceEndSec: 10 });+ expect(parts[2].timelineStartSec).toBeCloseTo(4.4, 6);+ });++ it("leaves every stored source coordinate exactly where it was", () => {+ // The whole reason the extension is appended to the LIST and not spliced into the+ // media's axis: adding one cannot move an anchor anyone else stored.+ const parts = clipParts(CLIP, [added("s1", 4, "really"), added("s2", 7, "quite")]);+ const recorded = parts.filter((p) => p.kind === "recording");+ expect(recorded.map((p) => [p.sourceStartSec, p.sourceEndSec])).toEqual([+ [0, 4],+ [4, 7],+ [7, 10],+ ]);+ });++ it("is contiguous, and starts where the clip starts", () => {+ const parts = clipParts({ ...CLIP, timelineStartSec: 12, timelineEndSec: 22 }, [+ added("s1", 4, "really"),+ added("s2", 7, "quite"),+ ]);+ expect(parts[0].timelineStartSec).toBe(12);+ for (const [i, part] of parts.slice(0, -1).entries()) {+ expect(part.timelineEndSec).toBeCloseTo(parts[i + 1].timelineStartSec, 9);+ }+ });++ it("grows the clip by exactly what was added, and by nothing else", () => {+ const bare = partsLengthSec(clipParts(CLIP, []));+ const grown = partsLengthSec(clipParts(CLIP, [added("s1", 4, "really")]));+ expect(bare).toBeCloseTo(10, 6);+ expect(grown - bare).toBeCloseTo(extensionDurationSec("really"), 6);+ });++ it("ignores a word another clip of the same recording plays", () => {+ const late = { ...CLIP, sourceStartSec: 6, sourceEndSec: 10, timelineEndSec: 4 };+ expect(clipParts(late, [added("s1", 4, "really")]).map((p) => p.kind)).toEqual(["recording"]);+ });++ it("ignores an empty word, which buys nothing", () => {+ expect(clipParts(CLIP, [added("s1", 4, " ")]).map((p) => p.kind)).toEqual(["recording"]);+ });+});++describe("extensionDurationSec", () => {+ it("is the text's own length at the assumed rate", () => {+ expect(extensionDurationSec("really")).toBeCloseTo(6 / 15, 6);+ });++ it("never returns a span too short to be a part", () => {+ expect(extensionDurationSec("a")).toBe(0.15);+ });++ it("is nothing at all for nothing at all", () => {+ expect(extensionDurationSec(" ")).toBe(0);+ });+});diff --git a/src/lib/ai-edition/timeline/clip-parts.ts b/src/lib/ai-edition/timeline/clip-parts.tsnew file mode 100644index 000000000..c86305f5f--- /dev/null+++ b/src/lib/ai-edition/timeline/clip-parts.ts@@ -0,0 +1,120 @@+// The insertion layer: media -> parts -> clip.+//+// A clip does not read a file, it reads a LIST of parts:+//+// [ recording 0→5.3 ] [ extension 0.4s ] [ recording 5.3→20.9 ]+//+// The extension does NOT slide into the media's own axis — it is appended to the clip's+// list. That is the decision the whole design rests on, and its consequence is the point:+// no stored source coordinate ever moves. A word, a trim, a zoom keeps the second it was+// authored at, for ever. Adding or removing an extension cannot corrupt an anchor.+//+// This is the ONLY module that knows an extension exists. Above it, a part is just media+// with a source window and a place on the timeline, and the plain arithmetic every reader+// used before insertions works again.++import type { AxcutClip, AxcutWord } from "../schema";++/** How fast a synthesized voice will be assumed to speak, in characters per second.+ *+ * A stand-in for measuring the real thing: there is no TTS yet, so nothing can say how+ * long the sentence actually takes. It is deliberately one number rather than a model —+ * ponytail: fixed rate, ask the synthesizer for the real duration once there is one. */+const CHARS_PER_SEC = 15;++/** Below this the extension is not worth a part of its own: the clip would gain a few+ * frames nobody asked for and every reader would carry a degenerate span. */+export const MIN_EXTENSION_SEC = 0.15;++/** How long the media for an added word has to be. */+export function extensionDurationSec(text: string): number {+ const chars = text.trim().length;+ if (chars === 0) return 0;+ return Math.max(MIN_EXTENSION_SEC, chars / CHARS_PER_SEC);+}++/** True for a word the user typed in, which no one said and nothing in the media carries. */+export function isAddedWord(word: AxcutWord): boolean {+ return word.source === "synth";+}++export type ClipPart =+ | {+ kind: "recording";+ timelineStartSec: number;+ timelineEndSec: number;+ /** The window of the clip's own asset this part plays. */+ sourceStartSec: number;+ sourceEndSec: number;+ }+ | {+ kind: "extension";+ timelineStartSec: number;+ timelineEndSec: number;+ /** The word this media exists for. Its file is derived from the pair. */+ wordId: string;+ text: string;+ };++/**+ * One clip, as the ordered list of media it actually plays.+ *+ * Added words split the clip's source window where they sit — at the END of the word they+ * follow — and their extension goes between the halves. A word outside the window belongs+ * to another clip of the same recording and is not this clip's business.+ *+ * `words` is the transcript of the clip's OWN asset; passing another's yields the clip+ * unsplit, which is the right answer rather than an error.+ */+export function clipParts(clip: AxcutClip, words: readonly AxcutWord[]): ClipPart[] {+ const sourceEnd = clip.sourceEndSec ?? clip.sourceStartSec;+ const parts: ClipPart[] = [];+ let timeline = clip.timelineStartSec;+ let from = clip.sourceStartSec;++ const added = words+ .filter(+ (word) =>+ isAddedWord(word) &&+ word.startSec > clip.sourceStartSec &&+ word.startSec <= sourceEnd &&+ extensionDurationSec(word.text) > 0,+ )+ .sort((a, b) => a.startSec - b.startSec);++ const pushRecording = (to: number) => {+ if (to - from <= 1e-6) return;+ parts.push({+ kind: "recording",+ timelineStartSec: timeline,+ timelineEndSec: timeline + (to - from),+ sourceStartSec: from,+ sourceEndSec: to,+ });+ timeline += to - from;+ from = to;+ };++ for (const word of added) {+ pushRecording(Math.min(word.startSec, sourceEnd));+ const durationSec = extensionDurationSec(word.text);+ parts.push({+ kind: "extension",+ timelineStartSec: timeline,+ timelineEndSec: timeline + durationSec,+ wordId: word.id,+ text: word.text,+ });+ timeline += durationSec;+ }+ pushRecording(sourceEnd);++ return parts;+}++/** What the clip's timeline length has to be, given its parts. The stored `timelineEndSec`+ * is this — the writer that adds a word is what keeps them equal. */+export function partsLengthSec(parts: readonly ClipPart[]): number {+ if (parts.length === 0) return 0;+ return parts[parts.length - 1].timelineEndSec - parts[0].timelineStartSec;+}