Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

refactor(app): move the monitor's derivations into lib/monitor.ts and test them

Jareerd388957165a8
diff --git a/desktop/src/App.tsx b/desktop/src/App.tsx
index 9875f6b7..24c44b8b 100644
--- a/desktop/src/App.tsx
+++ b/desktop/src/App.tsx
@@ -6,11 +6,20 @@ import { getCurrentWindow } from "@tauri-apps/api/window";
import { ConfirmDialog } from "./components/ConfirmDialog";
import { ContextMenu, type ContextTarget } from "./components/ContextMenu";
-import { ExportDialog, type ExportTitle } from "./components/ExportDialog";
+import { ExportDialog } from "./components/ExportDialog";
import { Icon } from "./components/Icon";
import { ALL_MEDIA, MediaBin, type BinFilter } from "./components/MediaBin";
import type { MenuOption } from "./components/Menu";
-import { Preview, type PreviewSource, type TextOverlay } from "./components/Preview";
+import { Preview } from "./components/Preview";
+import {
+ exportClipsOf,
+ exportTitlesOf,
+ previewGhostAt,
+ previewSourceAt,
+ previewVeilAt,
+ textOverlaysAt,
+ type ExportTitle,
+} from "./lib/monitor";
import { Resizer } from "./components/Resizer";
import { RightPanel, type RightTab } from "./components/RightPanel";
import { SaveTemplateDialog } from "./components/SaveTemplateDialog";
@@ -65,7 +74,7 @@ import {
} from "./lib/engine";
import { getTranscriberLanguage, getTranscriberModel } from "./lib/settings";
import { buildChain } from "./lib/filters";
-import { buildEffectChain, findTransition } from "./lib/effects";
+import { findTransition } from "./lib/effects";
import { defaultTextStyle, familyForPath, registerFont } from "./lib/text";
import { useTheme, type Theme } from "./lib/theme";
import { useTransport } from "./lib/transport";
@@ -1069,151 +1078,39 @@ function Editor({
// ── derived views ────────────────────────────────────────────────────────
- const textOverlays = useMemo<TextOverlay[]>(() => {
- const depth = (trackId: string) =>
- timeline.tracks.findIndex((track) => track.id === trackId);
- return clipsAt(project, playhead)
- .filter((clip) => clip.kind === "text" && clip.text !== undefined)
- .filter((clip) => findTrack(project, clip.trackId)?.visible !== false)
- .sort((a, b) => depth(a.trackId) - depth(b.trackId))
- .map((clip) => ({
- clipId: clip.id,
- style: clip.text!,
- offsetX: clip.offsetX,
- offsetY: clip.offsetY,
- }));
- }, [project, timeline, playhead]);
-
- const previewSource = useMemo<PreviewSource | null>(() => {
- const active = clipsAt(project, playhead).filter(
- (clip) => clip.kind !== "audio" && clip.kind !== "text",
- );
- if (active.length === 0) return null;
- const depth = (trackId: string) =>
- timeline.tracks.findIndex((track) => track.id === trackId);
- const top = active.reduce((best, clip) =>
- depth(clip.trackId) > depth(best.trackId) ? clip : best,
- );
- const media = findMedia(project, top.mediaId);
- if (!media) return null;
- return {
- clipId: top.id,
- path: media.path,
- time: top.sourceStart + (playhead - top.start) * top.speed,
- speed: top.speed,
- isStill: top.kind === "image",
- };
- }, [project, timeline, playhead]);
+ // Derived views of the monitor and exporter, all pure in lib/monitor.ts -
+ // which is what makes the cross-fade pre-roll and the flattening testable.
+ const textOverlays = useMemo(
+ () => textOverlaysAt(project, timeline, playhead),
+ [project, timeline, playhead],
+ );
+
+ const previewSource = useMemo(
+ () => previewSourceAt(project, timeline, playhead),
+ [project, timeline, playhead],
+ );
const previewClip = previewSource ? findClip(project, previewSource.clipId) : null;
const previewMedia = previewClip ? findMedia(project, previewClip.mediaId) : null;
- const previewGhost = useMemo(() => {
- for (const clip of timeline.clips) {
- const transition = clip.transitionIn;
- if (!transition || transition.id !== "cross-fade") continue;
- if (clip.kind !== "video" && clip.kind !== "image") continue;
- if (!precedingClip(project, clip.id)) continue;
- const handle =
- clip.kind === "image" ? Infinity : clip.sourceStart / Math.max(0.0625, clip.speed);
- const d = Math.min(transition.duration, handle);
- const cut = clip.start;
- if (d <= 0 || playhead < cut - d || playhead >= cut) continue;
- const media = findMedia(project, clip.mediaId);
- if (!media) continue;
- return {
- clipId: clip.id,
- path: media.path,
- time: Math.max(0, clip.sourceStart - (cut - playhead) * clip.speed),
- speed: clip.speed,
- opacity: 1 - (cut - playhead) / d,
- };
- }
- return null;
- }, [project, timeline, playhead]);
-
- const previewVeil = useMemo(() => {
- for (const clip of timeline.clips) {
- const transition = clip.transitionIn;
- if (!transition) continue;
- if (transition.id !== "fade-black" && transition.id !== "fade-white") continue;
- if (!precedingClip(project, clip.id)) continue;
- const half = transition.duration / 2;
- const cut = clip.start;
- if (playhead < cut - half || playhead > cut + half) continue;
- const opacity =
- playhead <= cut ? (playhead - (cut - half)) / half : (cut + half - playhead) / half;
- return {
- color: transition.id === "fade-white" ? "#ffffff" : "#000000",
- opacity: Math.min(1, Math.max(0, opacity)),
- };
- }
- return null;
- }, [project, timeline, playhead]);
+ const previewGhost = useMemo(
+ () => previewGhostAt(project, timeline, playhead),
+ [project, timeline, playhead],
+ );
+
+ const previewVeil = useMemo(
+ () => previewVeilAt(project, timeline, playhead),
+ [project, timeline, playhead],
+ );
// The exporter works from a flat list of the active timeline's clips.
const exportClips = useMemo<ExportClip[]>(
- () =>
- timeline.clips.flatMap((clip) => {
- if (clip.kind === "text") return [];
- const media = findMedia(project, clip.mediaId);
- const track = findTrack(project, clip.trackId);
- const index = timeline.tracks.findIndex((candidate) => candidate.id === clip.trackId);
- if (!media || !track || index < 0) return [];
- return [
- {
- path: media.path,
- kind: clip.kind as "video" | "audio" | "image",
- start: clip.start,
- duration: clip.duration,
- sourceStart: clip.sourceStart,
- track: index,
- hidden: !track.visible,
- muted: track.muted || clip.muted === true,
- volume: clip.volume,
- fadeIn: clip.fadeIn,
- fadeOut: clip.fadeOut,
- filterChain: buildChain(clip.filters) ?? "",
- speed: clip.speed,
- preservePitch: clip.preservePitch,
- scale: clip.scale,
- offsetX: clip.offsetX,
- offsetY: clip.offsetY,
- rotation: clip.rotation,
- opacity: clip.opacity,
- videoFilterChain: buildEffectChain(clip.videoEffects) ?? "",
- transition:
- clip.transitionIn && precedingClip(project, clip.id)
- ? { kind: clip.transitionIn.id, duration: clip.transitionIn.duration }
- : null,
- mediaWidth: media.width,
- mediaHeight: media.height,
- hasAudio: media.hasAudio,
- },
- ];
- }),
+ () => exportClipsOf(project, timeline),
[project, timeline],
);
const exportTitles = useMemo<ExportTitle[]>(
- () =>
- timeline.clips.flatMap((clip) => {
- if (clip.kind !== "text" || !clip.text) return [];
- const track = findTrack(project, clip.trackId);
- const index = timeline.tracks.findIndex((candidate) => candidate.id === clip.trackId);
- if (!track || !track.visible || index < 0) return [];
- return [
- {
- clipId: clip.id,
- style: clip.text,
- offsetX: clip.offsetX,
- offsetY: clip.offsetY,
- start: clip.start,
- duration: clip.duration,
- track: index,
- },
- ];
- }),
+ () => exportTitlesOf(project, timeline),
[project, timeline],
);
diff --git a/desktop/src/components/ExportDialog.tsx b/desktop/src/components/ExportDialog.tsx
index bf300319..22cb66f7 100644
--- a/desktop/src/components/ExportDialog.tsx
+++ b/desktop/src/components/ExportDialog.tsx
@@ -4,9 +4,9 @@ import { save } from "@tauri-apps/plugin-dialog";
import { revealItemInDir } from "@tauri-apps/plugin-opener";
import type { ExportClip, ExportProgress } from "../lib/engine";
+import type { ExportTitle } from "../lib/monitor";
import { cancelExport, exportProject, onExportProgress, writeCacheFile } from "../lib/engine";
import { rasterizeTitle } from "../lib/rasterize";
-import type { TextStyle } from "../lib/text";
import { shortDuration } from "../lib/time";
import { ErrorNotice } from "./ErrorNotice";
import { Icon } from "./Icon";
@@ -23,17 +23,6 @@ const QUALITIES = [
* dialog rasterises each of these into a full-frame transparent PNG at the
* output size just before the render starts.
*/
-export interface ExportTitle {
- clipId: string;
- style: TextStyle;
- /** Offset from centred, as a fraction of the frame. */
- offsetX: number;
- offsetY: number;
- start: number;
- duration: number;
- /** Index into the track stack, zero being bottom-most. */
- track: number;
-}
type Phase =
| { kind: "idle" }
diff --git a/desktop/src/components/Preview.tsx b/desktop/src/components/Preview.tsx
index 52e95493..e0df115a 100644
--- a/desktop/src/components/Preview.tsx
+++ b/desktop/src/components/Preview.tsx
@@ -9,9 +9,10 @@ import {
import { convertFileSrc } from "@tauri-apps/api/core";
import { buildPreviewLook, type AppliedEffect, type CanvasOp } from "../lib/effects";
+import type { PreviewSource, TextOverlay } from "../lib/monitor";
import { timecode } from "../lib/time";
import { MAX_SCALE, MIN_SCALE } from "../lib/editor";
-import { textCss, type TextStyle } from "../lib/text";
+import { textCss } from "../lib/text";
import { Icon, IconButton } from "./Icon";
import { Menu } from "./Menu";
import { PANEL_SHELL } from "./Panel";
@@ -72,17 +73,6 @@ function ratioLabel(width: number, height: number): string {
}
/** The video clip that should be on screen right now, if any. */
-export interface PreviewSource {
- clipId: string;
- path: string;
- /** Where in the source file the playhead sits, in seconds. */
- time: number;
- /** Playback rate, 1 being normal - the element must run at this rate or it
- drifts from the transport and stutters on every corrective seek. */
- speed: number;
- /** A still is shown as an image; there is nothing to seek or play. */
- isStill: boolean;
-}
/** How the displayed clip's picture sits in the frame. Mirrors the engine's
* `Transform`: scale over the fitted size, offsets as frame fractions from
@@ -94,14 +84,6 @@ export interface PreviewTransform {
rotation: number;
}
-/** A title to draw over the picture, already positioned. */
-export interface TextOverlay {
- clipId: string;
- style: TextStyle;
- /** Offset from centred, as a fraction of the frame. */
- offsetX: number;
- offsetY: number;
-}
/** Alignment guides live while something is being dragged: vertical lines at
* `x` and horizontal ones at `y`, as fractions of the frame. Lists, because a
diff --git a/desktop/src/lib/monitor.test.ts b/desktop/src/lib/monitor.test.ts
new file mode 100644
index 00000000..7c8b6ad6
--- /dev/null
+++ b/desktop/src/lib/monitor.test.ts
@@ -0,0 +1,200 @@
+/**
+ * The monitor's derivations, including the one that mirrors the exporter.
+ *
+ * `previewGhostAt`'s handle clamp is a deliberate twin of the host's
+ * `resolve_transitions` (export.rs): no source handle, shorter dissolve.
+ * If these expectations fail, read the exporter first and fix whichever
+ * side moved - a preview ghost that disagrees with the render is exactly
+ * the bug class this suite exists to keep out.
+ */
+import { describe, expect, test } from "vitest";
+
+import type { Clip, EditorProject } from "./editor";
+import {
+ exportClipsOf,
+ previewGhostAt,
+ previewSourceAt,
+ previewVeilAt,
+ textOverlaysAt,
+} from "./monitor";
+
+function clip(overrides: Partial<Clip> = {}): Clip {
+ return {
+ id: "c1",
+ trackId: "T1",
+ mediaId: "m1",
+ name: "a.mp4",
+ kind: "video",
+ start: 0,
+ duration: 10,
+ sourceStart: 0,
+ volume: 1,
+ fadeIn: 0,
+ fadeOut: 0,
+ scale: 1,
+ offsetX: 0,
+ offsetY: 0,
+ rotation: 0,
+ opacity: 1,
+ speed: 1,
+ preservePitch: true,
+ filters: [],
+ videoEffects: [],
+ ...overrides,
+ };
+}
+
+function project(clips: Clip[]): EditorProject {
+ return {
+ media: [
+ {
+ id: "m1",
+ path: "/a.mp4",
+ name: "a.mp4",
+ duration: 30,
+ kind: "video",
+ width: 1920,
+ height: 1080,
+ frameRate: 30,
+ frameRateFraction: "30/1",
+ videoCodec: "h264",
+ audioCodec: "aac",
+ hasAudio: true,
+ },
+ ],
+ fonts: [],
+ timelines: [
+ {
+ id: "TL1",
+ name: "Timeline 1",
+ tracks: [
+ { id: "T1", name: "Track 1", visible: true, muted: false },
+ { id: "T2", name: "Track 2", visible: true, muted: false },
+ ],
+ clips,
+ },
+ ],
+ activeTimelineId: "TL1",
+ };
+}
+
+const timeline = (p: EditorProject) => p.timelines[0];
+
+describe("previewSourceAt", () => {
+ test("the top-most visual clip wins and maps into its source", () => {
+ const p = project([
+ clip({ id: "under", trackId: "T1" }),
+ clip({ id: "over", trackId: "T2", sourceStart: 5, speed: 2 }),
+ ]);
+ const source = previewSourceAt(p, timeline(p), 3);
+ expect(source?.clipId).toBe("over");
+ // 3s into the clip at 2x from a 5s in-point: the affine map.
+ expect(source?.time).toBe(11);
+ expect(source?.speed).toBe(2);
+ });
+
+ test("a gap shows nothing", () => {
+ const p = project([clip({ start: 5 })]);
+ expect(previewSourceAt(p, timeline(p), 1)).toBeNull();
+ });
+});
+
+describe("previewGhostAt mirrors the exporter's handle clamp", () => {
+ const dissolve = (incoming: Partial<Clip>) =>
+ project([
+ clip({ id: "out", duration: 4 }),
+ clip({
+ id: "in",
+ start: 4,
+ duration: 4,
+ transitionIn: { id: "cross-fade", duration: 1 },
+ ...incoming,
+ }),
+ ]);
+
+ test("inside the window the pre-roll fades in", () => {
+ const p = dissolve({ sourceStart: 2 });
+ const ghost = previewGhostAt(p, timeline(p), 3.5);
+ expect(ghost?.clipId).toBe("in");
+ // Half a second before the cut: half faded, showing the handle.
+ expect(ghost?.opacity).toBeCloseTo(0.5, 10);
+ expect(ghost?.time).toBeCloseTo(1.5, 10);
+ });
+
+ test("no handle, shorter dissolve - exactly like resolve_transitions", () => {
+ // Only 0.25s of source exists before the in-point, so the window is
+ // 0.25s: at 0.5s before the cut there is no ghost yet.
+ const p = dissolve({ sourceStart: 0.25 });
+ expect(previewGhostAt(p, timeline(p), 3.5)).toBeNull();
+ expect(previewGhostAt(p, timeline(p), 3.9)?.opacity).toBeCloseTo(0.6, 10);
+ });
+
+ test("a still has an infinite handle", () => {
+ const p = dissolve({ kind: "image", sourceStart: 0 });
+ expect(previewGhostAt(p, timeline(p), 3.5)).not.toBeNull();
+ });
+
+ test("a cut edited apart orphans the transition", () => {
+ const p = project([
+ clip({ id: "out", duration: 3 }), // ends at 3, the cut is at 4
+ clip({
+ id: "in",
+ start: 4,
+ duration: 4,
+ sourceStart: 2,
+ transitionIn: { id: "cross-fade", duration: 1 },
+ }),
+ ]);
+ expect(previewGhostAt(p, timeline(p), 3.5)).toBeNull();
+ });
+});
+
+describe("previewVeilAt", () => {
+ test("washes in to the cut and out after it, peaking at one", () => {
+ const p = project([
+ clip({ id: "out", duration: 4 }),
+ clip({
+ id: "in",
+ start: 4,
+ duration: 4,
+ sourceStart: 1,
+ transitionIn: { id: "fade-black", duration: 2 },
+ }),
+ ]);
+ expect(previewVeilAt(p, timeline(p), 2.9)).toBeNull();
+ expect(previewVeilAt(p, timeline(p), 3.5)?.opacity).toBeCloseTo(0.5, 10);
+ expect(previewVeilAt(p, timeline(p), 4)?.opacity).toBe(1);
+ expect(previewVeilAt(p, timeline(p), 4.5)?.opacity).toBeCloseTo(0.5, 10);
+ expect(previewVeilAt(p, timeline(p), 5.1)).toBeNull();
+ });
+});
+
+describe("exportClipsOf", () => {
+ test("flattens with track state, chains and audio knowledge", () => {
+ const p = project([
+ clip({ id: "a" }),
+ clip({ id: "text", kind: "text", text: undefined }),
+ ]);
+ p.timelines[0].tracks[0].muted = true;
+
+ const flat = exportClipsOf(p, timeline(p));
+ expect(flat).toHaveLength(1);
+ expect(flat[0].muted).toBe(true);
+ expect(flat[0].hasAudio).toBe(true);
+ expect(flat[0].track).toBe(0);
+ expect(flat[0].transition).toBeNull();
+ });
+});
+
+describe("textOverlaysAt", () => {
+ test("only visible text at the playhead, bottom-most first", () => {
+ const style = { content: "hi" } as never;
+ const p = project([
+ clip({ id: "t2", trackId: "T2", kind: "text", text: style }),
+ clip({ id: "t1", trackId: "T1", kind: "text", text: style }),
+ clip({ id: "late", trackId: "T1", kind: "text", text: style, start: 20 }),
+ ]);
+ const overlays = textOverlaysAt(p, timeline(p), 1);
+ expect(overlays.map((overlay) => overlay.clipId)).toEqual(["t1", "t2"]);
+ });
+});
diff --git a/desktop/src/lib/monitor.ts b/desktop/src/lib/monitor.ts
new file mode 100644
index 00000000..6abcf56b
--- /dev/null
+++ b/desktop/src/lib/monitor.ts
@@ -0,0 +1,241 @@
+/**
+ * Pure derivations of what the monitor and the exporter see.
+ *
+ * Everything here answers one question - given the project and a playhead,
+ * what is on screen, what ghosts in, what washes over, what exports - with
+ * no React and no IO, which is what makes the cross-fade pre-roll arithmetic
+ * and the exporter flattening testable in microseconds. App.tsx memoises
+ * these; nothing else derives them a second way.
+ */
+
+import { buildEffectChain } from "./effects";
+import { buildChain } from "./filters";
+import {
+ clipsAt,
+ findMedia,
+ findTrack,
+ precedingClip,
+ type EditorProject,
+ type TimelineData,
+} from "./editor";
+import type { ExportClip } from "./engine";
+import type { TextStyle } from "./text";
+
+/** The clip the element preview shows: top-most visual clip at the playhead. */
+export interface PreviewSource {
+ clipId: string;
+ path: string;
+ /** Where in the source file the playhead sits, in seconds. */
+ time: number;
+ /** Playback rate, 1 being normal - the element must run at this rate or it
+ drifts from the transport and stutters on every corrective seek. */
+ speed: number;
+ /** A still is shown as an image; there is nothing to seek or play. */
+ isStill: boolean;
+}
+
+/** A title to draw over the picture, already positioned. */
+export interface TextOverlay {
+ clipId: string;
+ style: TextStyle;
+ /** Offset from centred, as a fraction of the frame. */
+ offsetX: number;
+ offsetY: number;
+}
+
+/** A title flattened for the export dialog's rasteriser. */
+export interface ExportTitle {
+ clipId: string;
+ style: TextStyle;
+ /** Offset from centred, as a fraction of the frame. */
+ offsetX: number;
+ offsetY: number;
+ start: number;
+ duration: number;
+ /** Index into the track stack, zero being bottom-most. */
+ track: number;
+}
+
+/** A cross-fade in progress: the incoming clip's pre-roll, faded in. */
+export interface PreviewGhost {
+ clipId: string;
+ path: string;
+ time: number;
+ speed: number;
+ opacity: number;
+}
+
+/** A fade-to-colour transition washing over the playhead. */
+export interface PreviewVeil {
+ color: string;
+ opacity: number;
+}
+
+const depthIn = (timeline: TimelineData) => {
+ return (trackId: string) => timeline.tracks.findIndex((track) => track.id === trackId);
+};
+
+/** Text clips at the playhead on visible tracks, bottom-most first. */
+export function textOverlaysAt(
+ project: EditorProject,
+ timeline: TimelineData,
+ playhead: number,
+): TextOverlay[] {
+ const depth = depthIn(timeline);
+ return clipsAt(project, playhead)
+ .filter((clip) => clip.kind === "text" && clip.text !== undefined)
+ .filter((clip) => findTrack(project, clip.trackId)?.visible !== false)
+ .sort((a, b) => depth(a.trackId) - depth(b.trackId))
+ .map((clip) => ({
+ clipId: clip.id,
+ style: clip.text!,
+ offsetX: clip.offsetX,
+ offsetY: clip.offsetY,
+ }));
+}
+
+/** The top-most visual clip under the playhead, mapped into its source. */
+export function previewSourceAt(
+ project: EditorProject,
+ timeline: TimelineData,
+ playhead: number,
+): PreviewSource | null {
+ const active = clipsAt(project, playhead).filter(
+ (clip) => clip.kind !== "audio" && clip.kind !== "text",
+ );
+ if (active.length === 0) return null;
+ const depth = depthIn(timeline);
+ const top = active.reduce((best, clip) =>
+ depth(clip.trackId) > depth(best.trackId) ? clip : best,
+ );
+ const media = findMedia(project, top.mediaId);
+ if (!media) return null;
+ return {
+ clipId: top.id,
+ path: media.path,
+ time: top.sourceStart + (playhead - top.start) * top.speed,
+ speed: top.speed,
+ isStill: top.kind === "image",
+ };
+}
+
+/**
+ * The incoming half of a cross-fade under the playhead, if one is in its
+ * dissolve window. The handle clamp mirrors the exporter's
+ * `resolve_transitions` exactly: no handle, shorter dissolve.
+ */
+export function previewGhostAt(
+ project: EditorProject,
+ timeline: TimelineData,
+ playhead: number,
+): PreviewGhost | null {
+ for (const clip of timeline.clips) {
+ const transition = clip.transitionIn;
+ if (!transition || transition.id !== "cross-fade") continue;
+ if (clip.kind !== "video" && clip.kind !== "image") continue;
+ if (!precedingClip(project, clip.id)) continue;
+ const handle =
+ clip.kind === "image" ? Infinity : clip.sourceStart / Math.max(0.0625, clip.speed);
+ const d = Math.min(transition.duration, handle);
+ const cut = clip.start;
+ if (d <= 0 || playhead < cut - d || playhead >= cut) continue;
+ const media = findMedia(project, clip.mediaId);
+ if (!media) continue;
+ return {
+ clipId: clip.id,
+ path: media.path,
+ time: Math.max(0, clip.sourceStart - (cut - playhead) * clip.speed),
+ speed: clip.speed,
+ opacity: 1 - (cut - playhead) / d,
+ };
+ }
+ return null;
+}
+
+/** A fade-to-colour wash over the playhead, if a cut's window covers it. */
+export function previewVeilAt(
+ project: EditorProject,
+ timeline: TimelineData,
+ playhead: number,
+): PreviewVeil | null {
+ for (const clip of timeline.clips) {
+ const transition = clip.transitionIn;
+ if (!transition) continue;
+ if (transition.id !== "fade-black" && transition.id !== "fade-white") continue;
+ if (!precedingClip(project, clip.id)) continue;
+ const half = transition.duration / 2;
+ const cut = clip.start;
+ if (playhead < cut - half || playhead > cut + half) continue;
+ const opacity =
+ playhead <= cut ? (playhead - (cut - half)) / half : (cut + half - playhead) / half;
+ return {
+ color: transition.id === "fade-white" ? "#ffffff" : "#000000",
+ opacity: Math.min(1, Math.max(0, opacity)),
+ };
+ }
+ return null;
+}
+
+/** The active timeline flattened for the exporter. Text clips are excluded;
+ * they rasterise separately and rejoin as image clips. */
+export function exportClipsOf(project: EditorProject, timeline: TimelineData): ExportClip[] {
+ return timeline.clips.flatMap((clip) => {
+ if (clip.kind === "text") return [];
+ const media = findMedia(project, clip.mediaId);
+ const track = findTrack(project, clip.trackId);
+ const index = timeline.tracks.findIndex((candidate) => candidate.id === clip.trackId);
+ if (!media || !track || index < 0) return [];
+ return [
+ {
+ path: media.path,
+ kind: clip.kind as "video" | "audio" | "image",
+ start: clip.start,
+ duration: clip.duration,
+ sourceStart: clip.sourceStart,
+ track: index,
+ hidden: !track.visible,
+ muted: track.muted || clip.muted === true,
+ volume: clip.volume,
+ fadeIn: clip.fadeIn,
+ fadeOut: clip.fadeOut,
+ filterChain: buildChain(clip.filters) ?? "",
+ speed: clip.speed,
+ preservePitch: clip.preservePitch,
+ scale: clip.scale,
+ offsetX: clip.offsetX,
+ offsetY: clip.offsetY,
+ rotation: clip.rotation,
+ opacity: clip.opacity,
+ videoFilterChain: buildEffectChain(clip.videoEffects) ?? "",
+ transition:
+ clip.transitionIn && precedingClip(project, clip.id)
+ ? { kind: clip.transitionIn.id, duration: clip.transitionIn.duration }
+ : null,
+ mediaWidth: media.width,
+ mediaHeight: media.height,
+ hasAudio: media.hasAudio,
+ },
+ ];
+ });
+}
+
+/** The active timeline's titles, flattened for rasterisation at export. */
+export function exportTitlesOf(project: EditorProject, timeline: TimelineData): ExportTitle[] {
+ return timeline.clips.flatMap((clip) => {
+ if (clip.kind !== "text" || !clip.text) return [];
+ const track = findTrack(project, clip.trackId);
+ const index = timeline.tracks.findIndex((candidate) => candidate.id === clip.trackId);
+ if (!track || !track.visible || index < 0) return [];
+ return [
+ {
+ clipId: clip.id,
+ style: clip.text,
+ offsetX: clip.offsetX,
+ offsetY: clip.offsetY,
+ start: clip.start,
+ duration: clip.duration,
+ track: index,
+ },
+ ];
+ });
+}