When we set out to build a browser-based audio compressor, the obvious path was to write a simple JavaScript encoder. But JS audio encoding is slow, limited in format support, and produces mediocre results compared to native tools.
We wanted FFmpeg quality. We wanted it to run in the browser. So we used WebAssembly.
What is WebAssembly?
WebAssembly (WASM) is a binary instruction format that runs in all modern browsers at near-native speed. Critically, it lets you compile code written in C, C++, or Rust to run inside the browser sandbox — with no plugins and no security exceptions.
FFmpeg is the gold standard of audio/video processing. It's written in C. Compiling it to WASM means we get FFmpeg's full codec library running locally, inside the user's browser tab.
The architecture
Here's how the audio compressor pipeline works under the hood:
ffmpeg.writeFile().The core code
import { createFFmpeg, fetchFile } from '@ffmpeg/ffmpeg';
const ffmpeg = createFFmpeg({ log: false });
await ffmpeg.load(); // loads ~30MB WASM binary once
async function compressAudio(file, bitrateKbps) {
// Write input to virtual FS
ffmpeg.writeFile('input', await fetchFile(file));
// Run FFmpeg: re-encode at target bitrate
await ffmpeg.run(
'-i', 'input',
'-b:a', `${bitrateKbps}k`,
'-acodec', 'libmp3lame',
'output.mp3'
);
// Read result back from virtual FS
const data = ffmpeg.readFile('output.mp3');
return new Blob([data.buffer], { type: 'audio/mp3' });
}
Performance considerations
WASM performance is close to native but not identical. A few things we optimised:
- SharedArrayBuffer — enables multi-threaded WASM (FFmpeg's internal threading). Requires the page to be served with specific COOP/COEP headers.
- WASM binary caching — the 30MB FFmpeg WASM binary is cached by the browser after the first load. Subsequent visits load instantly from cache.
- Chunked progress — FFmpeg emits progress events that we pipe to the UI so users see real-time progress rather than a frozen tab.
What this means for you
The upshot is that you get genuine FFmpeg-quality audio compression — the same engine used by professional media pipelines — running entirely in your browser tab, with no server, no upload, and no waiting.