The problem

Lightframe is a video compressor/converter that runs 100% in the browser — no uploads, no server. For most formats (MP4, MOV, MKV, WebM) that's easy: the browser's own WebCodecs API decodes and encodes natively, using the same codecs a desktop app would.

But some formats — AVI, WMV, FLV, old MPEG — can't be read by WebCodecs at all. DivX, Xvid, WMV, MJPEG simply aren't codecs the browser knows how to decode. For those, the only option is FFmpeg compiled to WebAssembly, running entirely client-side.

The catch: FFmpeg.wasm (@ffmpeg/core 0.12, the single-thread build — the multi-thread build needs cross-origin-isolation headers a static host can't always guarantee) runs on exactly one CPU thread, in software. On a real 1080p, 83-second WMV screen recording, converting it to MP4 took 561 seconds — over 9 minutes, on a 4-core machine with 3 idle cores doing nothing.

Why not just use more threads naively

The obvious fix — spin up a few FFmpeg.wasm workers and throw more cores at it — doesn't work if you parallelize the wrong axis. A single video file is one continuous bitstream; you can't just "encode it faster in parallel" by splitting arbitrary chunks of bytes.

What can split cleanly is time. A video is a sequence of frames; frames 0-10s don't depend on frames 50-60s (as long as you cut on sensible boundaries). So instead of parallelizing the encode of one stream, we parallelize the video itself — into independent time segments, each encoded by its own single-thread FFmpeg.wasm instance, then stitched back together.

The implementation

  • planSegments: splits the video into time ranges (2 per worker, each ≥8 seconds — too many tiny segments adds overhead without adding speed).
  • One Web Worker per CPU core minus one (workerCount) — we tried using every core once and it failed, likely a memory pressure issue, so one core is deliberately left free.
  • Each worker runs ffmpeg -ss <start> -i <in> -t <len> on just its slice, video only.
  • Audio is encoded once, separately, as its own job (it's cheap compared to video).
  • A final pass joins everything with a stream copy (-c copy, no re-encoding) using FFmpeg's concat demuxer — this join step is fast because no frames are touched, only containers are repackaged.
  • Safety net: segments above a size ceiling, VP9 targets, exact copies, audio-only files, and short clips (<16s) skip parallelization entirely and run as one piece. Any parallel failure also falls back to a single-piece encode automatically.

The result

Same file, same hardware (4 cores), same output settings (MP4, original quality):

Approach Time
Single-threaded FFmpeg.wasm (before) 561s
Parallel, 3 workers (after) 84s
For reference: a well-known competitor's tool, same file 168.5s

A 6.7x speedup from the parallelization alone, and roughly 2x faster than the fastest comparable tool we tested against on the same file — while still never sending a single byte of the video off the user's device.

What didn't help (tried and rejected)

Worth sharing the negative results too, since they're less commonly written up:

  • Using every CPU core (not leaving one free): slower, likely memory contention.
  • ultrafast vs veryfast x264 preset: ultrafast won outright (3x faster, same target bitrate approach).
  • Smaller segments (<8s each): more overhead from spinning up/tearing down FFmpeg instances, no net win.

Try it

The compressor and converter are live at lightframe-rouge.vercel.app — drop in an AVI or WMV file and watch your browser's dev tools Network tab; nothing leaves the page.