Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 4 min read

A Video Background Remover that runs entirely in the browser (WebGPU + WebCodecs)

Most video background removers work the same way: you upload your clip, wait in a queue while a server runs the model, then download the result. Often the export you actually want (transparent video, no watermark) needs

Most video background removers work the same way: you upload your clip, wait in a queue while a server runs the model, then download the result. Often the export you actually want (transparent video, no watermark) needs a subscription.

I wanted to see how far I could get doing all of it in the browser: no upload, no server GPU, no queue. The result is CutVideoBG, a background remover for videos, GIFs and images. The file is decoded, matted, composited and encoded on your own device.

Here's how it works, what went wrong, and where it still falls short.

What it does

  • Remove the background from a video of people. No green screen needed.
  • Remove a real green screen with a chroma key.
  • Remove the background from GIFs and images (any subject, not only people).
  • Keep it transparent, or replace it with a color, an image or a blur, or put text behind the person.
  • Export a transparent WebM (VP9 with alpha), an MP4 (H.264, original audio kept), a transparent GIF or a PNG.

No account is needed to try it, and previews are unlimited.

The pipeline

Everything runs in a Web Worker so the UI stays responsive:

  1. Decode with WebCodecs (via Mediabunny). MP4, MOV, WebM, GIF, PNG, JPG and WebP are supported.
  2. Matting with ONNX Runtime Web on WebGPU, falling back to WASM when WebGPU isn't available.
    • Videos use MODNet, a small, fast portrait-matting model.
    • GIFs and images use BiRefNet, which is much slower but handles any subject.
  3. Composite in WebGL2: alpha, background replacement, blur, edge softness, text layers.
  4. Encode back to WebM with alpha, MP4, GIF or PNG.

The server side is a single Cloudflare Worker that only handles accounts and payments. It never sees a frame of video.

Decision 1: process first, play later

My first version ran the model while the video played. On a fast GPU that was fine. On a slower laptop it dropped frames and stuttered, which made the cutout look worse than it was.

So now the whole clip (up to 2 minutes) is processed once with a progress bar and an estimate of the time left. The mattes are compressed and cached in memory. After that:

  • playback and seeking are instant,
  • changing the background, blur or text doesn't re-run the model,
  • export is just compositing and encoding, which is much faster than inference.

Videos above 30 fps are matted at 30 fps, and in-between frames reuse the nearest matte.

Decision 2: fp16 where possible

MODNet runs in fp16 when the device supports WebGPU's shader-f16 and falls back to fp32 when it doesn't. In my tests the quality difference was hard to see, while fp16 is half the download size and roughly 25% faster.

For reference, on my own machine (an RTX 4060 laptop, so a best case, not a typical one):

Model Time per frame Throughput
MODNet fp16 (short side 512) ~33 ms inference ~21 fps
MODNet fp32 ~38 ms inference ~19.5 fps
BiRefNet lite fp16 (512) ~910 ms ~1.1 fps

That gap is why the user never picks a model: videos get the fast one, and GIFs and images (with far fewer frames) get the accurate one. The page and the file type decide.

Gotcha: cross-origin isolation reaches into workers

Multi-threaded WASM needs SharedArrayBuffer, which needs cross-origin isolation (Cross-Origin-Opener-Policy: same-origin + Cross-Origin-Embedder-Policy: require-corp).

I had the headers on the HTML document and everything worked locally. In production the worker was silently blocked: the worker scripts and the model files served from a CDN need the right headers too (COEP on the worker script, CORP/CORS on everything it loads). If your WASM backend mysteriously falls back to a single thread, check the response headers on every script the worker loads, not only the page.

Why browser-side makes the business work

Because inference runs on the visitor's device, a free preview costs me almost nothing to serve. That's what lets the free tier be generous: unlimited previews, and the first 5 seconds export free (up to 720p, with a watermark). Full exports (up to 2 minutes, 1080p, no watermark) use a one-time credit pack or a Pro plan.

The whole stack is one Cloudflare Worker with D1, R2 and KV. There's no GPU bill.

Honest limits

  • Speed depends on your device. Desktop Chrome or Edge with WebGPU is the fastest. CPU (WASM) works, but it's much slower.
  • The video model is tuned for people. For a pet or a product in a video, I don't promise a clean result.
  • GIF and image cutouts are slow, about 1–2 seconds per frame, because BiRefNet is a much bigger model.
  • Fine edges are still hard. Hair against a busy background and low-resolution GIFs are where you'll see artifacts.

Try it

πŸ‘‰ cutvideobg.com: drop in a clip, no account needed.

I'd love feedback, especially:

  • How fast was processing on your hardware (GPU or browser)?
  • Where did the cutout fail on your footage?
  • Anyone else shipping ONNX Runtime Web + WebGPU in production? I'd like to hear how you handle model loading and fallbacks.

Happy to answer questions about any part of the pipeline in the comments.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.