Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

feat(preview): the DOM preview plays an extension, because it is handed a clip

EtienneLescot9a12a2778e26
diff --git a/src/components/ai-edition/NewEditorShell.tsx b/src/components/ai-edition/NewEditorShell.tsx
index e69fe96de..61744066c 100644
--- a/src/components/ai-edition/NewEditorShell.tsx
+++ b/src/components/ai-edition/NewEditorShell.tsx
@@ -32,6 +32,12 @@ import {
import { useUndoRedoShortcuts } from "@/lib/ai-edition/store/undo";
import { useSequentialTimelineOps } from "@/lib/ai-edition/store/useSequentialTimelineOps";
import { useTimeline } from "@/lib/ai-edition/store/useTimeline";
+import {
+ extensionAssetId,
+ extensionClipPath,
+ extensionDurationSec,
+ isAddedWord,
+} from "@/lib/ai-edition/timeline/clip-parts";
import { newRegionDurationSec } from "@/lib/ai-edition/timeline/newRegionDuration";
import { firstTimelineBusyView } from "@/lib/ai-edition/transcription/status";
import {
@@ -355,18 +361,43 @@ export function NewEditorShell() {
const videoSources = useMemo(() => {
if (!document) return [];
- return document.assets.map((asset) => ({
- id: asset.id,
- filePath: /^(https?|blob|data):/.test(asset.originalPath) ? undefined : asset.originalPath,
- // Real Electron assets are filesystem paths and go through toFileUrl.
- // In the browser preview an asset can already point at an http(s)/
- // blob/data URL served by Vite; toFileUrl would mangle those into a
- // broken file:// URL, so pass web URLs through untouched.
- src: /^(https?|blob|data):/.test(asset.originalPath)
- ? asset.originalPath
- : toFileUrl(asset.originalPath),
- label: asset.label,
- }));
+ // One source per added word, beside the recordings. The player keys sources by asset
+ // id and an extension answers to `ext:<wordId>`, so it swaps to the generated file at
+ // the boundary exactly as it swaps between two recordings — no new case.
+ //
+ // A file the save has not written yet simply fails to load, and the player reports it
+ // the way it reports any unreadable source: the edit stands, the picture catches up.
+ const extensions = document.transcripts.flatMap((transcript) => {
+ const asset = document.assets.find((a) => a.id === transcript.assetId);
+ if (!asset?.originalPath) return [];
+ return transcript.words.filter(isAddedWord).flatMap((word) => {
+ const durationSec = extensionDurationSec(word.text);
+ if (durationSec <= 0) return [];
+ const filePath = extensionClipPath(asset.originalPath, word.id, durationSec);
+ return [
+ {
+ id: extensionAssetId(word.id),
+ filePath,
+ src: toFileUrl(filePath),
+ label: word.text,
+ },
+ ];
+ });
+ });
+ return document.assets
+ .map((asset) => ({
+ id: asset.id,
+ filePath: /^(https?|blob|data):/.test(asset.originalPath) ? undefined : asset.originalPath,
+ // Real Electron assets are filesystem paths and go through toFileUrl.
+ // In the browser preview an asset can already point at an http(s)/
+ // blob/data URL served by Vite; toFileUrl would mangle those into a
+ // broken file:// URL, so pass web URLs through untouched.
+ src: /^(https?|blob|data):/.test(asset.originalPath)
+ ? asset.originalPath
+ : toFileUrl(asset.originalPath),
+ label: asset.label,
+ }))
+ .concat(extensions);
}, [document]);
const handleLoadedMetadata = useCallback(
diff --git a/src/components/ai-edition/VirtualPreview.tsx b/src/components/ai-edition/VirtualPreview.tsx
index cf388eb2e..84875bf35 100644
--- a/src/components/ai-edition/VirtualPreview.tsx
+++ b/src/components/ai-edition/VirtualPreview.tsx
@@ -22,6 +22,7 @@ import type {
} from "@/lib/ai-edition/schema";
import { audioGainScalar } from "@/lib/ai-edition/store/editorSettings";
import { useEditorSettings } from "@/lib/ai-edition/store/useEditorSettings";
+import { clipsWithExtensions } from "@/lib/ai-edition/timeline/clip-parts";
import type { PlaybackClockRef } from "@/lib/ai-edition/timeline/playback-clock";
import { removedRawSpans } from "@/lib/ai-edition/timeline/programme-time";
import { findActiveSpeedRegion, type SpeedRegion } from "@/lib/ai-edition/timeline/speed";
@@ -551,8 +552,13 @@ export function VirtualPreview({
// stuck at 0% and the drag range at `max=1`. The refs let the rAF
// always see the latest values without re-creating on every clip
// mutation.
- const clipsRef = useRef(clips);
- clipsRef.current = clips;
+ // The clips this player sees: each extension spliced in as a clip of its own, so the
+ // source-swap and the clock it already has apply to it without a new case. Everything
+ // below reads THIS list — `clips` from the props is the document's, and the difference
+ // is exactly the media an added word is spoken over.
+ const playerClips = useMemo(() => clipsWithExtensions(clips, transcripts), [clips, transcripts]);
+ const clipsRef = useRef(playerClips);
+ clipsRef.current = playerClips;
// Same reason as `clipsRef`: the rAF projects the playhead and each imported
// audio track's head raw→output every frame (see the audio-track loop), and
// must see the live trims, not the set captured when the loop was created.
@@ -593,8 +599,10 @@ export function VirtualPreview({
// source time to a RAW virtual time that jumps discontinuously by exactly the trim's
// width the moment the video itself jumps — matching the marker's own pixel span.
const playbackClips = useMemo(
- () => resolvePlaybackSegments(clips, trimRanges, transcripts),
- [clips, trimRanges, transcripts],
+ // `[]` transcripts, not a forgotten argument: `playerClips` has already spliced the
+ // extensions in, and splicing them twice would play each one twice.
+ () => resolvePlaybackSegments(playerClips, trimRanges, []),
+ [playerClips, trimRanges],
);
const playbackClipsRef = useRef(playbackClips);
playbackClipsRef.current = playbackClips;
diff --git a/src/lib/ai-edition/timeline/clip-parts.test.ts b/src/lib/ai-edition/timeline/clip-parts.test.ts
index 92d5fa955..71bde2a9c 100644
--- a/src/lib/ai-edition/timeline/clip-parts.test.ts
+++ b/src/lib/ai-edition/timeline/clip-parts.test.ts
@@ -3,7 +3,13 @@
import { describe, expect, it } from "vitest";
import type { AxcutClip, AxcutWord } from "../schema";
-import { clipParts, extensionDurationSec, partsLengthSec } from "./clip-parts";
+import {
+ clipParts,
+ clipsWithExtensions,
+ extensionAssetId,
+ extensionDurationSec,
+ partsLengthSec,
+} from "./clip-parts";
const CLIP: AxcutClip = {
id: "c1",
@@ -105,3 +111,34 @@ describe("extensionDurationSec", () => {
expect(extensionDurationSec(" ")).toBe(0);
});
});
+
+// ─── What a player sees ─────────────────────────────────────────────────────
+// The DOM preview already plays several clips over several files and swaps at the boundary.
+// An extension is exactly that, so it is handed a clip rather than taught a new case.
+
+describe("clipsWithExtensions", () => {
+ const transcripts = [{ assetId: "a1", words: [added("synth_1", 4, "really")] }];
+
+ it("splices the extension in as a clip of its own, on its own media", () => {
+ const out = clipsWithExtensions([CLIP], transcripts);
+ expect(out.map((c) => c.assetId)).toEqual(["a1", "ext:synth_1", "a1"]);
+ expect(out[1]).toMatchObject({ sourceStartSec: 0 });
+ expect(out[1].sourceEndSec).toBeCloseTo(6 / 15, 6);
+ });
+
+ it("lays them end to end, so the player's clock never sees a gap", () => {
+ const out = clipsWithExtensions([CLIP], transcripts);
+ expect(out[0].timelineStartSec).toBe(0);
+ for (const [i, clip] of out.slice(0, -1).entries()) {
+ expect(clip.timelineEndSec).toBeCloseTo(out[i + 1].timelineStartSec, 9);
+ }
+ });
+
+ it("returns the clips unchanged when no word was added", () => {
+ expect(clipsWithExtensions([CLIP], [{ assetId: "a1", words: [] }])).toEqual([CLIP]);
+ });
+
+ it("gives the extension an id no real asset can collide with", () => {
+ expect(extensionAssetId("synth_1")).toBe("ext:synth_1");
+ });
+});
diff --git a/src/lib/ai-edition/timeline/clip-parts.ts b/src/lib/ai-edition/timeline/clip-parts.ts
index c45abe372..fcd3bc29b 100644
--- a/src/lib/ai-edition/timeline/clip-parts.ts
+++ b/src/lib/ai-edition/timeline/clip-parts.ts
@@ -128,6 +128,50 @@ export function extensionClipPath(assetPath: string, wordId: string, durationSec
return `${dir}${sep}${EXTENSIONS_DIR}${sep}${wordId}_${Math.round(durationSec * 1000)}.mp4`;
}
+/** The id an extension's media answers to, so a player that keys sources by asset finds it
+ * without knowing what an extension is. Prefixed rather than opaque: it can never collide
+ * with a real asset id, and it says what it is in a log line. */
+export function extensionAssetId(wordId: string): string {
+ return `ext:${wordId}`;
+}
+
+/**
+ * The clips a PLAYER should see: each extension spliced in as a clip of its own.
+ *
+ * The DOM preview already knows how to play several clips over several files and swap at the
+ * boundary. An extension is exactly that — a different file, played for a stretch — so it is
+ * handed one rather than taught a new case. `resolvePlaybackSegments` does the same thing
+ * one layer down, with the trims applied; this is the untrimmed list the player's own clock
+ * maps against.
+ */
+export function clipsWithExtensions(
+ clips: readonly AxcutClip[],
+ transcripts: ReadonlyArray<{ assetId: string; words: readonly AxcutWord[] }>,
+): AxcutClip[] {
+ const wordsByAsset = new Map(transcripts.map((t) => [t.assetId, t.words]));
+ return clips.flatMap((clip) =>
+ clipParts(clip, wordsByAsset.get(clip.assetId) ?? []).map((part) =>
+ part.kind === "extension"
+ ? {
+ ...clip,
+ id: `${clip.id}__ext_${part.wordId}`,
+ assetId: extensionAssetId(part.wordId),
+ sourceStartSec: 0,
+ sourceEndSec: part.timelineEndSec - part.timelineStartSec,
+ timelineStartSec: part.timelineStartSec,
+ timelineEndSec: part.timelineEndSec,
+ }
+ : {
+ ...clip,
+ sourceStartSec: part.sourceStartSec,
+ sourceEndSec: part.sourceEndSec,
+ timelineStartSec: part.timelineStartSec,
+ timelineEndSec: part.timelineEndSec,
+ },
+ ),
+ );
+}
+
/** Hidden, because it is derived: deleting it costs nothing but a regeneration. */
export const EXTENSIONS_DIR = ".openscreen-extensions";