Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: parse audio probe streams without CSV side data ambiguity

Richie McIlroy0b95eeeeef67
diff --git a/apps/media-server/Dockerfile b/apps/media-server/Dockerfile
index e7fc94c90..62d6bac4e 100644
--- a/apps/media-server/Dockerfile
+++ b/apps/media-server/Dockerfile
@@ -15,6 +15,7 @@ RUN bun test src/__tests__/lib/audio-quality-policy.test.ts src/__tests__/lib/au
&& bun test src/__tests__/lib/audio-quality-formats.integration.test.ts src/__tests__/lib/audio-quality-benchmark.test.ts \
&& bun test src/__tests__/lib/drive-resumable-upload.test.ts src/__tests__/lib/storage-upload.test.ts src/__tests__/lib/container-memory.test.ts \
&& bun test src/__tests__/lib/media-size.test.ts src/__tests__/lib/media-probe.integration.test.ts \
+ && bun test src/__tests__/lib/media-audio.integration.test.ts \
&& bun test src/__tests__/lib/recording-verification.integration.test.ts src/__tests__/lib/job-manager.test.ts \
&& bun test src/__tests__/lib/media-transfer.test.ts \
&& bun test src/__tests__/routes/recording-verification.test.ts \
diff --git a/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts b/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
index 30608519c..826d49234 100644
--- a/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/media-audio.integration.test.ts
@@ -104,6 +104,48 @@ describe("mediaAudio integration tests", () => {
expect(hasAudio).toBe(false);
});
+ test.each([
+ [90, true],
+ [-90, true],
+ [180, true],
+ [90, false],
+ ] as const)(
+ "detects audio with display rotation %i and audio=%s",
+ async (rotation, hasAudio) => {
+ const dirPath = await mkdtemp(join(tmpdir(), "cap-audio-rotation-"));
+ const outputPath = join(dirPath, "rotated.mp4");
+ try {
+ const proc = Bun.spawn({
+ cmd: [
+ "ffmpeg",
+ "-v",
+ "error",
+ "-display_rotation",
+ String(rotation),
+ "-i",
+ hasAudio ? TEST_VIDEO_WITH_AUDIO : TEST_VIDEO_NO_AUDIO,
+ "-c",
+ "copy",
+ outputPath,
+ ],
+ stdout: "ignore",
+ stderr: "pipe",
+ });
+ const [stderr, exitCode] = await Promise.all([
+ new Response(proc.stderr).text(),
+ proc.exited,
+ ]);
+ expect(stderr).toBe("");
+ expect(exitCode).toBe(0);
+ expect(await checkHasAudioTrack(`file://${outputPath}`)).toBe(
+ hasAudio,
+ );
+ } finally {
+ await rm(dirPath, { recursive: true, force: true });
+ }
+ },
+ );
+
test("inherits signed queries for relative HLS segments", async () => {
const dirPath = await createHlsFixture();
const requests: string[] = [];
@@ -134,6 +176,21 @@ describe("mediaAudio integration tests", () => {
}
});
+ test("still rejects audio-only inputs without leaking an operation", async () => {
+ const beforeCount = getActiveProcessCount();
+ const dirPath = await mkdtemp(join(tmpdir(), "cap-audio-only-"));
+ const outputPath = join(dirPath, "audio.mp3");
+ try {
+ await Bun.write(outputPath, await extractAudio(TEST_VIDEO_WITH_AUDIO));
+ await expect(
+ checkHasAudioTrack(`file://${outputPath}`),
+ ).rejects.toThrow("No video stream found");
+ await waitForAudioOperations(beforeCount);
+ } finally {
+ await rm(dirPath, { recursive: true, force: true });
+ }
+ });
+
test("contains upstream failures without leaking the active operation", async () => {
const beforeCount = getActiveProcessCount();
const server = Bun.serve({
diff --git a/apps/media-server/src/lib/media-audio.ts b/apps/media-server/src/lib/media-audio.ts
index 9f88c5e50..5e438acbd 100644
--- a/apps/media-server/src/lib/media-audio.ts
+++ b/apps/media-server/src/lib/media-audio.ts
@@ -1,6 +1,7 @@
import { mkdtemp, rm } from "node:fs/promises";
import { join } from "node:path";
import { type Subprocess, spawn } from "bun";
+import { z } from "zod";
import { withTimeout } from "./media-common";
import {
canAcceptNewAudioOperation,
@@ -31,6 +32,9 @@ const MAX_AUDIO_SIZE_BYTES = 100 * 1024 * 1024;
const MAX_STDERR_BYTES = 64 * 1024;
const AUDIO_PROBE_MAX_ATTEMPTS = 3;
const AUDIO_PROBE_RETRY_BASE_MS = 250;
+const AUDIO_PROBE_SCHEMA = z.object({
+ streams: z.array(z.object({ codec_type: z.string().optional() })),
+});
const DEFAULT_OPTIONS: Required<AudioExtractionOptions> = {
format: "mp3",
@@ -144,7 +148,7 @@ function getAudioProbeArgs(inputPath: string): string[] {
"0",
);
}
- args.push("-show_entries", "stream=codec_type", "-of", "csv=p=0", inputPath);
+ args.push("-show_entries", "stream=codec_type", "-of", "json", inputPath);
return args;
}
@@ -224,10 +228,8 @@ async function probeAudioTracks(
const safeStderrText = redactProcessOutput(stderrText, sourceUrl);
if (exitCode === 0) {
- const trackTypes = stdoutText
- .split(/\r?\n/)
- .map((value) => value.trim())
- .filter(Boolean);
+ const { streams } = AUDIO_PROBE_SCHEMA.parse(JSON.parse(stdoutText));
+ const trackTypes = streams.map((stream) => stream.codec_type);
if (!trackTypes.includes("video")) {
throw new Error("No video stream found");
}