Skip to content

Write HTML. Render video. Built for agents.

fix(cli): select media streams before reading adopted metadata (#5270)

Abdullah M. Nazife6e5f68eba1f
diff --git a/packages/cli/src/media-use/lib/probe.mjs b/packages/cli/src/media-use/lib/probe.mjs
index 836df6e126..a700f795d1 100644
--- a/packages/cli/src/media-use/lib/probe.mjs
+++ b/packages/cli/src/media-use/lib/probe.mjs
@@ -3,6 +3,7 @@ import { extname } from "node:path";
import { ffprobeBinary } from "./ff-binaries.mjs";
const IMAGE_EXT = new Set([".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg", ".ico"]);
+const AUDIO_EXT = new Set([".mp3", ".wav", ".ogg", ".m4a", ".aac"]);
export function probe(filePath) {
const ext = extname(filePath).toLowerCase();
@@ -18,7 +19,11 @@ export function probe(filePath) {
{ encoding: "utf8", timeout: 5000 },
);
const info = JSON.parse(raw);
- const stream = info.streams?.[0];
+ const streams = info.streams ?? [];
+ const wantedType = AUDIO_EXT.has(ext) ? "audio" : "video";
+ const stream =
+ streams.find((s) => s.codec_type === wantedType && s.disposition?.attached_pic !== 1) ??
+ streams[0];
const format = info.format;
const isImage = IMAGE_EXT.has(ext);
diff --git a/packages/cli/src/media-use/lib/probe.test.mjs b/packages/cli/src/media-use/lib/probe.test.mjs
index fe0f39e55d..194e6009cb 100644
--- a/packages/cli/src/media-use/lib/probe.test.mjs
+++ b/packages/cli/src/media-use/lib/probe.test.mjs
@@ -4,6 +4,79 @@ import { chmodSync, mkdtempSync, writeFileSync, existsSync, rmSync } from "node:
import { join } from "node:path";
import { tmpdir } from "node:os";
import { probe } from "./probe.mjs";
+import childProcess from "node:child_process";
+import { syncBuiltinESMExports } from "node:module";
+
+function mockProbe(t, streams, format = { duration: "2" }) {
+ t.mock.method(childProcess, "execFileSync", () => JSON.stringify({ streams, format }));
+ syncBuiltinESMExports();
+ t.after(() => {
+ t.mock.restoreAll();
+ syncBuiltinESMExports();
+ });
+}
+
+const AUDIO_STREAM = { codec_type: "audio", codec_name: "aac", duration: "3" };
+const VIDEO_STREAM = {
+ codec_type: "video",
+ codec_name: "h264",
+ width: 64,
+ height: 48,
+ duration: "4",
+};
+const COVER_STREAM = {
+ codec_type: "video",
+ codec_name: "mjpeg",
+ width: 100,
+ height: 100,
+ disposition: { attached_pic: 1 },
+};
+
+for (const [name, streams] of [
+ ["audio before video", [AUDIO_STREAM, VIDEO_STREAM]],
+ ["video before audio", [VIDEO_STREAM, AUDIO_STREAM]],
+ ["cover art before video", [COVER_STREAM, AUDIO_STREAM, VIDEO_STREAM]],
+]) {
+ test(`probe selects the video stream with ${name}`, (t) => {
+ mockProbe(t, streams);
+ assert.deepEqual(probe("clip.mp4"), {
+ duration: 2,
+ width: 64,
+ height: 48,
+ codec: "h264",
+ });
+ });
+}
+
+test("probe selects audio metadata for an audio file with cover art", (t) => {
+ mockProbe(t, [COVER_STREAM, AUDIO_STREAM]);
+ assert.deepEqual(probe("song.m4a"), { duration: 2, width: null, height: null, codec: "aac" });
+});
+
+test("probe uses the selected video's duration when the container has none", (t) => {
+ mockProbe(t, [AUDIO_STREAM, VIDEO_STREAM], {});
+ assert.equal(probe("clip.mp4").duration, 4);
+});
+
+test("probe selects image dimensions without reporting an image duration", (t) => {
+ mockProbe(t, [AUDIO_STREAM, VIDEO_STREAM]);
+ assert.deepEqual(probe("frame.png"), {
+ duration: null,
+ width: 64,
+ height: 48,
+ codec: "h264",
+ });
+});
+
+test("probe retains audio-only metadata and the empty-stream fallback", (t) => {
+ mockProbe(t, [AUDIO_STREAM]);
+ assert.deepEqual(probe("clip.mp4"), { duration: 2, width: null, height: null, codec: "aac" });
+});
+
+test("probe retains the container duration when no streams are reported", (t) => {
+ mockProbe(t, []);
+ assert.deepEqual(probe("clip.mp4"), { duration: 2, width: null, height: null, codec: null });
+});
// Regression for the shell-injection fix: probe() must pass the path as a literal
// argv entry, never through a shell. A filename containing shell metacharacters