Exporting video in the browser with no server
The editor exports an MP4 without sending anything anywhere. The frames are composited in workers, encoded with WebCodecs, muxed by Mediabunny and written to the browser’s private storage (OPFS), and the download comes from there. The goal in the spec was an export faster than real time, with a progress bar, a cancel button, and the editor still usable while it runs. This post is about how it got there, including the audio. It’s part of a series that starts here.
A frozen snapshot, stepped by frame
The document is immutable (each edit produces a new one through Immer), so export takes the current document when it starts and works on that. You can keep editing, and your edits don’t reach the file.
There’s no clock in an export. For each frame index it asks the same resolver
the preview uses which clips are visible, draws them with the same compositor,
wraps the canvas in a VideoFrame and gives it to the encoder at the frame’s
exact timestamp. The preview and the export share one function for “draw
timeline frame n”, so the tests can compare an exported frame with the
preview’s. The tolerance is a mean difference of 2 on a 0–255 scale. The same
frame measures at most 1.0, and the next frame 5.77, so an off-by-one fails
the pixel check by itself.
Lanes, because there’s no GPU
The tests run in headless Chrome with no GPU, where compositing one 1080p frame costs about 25 ms on one thread. That alone is most of a frame’s 33 ms at 30 fps, and a single pipeline took between 0.86 and 1.18 times the project’s duration. So export runs a coordinator worker plus up to three render lanes, each one its own worker. The coordinator asks the lanes for frames in turn, at most two ahead per lane, and feeds the encoder in order.
On a 12-second 1080p30 project, the export took 27–37% of the project’s duration in the PR’s measurements. That number flatters it: the source is a 320×180 test clip scaled up, so it measures compositing, not decoding. The reviewer exported the 10-second 4K clip to 1080p and it took 1.80× its duration with three lanes. With 4K sources and no GPU, it’s slower than real time.
The lanes also brought a problem. Each lane worker had its own decoder pool with a limit of 4, so with three lanes and a few clips the export alone could hold 12 decoders, plus 4 for the preview. The spec’s limit is 4 in total, because hardware decoders are scarce. Now the preview drops to one decoder while an export runs and shows only keyframes, and the lanes share the other three. The test for it puts six clips on the timeline and checks that preview plus lanes never go over 4.
The encoder
The encoder is a small wrapper around VideoEncoder. When encodeQueueSize
reaches 8 it waits for the dequeue event before feeding more, so a fast
compositor can’t pile up frames in memory. A keyframe goes out every 2
seconds. The default quality is 0.15 bits per pixel per frame, about
9.3 Mbps at 1080p30.
H.264 is the default. VP9 and AV1 only appear in the menu when
VideoEncoder.isConfigSupported accepts them, and the first version asked
the wrong question. It checked each codec once, at the project’s full size
and the last quality used, and never again. Pick 480p and a codec could be
hidden although it worked at 480p. Switch to High at full size and a codec
could stay in the menu although the encoder would refuse the higher bitrate.
In that case the export failed and the panel went blank, with only a
console.error to show for it. Now the check runs again whenever resolution
or quality changes, against the exact config that will be encoded: codec
string, width, height, bitrate and frame rate. A refused export shows up as a
failure with the reason.
All three codecs go into MP4, which Mediabunny’s MP4 muxer accepts.
Audio, and the samples you don’t hear
For audio, the main thread rebuilds the playback audio graph from the
snapshot inside an OfflineAudioContext, renders the mix in 10-second
chunks, about 15 ms each, and sends the samples to the export worker. The
graph code is the one playback uses, so what you hear in the preview is
what goes into the file.
The first version encoded those samples with AudioEncoder directly and
muxed the packets itself. Opus worked, and Opus is what CI uses, because
Chrome on Linux has no AAC encoder. But the code tried AAC first, so AAC is
what most people would get: Chrome on macOS and Windows, and Safari. The
review found three problems there, none of which CI could see:
- AAC encoders emit priming samples at the start. Mediabunny measures that delay with an impulse probe and subtracts it. The editor didn’t. At 1024 to 2112 samples, the audio would play 21 to 44 ms late against the video.
- Safari returns an AAC decoder description that Mediabunny has to repair before it goes into the file. Without that, the MP4 could end up with no sound.
- Firefox needs Opus pre-skip subtracted from the first packet only. The editor subtracted it from every packet.
Mediabunny’s own AudioSampleSource already handles all three, but it wants
to own the output, and the editor needs to trim the last packet so the audio
ends exactly where the video does. The fix: encode through
AudioSampleSource on a throwaway output with a NullTarget, which writes
nothing, and relay its packets, already corrected, to the real file. Only the
last packet’s duration changes. Audio and video durations now match within
1 ms, at 30 and 29.97 fps.
The Opus path checks the relay with a non-zero offset: Opus’s first packet has a negative timestamp because of pre-skip. In the reviewer’s run, the export’s first 20 ms matched the preview’s render with a relative error of 0.009, and the last audible sample was at the same index in both, 575014. The AAC path still can’t run on CI, and testing it on macOS and Safari is an open follow-up.
The sample and the smoke test
To let someone try the editor without their own footage, the editor has a sample clip: 9 seconds, 1280×720, 30 fps, H.264 and AAC, 1.68 MB. It’s generated by a script from ffmpeg’s built-in sources (a gradient, a Mandelbrot zoom, a spiral and a few synthesized chords), so it carries no licence at all, and its hard cuts give an obvious place to split.
It also drives a smoke test. A script opens the built app in Chrome, clicks “Try the sample”, splits the clip, ripple-deletes the first piece, exports, downloads the file and checks it: 1280×720, the expected frame count and duration, an audio track, and no page errors. It runs on every PR, and the deploy workflow runs it on the staged build before publishing. It refuses to run on Playwright’s bundled Chromium, which has no H.264 or AAC, and says so instead of failing with a confusing decode error.
The codec most people get was the one CI couldn’t run. The tests were green because they only ran on the one platform where the problem didn’t exist.