DEV Community

Building a No-Upload Video-to-MP4 and MP4-to-MP3 Converter in the Browser with FFmpeg.wasm

Most online video converters still work the same way they did ten years ago: upload a 600 MB .mov or .mkv file to someone else's server, wait in a queue, hope they delete it, and download the result back.

When we added local file conversion to video2mp4.com, we didn't want to pay for transcoding bandwidth - and we didn't want to touch users' private screen recordings or phone clips at all. So we built a browser-based video to MP4 converter and an MP4 to MP3 and WAV audio extractor that run entirely inside the browser via @ffmpeg/ffmpeg (WebAssembly).

Shipping FFmpeg.wasm in production looks simple in a five-line demo, but real user files break almost every happy-path assumption. Here are the five engineering problems we ran into and how we solved them.

1. Scoping COOP/COEP headers so they don't break the rest of the site

Multi-threaded @ffmpeg/core-mt needs SharedArrayBuffer, which requires Cross-Origin Isolation:

/convert-video-to-mp4/
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

/mp4-to-mp3/
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp

/_astro/worker-*
Cross-Origin-Embedder-Policy: require-corp

The catch: turning on COEP: require-corp site-wide breaks any third-party script, analytics pixel, or external image that doesn't send a Cross-Origin-Resource-Policy header.

Two fixes kept everything sane:

  • Route-level isolation only: We apply COOP / COEP only to the converter routes and Astro's worker bundles (/_astro/worker-*), leaving the rest of the static Astro site untouched.
  • Graceful fallback for cross-origin assets on isolated pages: Any lightweight telemetry stays inline, and external badge images on isolated routes fall back to plain text instead of failing with ERR_BLOCKED_BY_RESPONSE.NotSameOriginAfterDefaultedToSameOriginByCoep.

2. Don't blindly -c copy - and don't blindly re-encode either

A naive browser converter either always runs libx264 (painfully slow in WASM) or always runs -c copy (which fails on .wmv / .flv or produces .mp4 files with VP9/Opus streams that QuickTime, iMovie, and TVs refuse to play).

Instead, we probe the input first by parsing FFmpeg's -i stream banner and build an ordered conversion plan:

  • Stream copy (mode: "copy") when the video codec is already h264 or hevc and audio is aac or mp3. Keeping hevc untouched is huge in practice: iPhones record HEVC .mov files by default, and re-encoding HEVC in WebAssembly is brutally slow - whereas remuxing it into .mp4 takes seconds with zero quality loss.
  • Audio-only re-encode when the video track is already MP4-safe (h264 / hevc) but the audio isn't (e.g., PCM or Opus), copying video and encoding audio to 160 kbps AAC.
  • Full re-encode fallback (mode: "encode") using libx264 -preset ultrafast -crf 23 -pix_fmt yuv420p when the input is VP8/VP9 WebM, older AVI, WMV, or legacy FLV.

Two subtle bugs we caught with real user files:

  • Cover art masquerading as video: Some .mkv and .mp4 files include an embedded thumbnail tagged (attached pic). Ignoring lines containing (attached pic) when picking the first video stream prevented FFmpeg from trying to transcode a JPEG cover instead of the actual movie.
  • Odd-sized screen recordings: Browser and OS screen recorders frequently produce odd pixel widths or heights (like 1919x1080), which causes libx264 with -pix_fmt yuv420p to abort. Adding -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 fixes it cleanly.

3. Separate file-size caps for stream copy vs. WASM re-encoding

Browser tabs have real RAM ceilings, especially on mobile. One hard cap doesn't fit both workloads:

  • Up to 2 GB (MAX_FILE_BYTES) for remuxing (-c copy) and audio extraction (MP4 to MP3 / WAV), because extracting audio skips decoding video frames completely and runs surprisingly fast even on long recordings.
  • 500 MB cap (ENCODE_MAX_BYTES) when full WASM video re-encoding is required. Single-threaded or limited-thread WASM x264 runs at only a few frames per second on HD footage; stopping early with an honest message beats freezing a visitor's browser tab for 45 minutes until it OOMs.

4. Audio extraction: one sane MP3 bitrate + uncompressed WAV

For our no-upload MP4 to MP3 converter, we deliberately skipped the fake "320 kbps HD MP3" dropdown that many web tools show. Most video soundtracks from phones and web platforms are ~128 kbps AAC. Re-encoding to a constant 192 kbps LAME MP3 preserves what's in the source (~1.4 MB/min) without bloating the file with imaginary detail.

And if a user is taking the audio into Audacity, GarageBand, or a DAW, we offer 16-bit PCM WAV directly so there's no second generation of lossy compression.

(Bonus: because FFmpeg handles audio containers too, the same local box converts M4A voice memos, FLAC, and OGG into MP3 or WAV with zero server round-trips.)

5. Pairing local converters with public-link tools

Client-side WebAssembly is ideal when the user already has a local file. When they only have a public social video URL instead of a file, the browser can't bypass CORS to fetch raw streams directly - that part still belongs on a lightweight server, which is what powers our link to MP3 converter and main video link to MP4 downloader.

Keeping those two architectures cleanly separated - server relay for public links, zero-upload WebAssembly for local files - let us cut infrastructure costs while giving privacy-conscious users a converter that literally works offline once the WASM binary is cached.

If you're building with ffmpeg.wasm on Astro or Cloudflare Workers and hit COEP/worker bundling quirks, happy to share more details in the comments.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.