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): generate the media an added word is spoken over

EtienneLescot54ea8f60cf9f
diff --git a/electron/media/extensionClip.test.ts b/electron/media/extensionClip.test.ts
new file mode 100644
index 000000000..342ff04c8
--- /dev/null
+++ b/electron/media/extensionClip.test.ts
@@ -0,0 +1,70 @@
+// The one thing worth pinning: the command says what we mean. Running ffmpeg in a unit test
+// would test ffmpeg, not us — the arguments are where a mistake actually lives.
+
+import { describe, expect, it } from "vitest";
+import { extensionClipArgs, extensionClipName } from "./extensionClip";
+
+const SPEC = {
+ sourcePath: "C:/rec/take.mp4",
+ atSec: 5.3,
+ durationSec: 3.6,
+ fps: 30,
+ width: 1920,
+ height: 1080,
+};
+
+describe("extensionClipArgs", () => {
+ const args = extensionClipArgs(SPEC, "C:/out/w1_3600.mp4");
+ const at = (flag: string) => args[args.indexOf(flag) + 1];
+
+ it("seeks BEFORE the input, so ffmpeg does not decode up to the moment", () => {
+ expect(args.indexOf("-ss")).toBeLessThan(args.indexOf("-i"));
+ expect(at("-ss")).toBe("5.300");
+ });
+
+ it("holds exactly the frozen frame for the whole duration", () => {
+ const filter = at("-filter_complex");
+ expect(filter).toContain("trim=end_frame=1");
+ expect(filter).toContain("loop=loop=-1:size=1:start=0");
+ expect(filter).toContain("trim=duration=3.600");
+ });
+
+ it("matches the recording's geometry, so the two concatenate downstream", () => {
+ expect(at("-filter_complex")).toContain("fps=30");
+ expect(at("-filter_complex")).toContain("scale=1920:1080");
+ });
+
+ it("carries an audio track rather than none — silence reads as a broken file", () => {
+ expect(args.some((a) => a.startsWith("anoisesrc="))).toBe(true);
+ expect(args).toContain("1:a");
+ });
+
+ it("bounds the output, so a looping filter cannot run away", () => {
+ // `-t` appears twice on purpose: once on the noise input, once on the output.
+ expect(args.filter((a) => a === "-t")).toHaveLength(2);
+ expect(args[args.length - 1]).toBe("C:/out/w1_3600.mp4");
+ });
+});
+
+describe("extensionClipName", () => {
+ 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));
+ });
+});
+
+describe("the two things reality imposed", () => {
+ it("uses an encoder the bundled LGPL ffmpeg actually has", () => {
+ // `libx264` is GPL and absent: the first real run failed with "Unknown encoder".
+ const args = extensionClipArgs(SPEC, "out.mp4");
+ expect(args).toContain("libopenh264");
+ expect(args).not.toContain("libx264");
+ });
+
+ it("still has a frame rate when the asset does not know its own", () => {
+ // The live project's asset carries `fps: 0` — the probe never filled it in, and a
+ // loop filter with no rate produces nothing.
+ const args = extensionClipArgs({ ...SPEC, fps: 0 }, "out.mp4");
+ expect(args[args.indexOf("-filter_complex") + 1]).toContain("fps=30");
+ });
+});
diff --git a/electron/media/extensionClip.ts b/electron/media/extensionClip.ts
new file mode 100644
index 000000000..5769a1aa7
--- /dev/null
+++ b/electron/media/extensionClip.ts
@@ -0,0 +1,135 @@
+/**
+ * The media an added word is spoken over.
+ *
+ * A word typed into the transcript has no recording behind it. Until there is TTS and frame
+ * generation, the stand-in is the last frame the recording showed, held, over faint noise —
+ * a real file, with real frames and a real audio track, so everything downstream decodes it
+ * like any other media instead of special-casing its absence.
+ *
+ * DERIVED, never authored: the word is the truth, this file is regenerable from it. The name
+ * carries what it was generated from, so a stale one is simply never asked for again, and a
+ * missing one is a regeneration rather than a broken edit.
+ */
+
+import { spawn } from "node:child_process";
+import { access, mkdir } from "node:fs/promises";
+import path from "node:path";
+import { resolveFfmpeg } from "./audioPeaks";
+
+export interface ExtensionClipSpec {
+ /** The recording the frozen frame is taken from. */
+ sourcePath: string;
+ /** Source second to freeze on — the moment the added word follows. */
+ atSec: number;
+ durationSec: number;
+ /** Matched to the recording so the two concatenate without a re-encode downstream.
+ * `0` when the asset was imported before the probe filled it in — see `FALLBACK_FPS`. */
+ fps: number;
+ width: number;
+ height: number;
+}
+
+/** Noise rather than silence: a silent track is indistinguishable from a broken one, and
+ * this stands in for a voice that will be synthesized later. Quiet enough not to startle. */
+const NOISE_AMPLITUDE = 0.02;
+const SAMPLE_RATE = 48_000;
+
+/** The bundled ffmpeg is LGPL, so `libx264` is not in it — `libopenh264` is the software
+ * H.264 encoder every LGPL build carries, on every platform. The hardware encoders
+ * (`h264_nvenc`, `h264_mf`, …) are faster and each needs its own hardware; for a clip of a
+ * few seconds that trade is not worth a platform branch. */
+const VIDEO_ENCODER = "libopenh264";
+
+/** Assets imported before the probe filled `video.fps` carry 0, and the live project has
+ * one. A loop filter needs a rate, so it gets a common one.
+ * ponytail: fixed 30, read the real rate off the source when the probe backfills it. */
+const FALLBACK_FPS = 30;
+
+/**
+ * The ffmpeg arguments, as a pure function so the command can be asserted without running it.
+ *
+ * One pass, no temp file: seek to the moment, keep the single frame there, loop it for the
+ * duration, and mux it against a noise source of the same length.
+ */
+export function extensionClipArgs(spec: ExtensionClipSpec, outPath: string): string[] {
+ const dur = spec.durationSec.toFixed(3);
+ const fps = spec.fps > 0 ? spec.fps : FALLBACK_FPS;
+ return [
+ "-y",
+ "-hide_banner",
+ "-loglevel",
+ "error",
+ // Before `-i`, so ffmpeg seeks rather than decoding up to the moment.
+ "-ss",
+ spec.atSec.toFixed(3),
+ "-i",
+ spec.sourcePath,
+ "-f",
+ "lavfi",
+ "-t",
+ dur,
+ "-i",
+ `anoisesrc=c=pink:a=${NOISE_AMPLITUDE}:r=${SAMPLE_RATE}`,
+ "-filter_complex",
+ `[0:v]trim=end_frame=1,loop=loop=-1:size=1:start=0,fps=${fps},trim=duration=${dur},setpts=PTS-STARTPTS,scale=${spec.width}:${spec.height}[v]`,
+ "-map",
+ "[v]",
+ "-map",
+ "1:a",
+ "-c:v",
+ VIDEO_ENCODER,
+ "-pix_fmt",
+ "yuv420p",
+ "-c:a",
+ "aac",
+ "-t",
+ dur,
+ outPath,
+ ];
+}
+
+/** 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. */
+export function extensionClipName(wordId: string, durationSec: number): string {
+ return `${wordId}_${Math.round(durationSec * 1000)}.mp4`;
+}
+
+/**
+ * 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.
+ */
+export async function ensureExtensionClip(
+ spec: ExtensionClipSpec,
+ wordId: string,
+ outDir: string,
+): Promise<string> {
+ const outPath = path.join(outDir, extensionClipName(wordId, spec.durationSec));
+ try {
+ await access(outPath);
+ return outPath;
+ } catch {
+ // Not there yet — generate it.
+ }
+ const ffmpeg = resolveFfmpeg();
+ if (!ffmpeg) throw new Error("no bundled ffmpeg to generate the extension clip with");
+ await mkdir(outDir, { recursive: true });
+ await run(ffmpeg, extensionClipArgs(spec, outPath));
+ return outPath;
+}
+
+function run(bin: string, args: string[]): Promise<void> {
+ return new Promise((resolve, reject) => {
+ const child = spawn(bin, args, { stdio: ["ignore", "ignore", "pipe"] });
+ let stderr = "";
+ child.stderr?.on("data", (chunk) => {
+ stderr += String(chunk);
+ });
+ child.on("error", reject);
+ child.on("close", (code) =>
+ code === 0
+ ? resolve()
+ : reject(new Error(`ffmpeg exited ${code}${stderr ? `: ${stderr.trim()}` : ""}`)),
+ );
+ });
+}