Skip to content

Open source CapCut alternative.

TypeScript

fix(cut): an imported link keeps its resolution [rebuild]

David Le7538d954bafb
diff --git a/site/src/cut/server/urlDownload.e2e.test.ts b/site/src/cut/server/urlDownload.e2e.test.ts
new file mode 100644
index 00000000..393fa21b
--- /dev/null
+++ b/site/src/cut/server/urlDownload.e2e.test.ts
@@ -0,0 +1,102 @@
+import { describe, expect, test } from "bun:test";
+import { spawnSync } from "node:child_process";
+import { mkdtemp, rm } from "node:fs/promises";
+import os from "node:os";
+import path from "node:path";
+import { playableMedia } from "./urlDownload";
+
+// What a link import lands has to play on every surface it reaches: the
+// phone's viewer and the Mac's player go through AVFoundation, which decodes
+// H.264 and nothing like VP9 or AV1. This hands the gate real files in the
+// codecs those sites actually serve and checks what comes out the other side.
+
+const available = (cmd: string) =>
+ spawnSync(cmd, ["-version"], { stdio: "ignore" }).status === 0;
+const tools = available("ffmpeg") && available("ffprobe");
+
+const ff = (args: string[]) => {
+ const r = spawnSync("ffmpeg", ["-hide_banner", "-loglevel", "error", "-y", ...args], {
+ encoding: "utf8",
+ });
+ if (r.status !== 0) throw new Error(`ffmpeg ${args.join(" ")} failed:\n${r.stderr}`);
+};
+
+const probe = (file: string) => {
+ const r = spawnSync(
+ "ffprobe",
+ ["-v", "error", "-show_entries", "stream=codec_type,codec_name,pix_fmt", "-of", "json", file],
+ { encoding: "utf8" }
+ );
+ const streams = (JSON.parse(r.stdout).streams ?? []) as {
+ codec_type?: string;
+ codec_name?: string;
+ pix_fmt?: string;
+ }[];
+ return {
+ audio: streams.find((s) => s.codec_type === "audio")?.codec_name,
+ pixFmt: streams.find((s) => s.codec_type === "video")?.pix_fmt,
+ video: streams.find((s) => s.codec_type === "video")?.codec_name,
+ };
+};
+
+const source = (dir: string, name: string, encode: string[]) => {
+ const file = path.join(dir, name);
+ ff([
+ "-f", "lavfi", "-i", "testsrc=size=320x240:rate=15:duration=1",
+ "-f", "lavfi", "-i", "sine=frequency=440:duration=1",
+ ...encode,
+ file,
+ ]);
+ return file;
+};
+
+describe.skipIf(!tools)("playableMedia", () => {
+ test("an H.264 mp4 is handed back untouched", async () => {
+ const dir = await mkdtemp(path.join(os.tmpdir(), "cut-play-"));
+ try {
+ const file = source(dir, "h264.mp4", ["-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac"]);
+ expect(await playableMedia(file)).toBe(file);
+ } finally {
+ await rm(dir, { force: true, recursive: true });
+ }
+ });
+
+ test("a VP9 mp4 comes back as H.264", async () => {
+ const dir = await mkdtemp(path.join(os.tmpdir(), "cut-play-"));
+ try {
+ const file = source(dir, "vp9.mp4", [
+ "-c:v", "libvpx-vp9", "-b:v", "200k", "-pix_fmt", "yuv420p", "-c:a", "aac",
+ ]);
+ expect(probe(file).video).toBe("vp9");
+ const out = await playableMedia(file);
+ expect(out).not.toBe(file);
+ expect(probe(out)).toEqual({ audio: "aac", pixFmt: "yuv420p", video: "h264" });
+ } finally {
+ await rm(dir, { force: true, recursive: true });
+ }
+ }, 120_000);
+
+ test("HEVC picture and Opus sound both come back playable", async () => {
+ const dir = await mkdtemp(path.join(os.tmpdir(), "cut-play-"));
+ try {
+ const file = source(dir, "hevc.mp4", [
+ "-c:v", "libx265", "-tag:v", "hvc1", "-pix_fmt", "yuv420p", "-c:a", "libopus",
+ ]);
+ const out = await playableMedia(file);
+ expect(probe(out)).toEqual({ audio: "aac", pixFmt: "yuv420p", video: "h264" });
+ } finally {
+ await rm(dir, { force: true, recursive: true });
+ }
+ }, 120_000);
+
+ test("sound with no picture is left alone", async () => {
+ const dir = await mkdtemp(path.join(os.tmpdir(), "cut-play-"));
+ try {
+ const file = path.join(dir, "audio.m4a");
+ ff(["-f", "lavfi", "-i", "sine=frequency=440:duration=1", "-c:a", "aac", file]);
+ expect(await playableMedia(file)).toBe(file);
+ } finally {
+ await rm(dir, { force: true, recursive: true });
+ }
+ });
+});
diff --git a/site/src/cut/server/urlDownload.ts b/site/src/cut/server/urlDownload.ts
index 13438b66..13050d56 100644
--- a/site/src/cut/server/urlDownload.ts
+++ b/site/src/cut/server/urlDownload.ts
@@ -187,7 +187,7 @@ const IMAGE_URL_RE = /\.(png|jpe?g|webp|gif|avif|bmp)(?:$|\?)/i;
* back untouched; anything else is re-encoded into one that plays. A
* conversion that fails leaves the original: a file that plays in fewer places
* beats no import at all. */
-async function playableMedia(file: string): Promise<string> {
+export async function playableMedia(file: string): Promise<string> {
const codecs = await streamCodecs(file);
// Sound alone decodes everywhere the import goes.
if (!codecs.video) return file;
@@ -744,14 +744,17 @@ function ytDlp(
// a ready-made mp4 first would land whatever single file the site keeps
// around for compatibility — on YouTube that is 360p next to a 4K original.
//
- // Codec leads the sort. Apple decodes H.264 and HEVC and nothing else, so
- // a taller VP9 or AV1 stream is a file that plays nowhere the import is
- // going: the phone's viewer, the Mac's own player, Safari. Instagram
- // serves 1440p VP9 beside 1080p H.264, and the 1080p file is the one worth
- // having. A source with no H.264 at all still comes down, and the
+ // Codec leads the sort, resolution follows it. Apple decodes H.264 and
+ // HEVC and nothing else, so a taller VP9 or AV1 stream is a file that
+ // plays nowhere the import is going: the phone's viewer, the Mac's own
+ // player, Safari. Instagram serves 1440p VP9 beside a 1080p-class H.264
+ // file, and the H.264 one is worth having. Sound is compared last, after
+ // the picture: judged any earlier it would rank YouTube's 360p
+ // sound-and-picture file over the 1080p stream that carries no sound of
+ // its own. A source with no H.264 at all still comes down, and the
// conversion below makes it playable.
"-f", "bestvideo*+bestaudio/best",
- "-S", "vcodec:h264,acodec:aac,res,fps,hdr:sdr",
+ "-S", "vcodec:h264,res,fps,hdr:sdr,acodec:aac",
"--merge-output-format", "mp4",
"-o", path.join(dir, "%(id)s.%(ext)s"),
"--print-json",