Open source CapCut alternative.
TypeScript
fix(site): bake freeze frames with the encoder the shipped ffmpeg has [rebuild]
David Le066d396ff99a
diff --git a/site/src/cut/server/frames.test.ts b/site/src/cut/server/frames.test.tsnew file mode 100644index 00000000..c06fd584--- /dev/null+++ b/site/src/cut/server/frames.test.ts@@ -0,0 +1,41 @@+import { afterAll, beforeAll, expect, test } from "bun:test";+import { spawnSync } from "node:child_process";+import { mkdir, mkdtemp, rm, stat } from "node:fs/promises";+import os from "node:os";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { makeFreezeFrame } from "./frames";+import { mediaDir } from "./projects";++// A freeze frame is baked by the ffmpeg the editor ships: the LGPL build,+// which has no libx264, so the still has to encode with what that build has.++const VENDOR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../../../vendor/donkey-tools");+const PROJECT = "freeze-test";+let data = "";+let pathBefore: string | undefined;+let dataBefore: string | undefined;++beforeAll(async () => {+ data = await mkdtemp(path.join(os.tmpdir(), "cut-freeze-"));+ dataBefore = process.env.DONKEY_CUT_DATA_DIR;+ pathBefore = process.env.PATH;+ process.env.DONKEY_CUT_DATA_DIR = data;+ process.env.PATH = `${VENDOR}:${pathBefore}`;+ await mkdir(mediaDir(PROJECT), { recursive: true });+ const made = spawnSync("ffmpeg", ["-v", "error", "-f", "lavfi", "-i", "color=c=0x336699:s=64x36", "-frames:v", "1", path.join(mediaDir(PROJECT), "src.png")]);+ if (made.status !== 0) throw new Error(String(made.stderr));+});++afterAll(async () => {+ process.env.PATH = pathBefore;+ if (dataBefore === undefined) delete process.env.DONKEY_CUT_DATA_DIR;+ else process.env.DONKEY_CUT_DATA_DIR = dataBefore;+ await rm(data, { recursive: true, force: true });+});++test("a freeze frame bakes with the shipped ffmpeg", async () => {+ const still = await makeFreezeFrame(PROJECT, "src.png", 0, 1, { w: 128, h: 72 }, { fit: "fill", panX: 0, panY: 0 });+ expect(still).toMatchObject({ duration: 1, width: 128, height: 72 });+ expect((await stat(path.join(mediaDir(PROJECT), still.fileName))).size).toBeGreaterThan(0);+});diff --git a/site/src/cut/server/frames.ts b/site/src/cut/server/frames.tsindex a54514a1..ef09654c 100644--- a/site/src/cut/server/frames.ts+++ b/site/src/cut/server/frames.ts@@ -2,6 +2,7 @@ import { spawn } from "node:child_process";import { mkdtemp, rm } from "node:fs/promises";import os from "node:os";import path from "node:path";+import { h264Encoder, vtQuality } from "./exportPipeline";import { mediaPath } from "./projects";import { assertGraphSafe, fexpr } from "./filterGraph";import { num, round } from "./util";@@ -70,6 +71,9 @@ export interface FreezeFraming {flipV?: boolean;}+/** A freeze's quality on VideoToolbox, as the crf libx264's default gives. */+const FREEZE_CRF = 23;+/*** Render a still-video clip from one frame of a project media file* (a freeze frame), written into the project's media folder.@@ -119,6 +123,9 @@ export async function makeFreezeFrame(`,pad=${w}:${h}:(ow-iw)/2:(oh-ih)/2:black`;}+ // The shipped LGPL ffmpeg has no libx264: the still encodes with what+ // the build carries, as every other render here does.+ const enc = await h264Encoder();await run("ffmpeg", ["-y","-loop", "1",@@ -126,8 +133,9 @@ export async function makeFreezeFrame("-t", dur.toFixed(3),"-r", "30",...(vf ? ["-vf", assertGraphSafe(vf)] : []),- "-c:v", "libx264",- "-preset", "veryfast",+ ...(enc === "libx264"+ ? ["-c:v", "libx264", "-preset", "veryfast"]+ : ["-c:v", "h264_videotoolbox", "-q:v", String(vtQuality(FREEZE_CRF)), "-allow_sw", "1"]),"-pix_fmt", "yuv420p",mediaPath(projectId, fileName),]);