Converting video in the browser without uploading it: ffmpeg.wasm in a Web Worker
DEV Community

Converting video in the browser without uploading it: ffmpeg.wasm in a Web Worker

Most "free online converters" work the same way: you upload your file, their server runs ffmpeg, and you download the result. That's fine for a meme, less fine for a screen recording of your bank account or a voice note from your doctor.

For nocaptools.link I wanted MP4 to MP3, MOV to MP4, video to GIF and "compress this video to under 10 MB" to work without the file ever leaving the device. ffmpeg.wasm makes that possible, but there are a few traps between the README and something that works on a normal laptop. Here's what I ended up with.

1. Use the single-threaded core (and skip the header headache)

ffmpeg.wasm ships a multi-threaded core that needs SharedArrayBuffer, which in turn needs your page to be cross-origin isolated (Cross-Origin-Opener-Policy: same-origin + Cross-Origin-Embedder-Policy: require-corp). Those headers break a lot of third-party embeds (ads, analytics, some fonts), and on a static host you have to be careful to set them on every response.

The single-threaded core needs none of that. It's slower on big videos, but for the jobs people actually do in a browser (a few minutes of phone video, an audio track) it's fine, and it works everywhere.

2. Load the core from a CDN, pinned

ffmpeg-core.wasm is about 32 MB. My site is a static Next.js export on Cloudflare Pages, which has a 25 MiB per-file limit, so bundling it wasn't an option anyway. Loading it from jsDelivr at a pinned version costs nothing and the browser caches it after the first use:

const FFMPEG_CORE_BASE = "https://cdn.jsdelivr.net/npm/@ffmpeg/core@0.12.10/dist/esm";
const coreURL = `${FFMPEG_CORE_BASE}/ffmpeg-core.js`;
const wasmURL = `${FFMPEG_CORE_BASE}/ffmpeg-core.wasm`;
const mod = await import(/* webpackIgnore: true */ coreURL);
// The core reads the wasm location from the URL hash (the same trick @ffmpeg/ffmpeg uses)
const core = await mod.default({
  mainScriptUrlOrBlob: `${coreURL}#${btoa(JSON.stringify({ wasmURL, workerURL: "" }))}`,
});

Pin the version. A floating @latest is a great way to wake up to a broken converter.

Tell users about the one-time download. "Loading the converter engine (about 32 MB, only the first time)" turns a confusing pause into an expected one.

3. Run it in a Web Worker

ffmpeg will happily block the main thread for as long as a conversion takes. Put the whole thing in a worker and talk to it with messages (load, probe, exec).

A side benefit: cancel is just worker.terminate(), which is far more reliable than trying to interrupt ffmpeg mid-run.

4. Don't copy the file into WebAssembly memory

The examples usually do ffmpeg.writeFile("input.mp4", await fetchFile(file)). That copies the entire video into the wasm heap, so a 1 GB video needs over 1 GB of extra memory before ffmpeg even starts. On an 8 GB laptop with a few tabs open, that's where things die.

Emscripten's WORKERFS lets you mount the File object read-only instead, so ffmpeg reads it in chunks straight from disk:

core.FS.mkdir("/in");
core.FS.mount(core.FS.filesystems.WORKERFS, { files: [file] }, "/in");
const inputPath = `/in/${file.name}`;
// ... exec ffmpeg with -i inputPath ...
core.FS.unmount("/in");

Keep a fallback that copies the file for the rare browser where the mount fails, but in practice WORKERFS is what makes multi-hundred-MB files workable.

5. Don't re-encode when you don't have to

A lot of "convert MOV to MP4" jobs are iPhone videos that are already H.264 + AAC, just in a different container. Re-encoding them in single-threaded wasm is slow and loses quality. Probe first (-i and read the stream info from the log), and if the codecs already fit the target, remux:

-i input.mov -c copy -movflags +faststart output.mp4

That takes milliseconds instead of minutes, with zero quality loss. Only fall back to libx264 when the source is something like HEVC, VP9 or an old codec.

6. "Compress to under 25 MB" is just arithmetic (plus one retry)

People don't want to pick a bitrate; they want "small enough for Discord / email". If you know the duration, you can work out the total bitrate budget:

const targetBits = targetMB * 8 * 1_000_000 * 0.95; // leave ~5% for container overhead
const audioKbps = 96;
const videoKbps = Math.max(150, targetBits / durationSec / 1000 - audioKbps);

Then pick a resolution that makes sense for that bitrate (no point keeping 1080p at 300 kbps). One-pass bitrate control can overshoot slightly, so if the output is still over the limit, re-encode once with the budget scaled down by how much you missed. One retry gets almost every file under the line.

7. Be honest about limits

Things I put directly on the page:

  • The first conversion downloads the engine once.
  • Long videos are slow on phones, which have much less memory available to a web page. Trim or compress first.
  • Nothing is uploaded, which you can check in your browser's network tab: there are no requests carrying your file.

Was it worth it? For the common jobs (MP4 to MP3, iPhone MOV to MP4, a GIF from a short clip, a video small enough for Discord) the browser version is good enough that I don't miss a server at all, and it costs nothing to run because the visitor's device does the work.

If you want to poke at it, the converter is at nocaptools.link/audio-converter/mp4-to-mp3. I'd love to hear where it breaks on your machine.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.