Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Switch audio extraction to MP3 and improve health checks

Richie McIlroyfc22bc8e672c
diff --git a/apps/media-server/src/__tests__/lib/ffmpeg.integration.test.ts b/apps/media-server/src/__tests__/lib/ffmpeg.integration.test.ts
index af1510e4f..d736cfc8a 100644
--- a/apps/media-server/src/__tests__/lib/ffmpeg.integration.test.ts
+++ b/apps/media-server/src/__tests__/lib/ffmpeg.integration.test.ts
@@ -26,12 +26,13 @@ describe("ffmpeg integration tests", () => {
expect(audioData).toBeInstanceOf(Uint8Array);
expect(audioData.length).toBeGreaterThan(0);
- const hasFtypBox =
- audioData[4] === 0x66 &&
- audioData[5] === 0x74 &&
- audioData[6] === 0x79 &&
- audioData[7] === 0x70;
- expect(hasFtypBox).toBe(true);
+ const hasId3Tag =
+ audioData[0] === 0x49 &&
+ audioData[1] === 0x44 &&
+ audioData[2] === 0x33;
+ const hasMpegSync =
+ audioData[0] === 0xff && (audioData[1] & 0xe0) === 0xe0;
+ expect(hasId3Tag || hasMpegSync).toBe(true);
});
test("throws error for video without audio track", async () => {
diff --git a/apps/media-server/src/__tests__/routes/audio.test.ts b/apps/media-server/src/__tests__/routes/audio.test.ts
index 9a212dd2c..08ffdc65f 100644
--- a/apps/media-server/src/__tests__/routes/audio.test.ts
+++ b/apps/media-server/src/__tests__/routes/audio.test.ts
@@ -151,7 +151,7 @@ describe("POST /audio/extract", () => {
);
expect(response.status).toBe(200);
- expect(response.headers.get("Content-Type")).toBe("audio/mp4");
+ expect(response.headers.get("Content-Type")).toBe("audio/mpeg");
expect(response.headers.get("Content-Length")).toBe(
mockAudioData.length.toString(),
);
diff --git a/apps/media-server/src/__tests__/routes/health.test.ts b/apps/media-server/src/__tests__/routes/health.test.ts
index e82a35156..1b26c3c80 100644
--- a/apps/media-server/src/__tests__/routes/health.test.ts
+++ b/apps/media-server/src/__tests__/routes/health.test.ts
@@ -2,11 +2,14 @@ import { describe, expect, test } from "bun:test";
import app from "../../app";
describe("GET /health", () => {
- test("returns status ok", async () => {
+ test("returns status ok with ffmpeg info", async () => {
const response = await app.fetch(new Request("http://localhost/health"));
expect(response.status).toBe(200);
const data = await response.json();
- expect(data).toEqual({ status: "ok" });
+ expect(data.status).toBe("ok");
+ expect(data.ffmpeg).toBeDefined();
+ expect(data.ffmpeg.available).toBe(true);
+ expect(typeof data.ffmpeg.version).toBe("string");
});
});
diff --git a/apps/media-server/src/lib/ffmpeg.ts b/apps/media-server/src/lib/ffmpeg.ts
index 7d5d5f8b1..7ad44d266 100644
--- a/apps/media-server/src/lib/ffmpeg.ts
+++ b/apps/media-server/src/lib/ffmpeg.ts
@@ -1,14 +1,14 @@
import { spawn } from "bun";
export interface AudioExtractionOptions {
- format?: "m4a";
- codec?: "aac";
+ format?: "mp3";
+ codec?: "libmp3lame";
bitrate?: string;
}
const DEFAULT_OPTIONS: Required<AudioExtractionOptions> = {
- format: "m4a",
- codec: "aac",
+ format: "mp3",
+ codec: "libmp3lame",
bitrate: "128k",
};
@@ -22,9 +22,7 @@ export async function checkHasAudioTrack(videoUrl: string): Promise<boolean> {
const stderrText = await new Response(proc.stderr).text();
await proc.exited;
- const hasAudio = /Stream #\d+:\d+.*Audio:/.test(stderrText);
- console.log(`[ffmpeg] Video has audio track: ${hasAudio}`);
- return hasAudio;
+ return /Stream #\d+:\d+.*Audio:/.test(stderrText);
}
export async function extractAudio(
@@ -43,14 +41,10 @@ export async function extractAudio(
"-b:a",
opts.bitrate,
"-f",
- "ipod",
- "-movflags",
- "+frag_keyframe+empty_moov",
+ "mp3",
"pipe:1",
];
- console.log(`[ffmpeg] Starting audio extraction: ${ffmpegArgs.join(" ")}`);
-
const proc = spawn({
cmd: ffmpegArgs,
stdout: "pipe",
@@ -64,13 +58,9 @@ export async function extractAudio(
]);
if (exitCode !== 0) {
- console.error(`[ffmpeg] Audio extraction failed:\n${stderrText}`);
- throw new Error(`FFmpeg exited with code ${exitCode}`);
+ throw new Error(`FFmpeg exited with code ${exitCode}: ${stderrText}`);
}
- console.log(
- `[ffmpeg] Audio extraction complete, size: ${stdout.byteLength} bytes`,
- );
return new Uint8Array(stdout);
}
@@ -90,27 +80,15 @@ export async function extractAudioStream(
"-b:a",
opts.bitrate,
"-f",
- "ipod",
- "-movflags",
- "+frag_keyframe+empty_moov",
+ "mp3",
"pipe:1",
];
- console.log(
- `[ffmpeg] Starting audio extraction (stream): ${ffmpegArgs.join(" ")}`,
- );
-
const proc = spawn({
cmd: ffmpegArgs,
stdout: "pipe",
stderr: "pipe",
});
- proc.exited.then((code) => {
- if (code !== 0) {
- console.error(`[ffmpeg] Stream extraction failed with code ${code}`);
- }
- });
-
return proc.stdout as ReadableStream<Uint8Array>;
}
diff --git a/apps/media-server/src/routes/audio.ts b/apps/media-server/src/routes/audio.ts
index b8ef1ffd1..253d6e235 100644
--- a/apps/media-server/src/routes/audio.ts
+++ b/apps/media-server/src/routes/audio.ts
@@ -67,7 +67,7 @@ audio.post("/extract", async (c) => {
return new Response(Buffer.from(audioData), {
headers: {
- "Content-Type": "audio/mp4",
+ "Content-Type": "audio/mpeg",
"Content-Length": audioData.length.toString(),
},
});
diff --git a/apps/media-server/src/routes/health.ts b/apps/media-server/src/routes/health.ts
index 1fbd83e89..0488d0181 100644
--- a/apps/media-server/src/routes/health.ts
+++ b/apps/media-server/src/routes/health.ts
@@ -1,9 +1,38 @@
+import { spawn } from "bun";
import { Hono } from "hono";
const health = new Hono();
-health.get("/", (c) => {
- return c.json({ status: "ok" });
+health.get("/", async (c) => {
+ let ffmpegVersion = "unknown";
+ let ffmpegAvailable = false;
+
+ try {
+ const proc = spawn({
+ cmd: ["ffmpeg", "-version"],
+ stdout: "pipe",
+ stderr: "pipe",
+ });
+
+ const stdout = await new Response(proc.stdout).text();
+ const exitCode = await proc.exited;
+
+ if (exitCode === 0) {
+ ffmpegAvailable = true;
+ const versionMatch = stdout.match(/ffmpeg version (\S+)/);
+ if (versionMatch) {
+ ffmpegVersion = versionMatch[1];
+ }
+ }
+ } catch {}
+
+ return c.json({
+ status: ffmpegAvailable ? "ok" : "degraded",
+ ffmpeg: {
+ available: ffmpegAvailable,
+ version: ffmpegVersion,
+ },
+ });
});
export default health;
diff --git a/apps/web/__tests__/unit/audio-extract.test.ts b/apps/web/__tests__/unit/audio-extract.test.ts
index 6f7aeb4eb..7a4fefb63 100644
--- a/apps/web/__tests__/unit/audio-extract.test.ts
+++ b/apps/web/__tests__/unit/audio-extract.test.ts
@@ -132,12 +132,12 @@ describe("audio-extract", () => {
expect(args).toContain("https://example.com/video.mp4");
expect(args).toContain("-vn");
expect(args).toContain("-acodec");
- expect(args).toContain("aac");
+ expect(args).toContain("libmp3lame");
expect(args).toContain("-b:a");
expect(args).toContain("128k");
});
- it("returns audio/mp4 mime type", async () => {
+ it("returns audio/mpeg mime type", async () => {
const { extractAudioFromUrl } = await import("@/lib/audio-extract");
const resultPromise = extractAudioFromUrl(
@@ -149,10 +149,10 @@ describe("audio-extract", () => {
}, 10);
const result = await resultPromise;
- expect(result.mimeType).toBe("audio/mp4");
+ expect(result.mimeType).toBe("audio/mpeg");
});
- it("generates .m4a file in temp directory", async () => {
+ it("generates .mp3 file in temp directory", async () => {
const { extractAudioFromUrl } = await import("@/lib/audio-extract");
const resultPromise = extractAudioFromUrl(
@@ -165,7 +165,7 @@ describe("audio-extract", () => {
const result = await resultPromise;
expect(result.filePath).toContain("audio-");
- expect(result.filePath).toContain(".m4a");
+ expect(result.filePath).toContain(".mp3");
});
it("provides cleanup function", async () => {
diff --git a/apps/web/__tests__/unit/media-client.test.ts b/apps/web/__tests__/unit/media-client.test.ts
index a459fd3bd..7264fe4cb 100644
--- a/apps/web/__tests__/unit/media-client.test.ts
+++ b/apps/web/__tests__/unit/media-client.test.ts
@@ -183,7 +183,7 @@ describe("media-client", () => {
await expect(
extractAudioViaMediaServer("https://example.com/video.mp4"),
- ).rejects.toThrow("Failed to extract audio");
+ ).rejects.toThrow("FFmpeg process exited with code 1");
});
});
});
diff --git a/apps/web/lib/audio-extract.ts b/apps/web/lib/audio-extract.ts
index bcd2604fb..ae039a8ab 100644
--- a/apps/web/lib/audio-extract.ts
+++ b/apps/web/lib/audio-extract.ts
@@ -25,7 +25,6 @@ function getFfmpegPath(): string {
for (const path of candidatePaths) {
if (existsSync(path)) {
- console.log(`[audio-extract] Found FFmpeg at: ${path}`);
cachedFfmpegPath = path;
return path;
}
@@ -46,31 +45,23 @@ export async function extractAudioFromUrl(
videoUrl: string,
): Promise<AudioExtractionResult> {
const ffmpeg = getFfmpegPath();
- const outputPath = join(tmpdir(), `audio-${randomUUID()}.m4a`);
+ const outputPath = join(tmpdir(), `audio-${randomUUID()}.mp3`);
const ffmpegArgs = [
"-i",
videoUrl,
"-vn",
"-acodec",
- "aac",
+ "libmp3lame",
"-b:a",
"128k",
"-f",
- "ipod",
- "-movflags",
- "+faststart",
+ "mp3",
"-y",
outputPath,
];
return new Promise((resolve, reject) => {
- console.log(
- "[audio-extract] FFmpeg started:",
- ffmpeg,
- ffmpegArgs.join(" "),
- );
-
const proc = spawn(ffmpeg, ffmpegArgs, { stdio: ["pipe", "pipe", "pipe"] });
let stderr = "";
@@ -80,28 +71,24 @@ export async function extractAudioFromUrl(
});
proc.on("error", (err: Error) => {
- console.error("[audio-extract] FFmpeg error:", err);
fs.unlink(outputPath).catch(() => {});
reject(new Error(`Audio extraction failed: ${err.message}`));
});
proc.on("close", (code: number | null) => {
if (code === 0) {
- console.log("[audio-extract] Audio extraction complete");
resolve({
filePath: outputPath,
- mimeType: "audio/mp4",
+ mimeType: "audio/mpeg",
cleanup: async () => {
try {
await fs.unlink(outputPath);
- console.log("[audio-extract] Cleaned up temp file:", outputPath);
} catch {}
},
});
} else {
- console.error("[audio-extract] FFmpeg stderr:", stderr);
fs.unlink(outputPath).catch(() => {});
- reject(new Error(`Audio extraction failed with code ${code}`));
+ reject(new Error(`Audio extraction failed with code ${code}: ${stderr}`));
}
});
});
@@ -114,13 +101,11 @@ export async function extractAudioToBuffer(videoUrl: string): Promise<Buffer> {
videoUrl,
"-vn",
"-acodec",
- "aac",
+ "libmp3lame",
"-b:a",
"128k",
"-f",
- "ipod",
- "-movflags",
- "+frag_keyframe+empty_moov",
+ "mp3",
"-pipe:1",
];
@@ -139,17 +124,14 @@ export async function extractAudioToBuffer(videoUrl: string): Promise<Buffer> {
});
proc.on("error", (err: Error) => {
- console.error("[audio-extract] FFmpeg error:", err);
reject(new Error(`Audio extraction failed: ${err.message}`));
});
proc.on("close", (code: number | null) => {
if (code === 0) {
- console.log("[audio-extract] Audio extraction to buffer complete");
resolve(Buffer.concat(chunks));
} else {
- console.error("[audio-extract] FFmpeg stderr:", stderr);
- reject(new Error(`Audio extraction failed with code ${code}`));
+ reject(new Error(`Audio extraction failed with code ${code}: ${stderr}`));
}
});
});
@@ -159,8 +141,7 @@ export async function checkHasAudioTrack(videoUrl: string): Promise<boolean> {
let ffmpeg: string;
try {
ffmpeg = getFfmpegPath();
- } catch (err) {
- console.error("[audio-extract] FFmpeg binary not found:", err);
+ } catch {
return false;
}
const ffmpegArgs = ["-i", videoUrl, "-hide_banner"];
@@ -176,15 +157,12 @@ export async function checkHasAudioTrack(videoUrl: string): Promise<boolean> {
stderr += data.toString();
});
- proc.on("error", (err: Error) => {
- console.error("[audio-extract] FFmpeg error:", err);
+ proc.on("error", () => {
resolve(false);
});
proc.on("close", () => {
- const hasAudio = /Stream #\d+:\d+.*Audio:/.test(stderr);
- console.log(`[audio-extract] Video has audio track: ${hasAudio}`);
- resolve(hasAudio);
+ resolve(/Stream #\d+:\d+.*Audio:/.test(stderr));
});
});
}
diff --git a/apps/web/lib/media-client.ts b/apps/web/lib/media-client.ts
index a036c2cc8..d13a9a779 100644
--- a/apps/web/lib/media-client.ts
+++ b/apps/web/lib/media-client.ts
@@ -10,6 +10,26 @@ export function isMediaServerConfigured(): boolean {
return !!serverEnv().MEDIA_SERVER_URL;
}
+export async function checkMediaServerHealth(): Promise<{
+ status: string;
+ ffmpeg: { available: boolean; version: string };
+}> {
+ const mediaServerUrl = serverEnv().MEDIA_SERVER_URL;
+ if (!mediaServerUrl) {
+ throw new Error("MEDIA_SERVER_URL is not configured");
+ }
+
+ const response = await fetch(`${mediaServerUrl}/health`, {
+ method: "GET",
+ });
+
+ if (!response.ok) {
+ throw new Error(`Media server health check failed: ${response.status}`);
+ }
+
+ return response.json();
+}
+
export async function checkHasAudioTrackViaMediaServer(
videoUrl: string,
): Promise<boolean> {
@@ -26,7 +46,6 @@ export async function checkHasAudioTrackViaMediaServer(
if (!response.ok) {
const errorData = (await response.json()) as MediaServerError;
- console.error("[media-client] Audio check failed:", errorData);
throw new Error(errorData.error || "Audio check failed");
}
@@ -49,13 +68,21 @@ export async function extractAudioViaMediaServer(
});
if (!response.ok) {
- const errorData = (await response.json()) as MediaServerError;
- console.error("[media-client] Audio extraction failed:", errorData);
+ let errorData: MediaServerError;
+ try {
+ errorData = (await response.json()) as MediaServerError;
+ } catch {
+ throw new Error(
+ `Audio extraction failed: ${response.status} ${response.statusText}`,
+ );
+ }
if (errorData.code === "NO_AUDIO_TRACK") {
throw new Error("NO_AUDIO_TRACK");
}
- throw new Error(errorData.error || "Audio extraction failed");
+ throw new Error(
+ errorData.details || errorData.error || "Audio extraction failed",
+ );
}
const arrayBuffer = await response.arrayBuffer();
diff --git a/apps/web/package.json b/apps/web/package.json
index c951cb82c..9ec1589bf 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -4,8 +4,8 @@
"private": true,
"scripts": {
"dev": "NODE_OPTIONS='--disable-warning=DEP0169' dotenv -e ../../.env -- next dev",
- "build": "next build --turbopack",
- "build:web": "next build --turbopack",
+ "build": "next build",
+ "build:web": "next build",
"build:docker": "cd ../.. && docker build -t cap-web-docker . --no-cache --progress=plain",
"start": "NODE_OPTIONS='--disable-warning=DEP0169' next start",
"compress-images": "bash tools/compress-images.sh",
diff --git a/apps/web/workflows/transcribe.ts b/apps/web/workflows/transcribe.ts
index c8f781f0a..3c01ba39d 100644
--- a/apps/web/workflows/transcribe.ts
+++ b/apps/web/workflows/transcribe.ts
@@ -37,8 +37,6 @@ export async function transcribeVideoWorkflow(
const { videoId, userId, aiGenerationEnabled } = payload;
- console.log(`[transcribe-workflow] Starting for video ${videoId}`);
-
const videoData = await validateVideo(videoId);
if (videoData.transcriptionDisabled) {
@@ -66,7 +64,6 @@ export async function transcribeVideoWorkflow(
await generateMetadata(videoId, userId);
}
- console.log(`[transcribe-workflow] Completed for video ${videoId}`);
return { success: true, message: "Transcription completed successfully" };
}
@@ -127,7 +124,6 @@ async function markSkipped(videoId: string): Promise<void> {
async function markNoAudio(videoId: string): Promise<void> {
"use step";
- console.log(`[transcribe-workflow] Video ${videoId} has no audio track`);
await db()
.update(videos)
.set({ transcriptionStatus: "NO_AUDIO" })
@@ -157,9 +153,6 @@ async function extractAudio(
}
const useMediaServer = isMediaServerConfigured();
- console.log(
- `[transcribe-workflow] Using ${useMediaServer ? "media server" : "local FFmpeg"} for audio extraction`,
- );
let hasAudio: boolean;
let audioBuffer: Buffer;
@@ -167,20 +160,16 @@ async function extractAudio(
if (useMediaServer) {
hasAudio = await checkHasAudioTrackViaMediaServer(videoUrl);
if (!hasAudio) {
- console.log("[transcribe-workflow] Video has no audio track");
return null;
}
- console.log("[transcribe-workflow] Extracting audio via media server");
audioBuffer = await extractAudioViaMediaServer(videoUrl);
} else {
hasAudio = await checkHasAudioTrack(videoUrl);
if (!hasAudio) {
- console.log("[transcribe-workflow] Video has no audio track");
return null;
}
- console.log("[transcribe-workflow] Extracting audio from video");
const result = await extractAudioFromUrl(videoUrl);
try {
@@ -190,11 +179,11 @@ async function extractAudio(
}
}
- const audioKey = `${userId}/${videoId}/audio-temp.m4a`;
+ const audioKey = `${userId}/${videoId}/audio-temp.mp3`;
await bucket
.putObject(audioKey, audioBuffer, {
- contentType: "audio/mp4",
+ contentType: "audio/mpeg",
})
.pipe(runPromise);
@@ -202,14 +191,21 @@ async function extractAudio(
.getSignedObjectUrl(audioKey)
.pipe(runPromise);
- console.log("[transcribe-workflow] Audio uploaded to S3");
return audioSignedUrl;
}
async function transcribeWithDeepgram(audioUrl: string): Promise<string> {
"use step";
- console.log("[transcribe-workflow] Calling Deepgram API");
+ const audioCheckResponse = await fetch(audioUrl, {
+ method: "HEAD",
+ });
+ if (!audioCheckResponse.ok) {
+ throw new Error(
+ `Audio URL not accessible: ${audioCheckResponse.status} ${audioCheckResponse.statusText}`,
+ );
+ }
+
const deepgram = createClient(serverEnv().DEEPGRAM_API_KEY as string);
const { result, error } = await deepgram.listen.prerecorded.transcribeUrl(
@@ -219,16 +215,14 @@ async function transcribeWithDeepgram(audioUrl: string): Promise<string> {
smart_format: true,
detect_language: true,
utterances: true,
- mime_type: "audio/mp4",
+ mime_type: "audio/mpeg",
},
);
if (error) {
- console.error("[transcribe-workflow] Deepgram error:", error);
throw new Error(`Deepgram transcription failed: ${error.message}`);
}
- console.log("[transcribe-workflow] Transcription received");
return formatToWebVTT(result as unknown as DeepgramResult);
}
@@ -254,8 +248,6 @@ async function saveTranscription(
.update(videos)
.set({ transcriptionStatus: "COMPLETE" })
.where(eq(videos.id, videoId as Video.VideoId));
-
- console.log("[transcribe-workflow] Transcription saved to S3");
}
async function cleanupTempAudio(
@@ -270,9 +262,8 @@ async function cleanupTempAudio(
Option.fromNullable(bucketId),
).pipe(runPromise);
- const audioKey = `${userId}/${videoId}/audio-temp.m4a`;
+ const audioKey = `${userId}/${videoId}/audio-temp.mp3`;
await bucket.deleteObject(audioKey).pipe(runPromise);
- console.log("[transcribe-workflow] Cleaned up temp audio file");
} catch {}
}
@@ -282,6 +273,5 @@ async function generateMetadata(
): Promise<void> {
"use step";
- console.log("[transcribe-workflow] Triggering AI metadata generation");
await generateAiMetadata(videoId as Video.VideoId, userId);
}