A user uploads a video that’s too large, and you want to compress it in the browser before sending it. The first answer you’ll probably find is ffmpeg.wasm, which compiles FFmpeg to WebAssembly so it can run in the browser. It works, but the download is usually over 20 MB, takes time to load, uses a lot of memory, and performs poorly on mobile.
So is there a lighter option?
Yes, though each comes with trade-offs. Here are the options from lightest to heaviest, along with what each can and can’t do.
Lightest: use MediaRecorder directly
MediaRecorder is the browser’s built-in recording API. It needs no third-party library. You can give it a MIME type and bitrate and record a MediaStream to a file.
const stream = video.captureStream();
const recorder = new MediaRecorder(stream, {
mimeType: 'video/webm; codecs=vp9',
videoBitsPerSecond: 1_000_000, // 1 Mbps
});
const chunks = [];
recorder.ondataavailable = e => chunks.push(e.data);
recorder.onstop = () => {
const blob = new Blob(chunks, { type: 'video/webm' });
// 上传 blob
};
recorder.start();
video.play();
video.onended = () => recorder.stop();
What it can do: Re-record at a target bitrate to produce a smaller file. What it can’t do: Give precise control over encoding parameters, change resolution or frame rate on its own, or guarantee MP4 output.
Available container and codec combinations vary by browser. Probe for support first:
MediaRecorder.isTypeSupported('video/webm; codecs=vp9') // Chrome: true, Safari: false
MediaRecorder.isTypeSupported('video/mp4; codecs=avc1') // Safari: true, Chrome: 不确定
Good uses include webcam and screen recording, or a local video file where “a little smaller” is all you need.
Lightweight and capable: Canvas + MediaRecorder
Putting a canvas in front of MediaRecorder lets you downscale, reduce the frame rate, crop, or add a watermark. Draw the video onto the canvas frame by frame, then use captureStream() to pass the canvas output stream to MediaRecorder for re-recording.
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth / 2; // 降分辨率
canvas.height = video.videoHeight / 2;
const ctx = canvas.getContext('2d');
// 指定帧率(比如从 30fps 降到 15fps)
const stream = canvas.captureStream(15);
const recorder = new MediaRecorder(stream, {
mimeType: 'video/webm; codecs=vp9',
videoBitsPerSecond: 800_000,
});
const chunks = [];
recorder.ondataavailable = e => chunks.push(e.data);
recorder.onstop = () => {
const blob = new Blob(chunks, { type: 'video/webm' });
};
recorder.start();
function drawFrame() {
if (video.paused || video.ended) {
recorder.stop();
return;
}
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
requestAnimationFrame(drawFrame);
}
video.play();
video.onplay = drawFrame;
Canvas work can move to an OffscreenCanvas in a worker to avoid blocking the main thread:
// main.js
const worker = new Worker('compress-worker.js');
const offscreen = canvas.transferControlToOffscreen();
worker.postMessage({ canvas: offscreen }, [offscreen]);
What it can do: Reduce resolution and frame rate, apply basic visual processing, and compress before upload.
What it can’t do: Offer precise encoder controls. The output formats are still limited by MediaRecorder (usually WebM), and audio-track handling is limited.
This covers most cases where the video is too large to upload. It takes little code and is straightforward to integrate, making it my first choice when I want to avoid WebAssembly.
Most capable while still lighter than WebAssembly: WebCodecs + a muxer
WebCodecs is an API that the W3C began standardizing in 2021. It calls the browser’s built-in, hardware-accelerated codecs directly, so you don’t have to ship codec implementations with the frontend. In 2026 global support was around 94%, according to Can I Use.
WebCodecs handles encoding and decoding. Packaging the streams into an MP4 or WebM container requires a muxer. The formerly popular mp4-muxer and webm-muxer are deprecated. Their official migration target is Mediabunny, designed for reading, writing, and converting media files in browsers.
A simplified compression pipeline:
import { Muxer, ArrayBufferTarget } from 'mediabunny';
// 1. 解码原始视频
const decoder = new VideoDecoder({
output: async (frame) => {
// 2. 编码为目标格式
encoder.encode(frame);
frame.close();
},
error: (e) => console.error(e),
});
// 3. 设置编码器参数(可精确控制)
const encoder = new VideoEncoder({
output: (chunk, meta) => muxer.addVideoChunk(chunk, meta),
error: (e) => console.error(e),
});
encoder.configure({
codec: 'avc1.42001e', // H.264
width: 1280,
height: 720,
bitrate: 1_000_000,
framerate: 30,
hardwareAcceleration: 'prefer-hardware',
});
// 4. 封装成 MP4
const muxer = new Muxer({
target: new ArrayBufferTarget(),
video: { codec: 'avc', width: 1280, height: 720 },
});
What it can do: Give frame-level control, select codecs, use hardware acceleration, and output MP4 or WebM. It suits video editors, local transcoders, and production media pipelines. Cost: Integration is more complicated than the canvas approach. You have to handle demuxing and synchronize the audio and video tracks yourself. Poor fit: Supporting old browsers. IE has no support, and neither do Safari versions before 15.4.
Check capabilities before using the API:
const config = { codec: 'avc1.42001e', width: 1280, height: 720, bitrate: 1_000_000 };
const { supported } = await VideoEncoder.isConfigSupported(config);
if (!supported) {
// fallback 到 Canvas 方案或提示用户
}
A separate case: changing WebRTC parameters
For real-time calls, video conferences, or live streaming, use RTCRtpSender.setParameters() to dynamically reduce the bitrate or resolution during transmission:
const sender = peerConnection.getSenders().find(s => s.track.kind === 'video');
const params = sender.getParameters();
params.encodings[0].maxBitrate = 500_000;
await sender.setParameters(params);
This adjusts live transmission parameters. Exporting a compressed file is a separate operation, so keep the two use cases distinct.
Comparison
| Approach | Download size | Integration difficulty | Degree of control | Best use |
|---|---|---|---|---|
MediaRecorder | No dependencies | Very low | Low | Recording or a simple re-encode |
Canvas + MediaRecorder | No dependencies | Low | Medium | Compressing before upload, reducing resolution or frame rate |
WebCodecs + Mediabunny | Tens of KB | Medium to high | High | Local transcoding, video editors |
wasm + ffmpeg | 20 MB+ | Low with existing wrappers | Very high | Access to the full FFmpeg feature set |
WebRTC setParameters | No dependencies | Low | Low (live transmission only) | Video calls, live streaming |
Here’s how I’d choose:
- If all you want is a smaller file, choose
Canvas + MediaRecorder. - For precise control and MP4 output without shipping 20 MB of WebAssembly, use
WebCodecs + Mediabunny. - For real-time transmission, use
WebRTC. - If you need the full FFmpeg feature set, including complex filters, subtitles, and many formats, or you need older browser support, WebAssembly is still the simplest option.
Appendix
- MediaRecorder API. (n.d.). MDN Web Docs. Retrieved March 6, 2026, from https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder
- WebCodecs API. (n.d.). MDN Web Docs. Retrieved March 6, 2026, from https://developer.mozilla.org/en-US/docs/Web/API/WebCodecs_API
- Manipulating video using canvas. (n.d.). MDN Web Docs. Retrieved March 6, 2026, from https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Manipulating_video_using_canvas
- RTCRtpSender: setParameters() method. (n.d.). MDN Web Docs. Retrieved March 6, 2026, from https://developer.mozilla.org/en-US/docs/Web/API/RTCRtpSender/setParameters
- Vanilagy. (n.d.). mediabunny [GitHub repository]. Retrieved March 6, 2026, from https://github.com/Vanilagy/mediabunny
- WebCodecs. (n.d.). Can I Use. Retrieved March 6, 2026, from https://caniuse.com/webcodecs