Extracting MP3 audio from a video entirely in the browser with ffmpeg.wasm
Disclosure: this article was written with AI assistance and reviewed by me. I built the tool used as the example below. Most "video to MP3" websites upload your file to a server, convert it there and send the result bac
Disclosure: this article was written with AI assistance and reviewed by me. I built the tool used as the example below.
Most "video to MP3" websites upload your file to a server, convert it there and send the result back. For a 500 MB phone video that means a long upload, a queue and a copy of your private video on someone else's disk. Since WebAssembly became fast enough, you can do the whole thing locally in the tab. Here is how it works and the pitfalls I hit.
The building block: ffmpeg.wasm
ffmpeg.wasm is FFmpeg compiled to WebAssembly. It exposes a small virtual file system (MEMFS) and an exec function that takes the same arguments as the command-line tool.
import { FFmpeg } from "@ffmpeg/ffmpeg";
import { fetchFile } from "@ffmpeg/util";
const ffmpeg = new FFmpeg();
await ffmpeg.load(); // downloads the ~30 MB core once, then it is cached
async function videoToMp3(file, bitrate = "192k") {
await ffmpeg.writeFile("input", await fetchFile(file));
await ffmpeg.exec([
"-i", "input",
"-vn", // drop the video stream
"-acodec", "libmp3lame",
"-b:a", bitrate,
"output.mp3",
]);
const data = await ffmpeg.readFile("output.mp3");
await ffmpeg.deleteFile("input");
await ffmpeg.deleteFile("output.mp3");
return new Blob([data.buffer], { type: "audio/mpeg" });
}
-vn is the important flag: FFmpeg never decodes the video frames, so the job is mostly audio decoding plus MP3 encoding and finishes in seconds even on a laptop.
Pitfall 1: memory
MEMFS keeps the input and the output in the WebAssembly heap. The single-threaded core is limited to about 2 GB, and mobile browsers kill the tab much earlier. What helped:
- Delete files from MEMFS right after reading the result (see above).
- Warn the user above ~1 GB instead of failing silently.
- Reuse one
FFmpeginstance instead of creating a new one per file.
Pitfall 2: multithreading needs cross-origin isolation
The multi-threaded core (@ffmpeg/core-mt) uses SharedArrayBuffer, which browsers only expose when the page is cross-origin isolated:
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
These headers also block third-party scripts and images that don't send CORP/CORS headers, so check everything you embed before turning them on. For audio extraction the single-threaded core is fast enough, so I kept it simple.
Pitfall 3: choosing the bitrate
Users rarely know what bitrate to pick. A sensible default is 192 kbps: transparent for most speech and music from phone videos. 128 kbps is fine for voice-only recordings (lectures, meetings) and halves the file size; 320 kbps only makes sense when the source audio is already high quality. Re-encoding cannot add quality that isn't in the source track.
Pitfall 4: progress feedback
ffmpeg.on("progress", ({ progress }) => ...) gives a 0-1 ratio. For audio-only jobs it can jump, so I also listen to the log event and parse time= to show elapsed media time.
Result
The page works offline after the first load, nothing leaves the device, and there is no server cost per conversion, which is what lets the tool stay free. You can try the version I built here: Free Video to MP3 Converter.
If you have tricks for keeping ffmpeg.wasm memory under control on mobile, I'd love to hear them in the comments.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.