(videoUrl: string)
| 109 | } |
| 110 | |
| 111 | export async function extractAudioToBuffer(videoUrl: string): Promise<Buffer> { |
| 112 | const ffmpeg = getFfmpegPath(); |
| 113 | const ffmpegArgs = [ |
| 114 | "-i", |
| 115 | videoUrl, |
| 116 | "-vn", |
| 117 | "-acodec", |
| 118 | "libmp3lame", |
| 119 | "-b:a", |
| 120 | "128k", |
| 121 | "-f", |
| 122 | "mp3", |
| 123 | "-pipe:1", |
| 124 | ]; |
| 125 | |
| 126 | return new Promise((resolve, reject) => { |
| 127 | const proc = spawn(ffmpeg, ffmpegArgs, { stdio: ["pipe", "pipe", "pipe"] }); |
| 128 | |
| 129 | const chunks: Buffer[] = []; |
| 130 | let stderr = ""; |
| 131 | |
| 132 | proc.stdout?.on("data", (chunk: Buffer) => { |
| 133 | chunks.push(chunk); |
| 134 | }); |
| 135 | |
| 136 | proc.stderr?.on("data", (data: Buffer) => { |
| 137 | stderr += data.toString(); |
| 138 | }); |
| 139 | |
| 140 | proc.on("error", (err: Error) => { |
| 141 | reject(new Error(`Audio extraction failed: ${err.message}`)); |
| 142 | }); |
| 143 | |
| 144 | proc.on("close", (code: number | null) => { |
| 145 | if (code === 0) { |
| 146 | resolve(Buffer.concat(chunks)); |
| 147 | } else { |
| 148 | reject( |
| 149 | new Error(`Audio extraction failed with code ${code}: ${stderr}`), |
| 150 | ); |
| 151 | } |
| 152 | }); |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | export async function convertWavToMp3(wavBuffer: Buffer): Promise<Buffer> { |
| 157 | const ffmpeg = getFfmpegPath(); |
no test coverage detected