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(media): an extension resolves to a file, and the save generates it

EtienneLescotc67fa22a63d4
diff --git a/electron/ai-edition/document-service.ts b/electron/ai-edition/document-service.ts
index 9c9b958b9..2fca7962c 100644
--- a/electron/ai-edition/document-service.ts
+++ b/electron/ai-edition/document-service.ts
@@ -21,6 +21,7 @@ import {
documentSchema,
migrateRawDocumentToCurrent,
} from "../../src/lib/ai-edition/schema";
+import { ensureDocumentExtensions } from "../media/extensionClip";
import { relinkProjectMedia } from "../media/projectMediaRelinker";
const PROJECT_FILE_EXTENSION = ".openscreen";
@@ -294,6 +295,9 @@ export class DocumentService {
project: { ...parsed.project, updatedAt: new Date().toISOString() },
};
await this.writeProject(stamped);
+ // After the write, never before: a derived file is not worth delaying the user's edit
+ // reaching disk, and a failure to generate one must not fail the save.
+ await ensureDocumentExtensions(stamped);
return stamped;
}
diff --git a/electron/media/extensionClip.test.ts b/electron/media/extensionClip.test.ts
index 342ff04c8..d7199550a 100644
--- a/electron/media/extensionClip.test.ts
+++ b/electron/media/extensionClip.test.ts
@@ -2,7 +2,8 @@
// would test ffmpeg, not us — the arguments are where a mistake actually lives.
import { describe, expect, it } from "vitest";
-import { extensionClipArgs, extensionClipName } from "./extensionClip";
+import { extensionClipPath } from "../../src/lib/ai-edition/timeline/clip-parts";
+import { extensionClipArgs } from "./extensionClip";
const SPEC = {
sourcePath: "C:/rec/take.mp4",
@@ -46,10 +47,26 @@ describe("extensionClipArgs", () => {
});
});
-describe("extensionClipName", () => {
+/** One backslash, built rather than escaped: the escape is what this test keeps losing. */
+const BS = String.fromCharCode(92);
+
+describe("extensionClipPath", () => {
+ it("sits beside the recording it was cut from, in a hidden folder", () => {
+ expect(extensionClipPath("C:/rec/take.mp4", "synth_2", 3.6)).toBe(
+ "C:/rec/.openscreen-extensions/synth_2_3600.mp4",
+ );
+ });
+
it("carries the word and the duration, so a re-typed word asks for a different file", () => {
- expect(extensionClipName("synth_2", 3.6)).toBe("synth_2_3600.mp4");
- expect(extensionClipName("synth_2", 3.8)).not.toBe(extensionClipName("synth_2", 3.6));
+ expect(extensionClipPath("C:/rec/take.mp4", "synth_2", 3.8)).not.toBe(
+ extensionClipPath("C:/rec/take.mp4", "synth_2", 3.6),
+ );
+ });
+
+ it("is the same rule on a Windows path, so both processes name one file", () => {
+ expect(extensionClipPath(`C:${BS}rec${BS}take.mp4`, "w1", 1)).toBe(
+ `C:${BS}rec${BS}.openscreen-extensions${BS}w1_1000.mp4`,
+ );
});
});
diff --git a/electron/media/extensionClip.ts b/electron/media/extensionClip.ts
index 09dc4f503..4b874a66a 100644
--- a/electron/media/extensionClip.ts
+++ b/electron/media/extensionClip.ts
@@ -14,6 +14,12 @@
import { spawn } from "node:child_process";
import { access, mkdir } from "node:fs/promises";
import path from "node:path";
+import type { AxcutWord } from "../../src/lib/ai-edition/schema";
+import {
+ extensionClipPath,
+ extensionDurationSec,
+ isAddedWord,
+} from "../../src/lib/ai-edition/timeline/clip-parts";
import { resolveFfmpeg } from "./audioPeaks";
export interface ExtensionClipSpec {
@@ -88,26 +94,60 @@ 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.
+/**
+ * Every extension the document's words call for, generated if it is not already there.
*
- * 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`;
+ * Called on SAVE, which is the only moment the main process — the one that can spawn ffmpeg
+ * — sees the document. Idempotent by name, so a save that adds nothing costs one `stat` per
+ * added word. A failure is logged and swallowed: the edit is not lost because a derived file
+ * could not be written, and the segment renders black until the next save regenerates it.
+ */
+export async function ensureDocumentExtensions(document: {
+ assets: ReadonlyArray<{
+ id: string;
+ originalPath?: string;
+ video?: { width: number; height: number; fps: number };
+ }>;
+ transcripts: ReadonlyArray<{ assetId: string; words: ReadonlyArray<AxcutWord> }>;
+}): Promise<void> {
+ for (const transcript of document.transcripts) {
+ const asset = document.assets.find((a) => a.id === transcript.assetId);
+ if (!asset?.originalPath) continue;
+ for (const word of transcript.words) {
+ if (!isAddedWord(word)) continue;
+ const durationSec = extensionDurationSec(word.text);
+ if (durationSec <= 0) continue;
+ const outPath = extensionClipPath(asset.originalPath, word.id, durationSec);
+ try {
+ await ensureExtensionClip(
+ {
+ sourcePath: asset.originalPath,
+ atSec: word.startSec,
+ durationSec,
+ fps: asset.video?.fps ?? 0,
+ width: asset.video?.width ?? 0,
+ height: asset.video?.height ?? 0,
+ },
+ outPath,
+ );
+ } catch (error) {
+ console.error(`[extension] ${word.id}: ${(error as Error).message}`);
+ }
+ }
+ }
}
/**
* Generate the file if it is not already there, and return its path.
*
- * Idempotent by name: the same word and duration reuse the file rather than re-encoding.
+ * Idempotent: the same word and duration name the same file, which is reused rather than
+ * re-encoded. The path is decided by `extensionClipPath`, so the renderer names the file it
+ * expects and this writes the file it named — one rule, both sides.
*/
export async function ensureExtensionClip(
spec: ExtensionClipSpec,
- wordId: string,
- outDir: string,
+ outPath: string,
): Promise<string> {
- const outPath = path.join(outDir, extensionClipName(wordId, spec.durationSec));
try {
await access(outPath);
return outPath;
@@ -116,7 +156,7 @@ export async function ensureExtensionClip(
}
const ffmpeg = resolveFfmpeg();
if (!ffmpeg) throw new Error("no bundled ffmpeg to generate the extension clip with");
- await mkdir(outDir, { recursive: true });
+ await mkdir(path.dirname(outPath), { recursive: true });
await run(ffmpeg, extensionClipArgs(spec, outPath));
return outPath;
}
diff --git a/src/lib/ai-edition/timeline/clip-parts.ts b/src/lib/ai-edition/timeline/clip-parts.ts
index c86305f5f..c45abe372 100644
--- a/src/lib/ai-edition/timeline/clip-parts.ts
+++ b/src/lib/ai-edition/timeline/clip-parts.ts
@@ -112,6 +112,25 @@ export function clipParts(clip: AxcutClip, words: readonly AxcutWord[]): ClipPar
return parts;
}
+/** Where the generated media for an added word lives.
+ *
+ * Beside the recording it was cut from, in a hidden sibling folder, and derived by pure
+ * string work from the asset path and the word — so the renderer and the main process
+ * arrive at the same path without asking each other. That is what keeps the file DERIVED:
+ * no one stores it, anyone can name it, and the process that can spawn ffmpeg is the only
+ * one that has to create it.
+ *
+ * The name carries the duration, so a re-typed word asks for a different file and a stale
+ * one is simply never named again. */
+export function extensionClipPath(assetPath: string, wordId: string, durationSec: number): string {
+ const sep = assetPath.includes("\\") ? "\\" : "/";
+ const dir = assetPath.slice(0, Math.max(0, assetPath.lastIndexOf(sep)));
+ return `${dir}${sep}${EXTENSIONS_DIR}${sep}${wordId}_${Math.round(durationSec * 1000)}.mp4`;
+}
+
+/** Hidden, because it is derived: deleting it costs nothing but a regeneration. */
+export const EXTENSIONS_DIR = ".openscreen-extensions";
+
/** 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 {
diff --git a/src/native/sceneDescription.ts b/src/native/sceneDescription.ts
index 6c7981d0f..1832851be 100644
--- a/src/native/sceneDescription.ts
+++ b/src/native/sceneDescription.ts
@@ -40,6 +40,7 @@ import {
import type { AxcutClip, AxcutDocument } from "@/lib/ai-edition/schema";
import { getEditorSettings } from "@/lib/ai-edition/store/editorSettings";
import { assetCameraSource } from "@/lib/ai-edition/timeline/camera";
+import { extensionClipPath } from "@/lib/ai-edition/timeline/clip-parts";
import { resolveClipSourceEndSec } from "@/lib/ai-edition/timeline/clipDuration";
import { removedRawSpans } from "@/lib/ai-edition/timeline/programme-time";
import { takeProgramme } from "@/lib/ai-edition/timeline/take-programme";
@@ -702,6 +703,25 @@ export function buildSceneDescription(
const clips: CompositorClipInput[] = visibleClips.flatMap((clip) => {
const asset = assetById.get(clip.assetId);
if (!asset?.originalPath) return [];
+ // An extension plays GENERATED media, not the recording's. Everything else about the
+ // entry is the clip's — same webcam pairing, same audio expectation — because that is
+ // the point of making it a file rather than a behaviour.
+ if (clip.extensionWordId) {
+ return [
+ {
+ screenPath: extensionClipPath(
+ asset.originalPath,
+ clip.extensionWordId,
+ clip.sourceEndSec ?? 0,
+ ),
+ webcamPath: "",
+ sourceStartSec: 0,
+ sourceEndSec: clip.sourceEndSec ?? 0,
+ webcamOffsetSec: 0,
+ hasAudio: true,
+ },
+ ];
+ }
const camera = assetCameraSource(asset);
// ponytail: `asset.audio` exists in the schema but the probe pipeline never
// populates it, so there is no per-asset "is there a track?" signal to read