MCPcopy
hub / github.com/CapSoftware/Cap / extractAudioFromUrl

Function extractAudioFromUrl

apps/web/lib/audio-extract.ts:56–109  ·  view source on GitHub ↗
(
	videoUrl: string,
)

Source from the content-addressed store, hash-verified

54}
55
56export async function extractAudioFromUrl(
57 videoUrl: string,
58): Promise<AudioExtractionResult> {
59 const ffmpeg = getFfmpegPath();
60 const outputPath = join(tmpdir(), `audio-${randomUUID()}.mp3`);
61
62 const ffmpegArgs = [
63 "-i",
64 videoUrl,
65 "-vn",
66 "-acodec",
67 "libmp3lame",
68 "-b:a",
69 "128k",
70 "-f",
71 "mp3",
72 "-y",
73 outputPath,
74 ];
75
76 return new Promise((resolve, reject) => {
77 const proc = spawn(ffmpeg, ffmpegArgs, { stdio: ["pipe", "pipe", "pipe"] });
78
79 let stderr = "";
80
81 proc.stderr?.on("data", (data: Buffer) => {
82 stderr += data.toString();
83 });
84
85 proc.on("error", (err: Error) => {
86 fs.unlink(outputPath).catch(() => {});
87 reject(new Error(`Audio extraction failed: ${err.message}`));
88 });
89
90 proc.on("close", (code: number | null) => {
91 if (code === 0) {
92 resolve({
93 filePath: outputPath,
94 mimeType: "audio/mpeg",
95 cleanup: async () => {
96 try {
97 await fs.unlink(outputPath);
98 } catch {}
99 },
100 });
101 } else {
102 fs.unlink(outputPath).catch(() => {});
103 reject(
104 new Error(`Audio extraction failed with code ${code}: ${stderr}`),
105 );
106 }
107 });
108 });
109}
110
111export async function extractAudioToBuffer(videoUrl: string): Promise<Buffer> {
112 const ffmpeg = getFfmpegPath();

Callers 2

extractAudioFunction · 0.90

Calls 4

getFfmpegPathFunction · 0.85
rejectFunction · 0.85
onMethod · 0.80
resolveFunction · 0.70

Tested by

no test coverage detected