Back to Glossary
Video

WebCodecs

What Is WebCodecs?

WebCodecs is a browser API that hands JavaScript the audio and video encoders and decoders the browser already ships, many of them hardware accelerated, with control over every frame. It is built for apps that process media themselves: video and audio editors, live streaming and video calls.

Before it, an app that needed more than a video element or MediaRecorder brought its own codecs, compiled to WebAssembly or JavaScript, so every visitor downloaded software the browser already had and spent more processing power and battery running it. WebCodecs works on the page and in dedicated web workers, on pages served over HTTPS.

  • VideoFrame and AudioData hold raw pictures and sound samples.
  • EncodedVideoChunk and EncodedAudioChunk hold compressed data, and each video chunk is marked key or delta.
  • VideoEncoder and AudioEncoder turn raw media into chunks.
  • VideoDecoder and AudioDecoder turn chunks back into raw media.
  • ImageDecoder unpacks still and animated images, such as a GIF, into frames.

What Does WebCodecs Leave to Your Code?

WebCodecs stops at frames and chunks. Reading files, writing files and knowing which codec a device can handle are left to you or to a library.

Files need a muxing library

WebCodecs never reads or writes MP4, WebM or any other container. A demuxing library such as Mediabunny or web-demuxer pulls the chunks and the codec string out of a file, and a muxer such as Mediabunny writes encoded chunks into one that a player can open.

Codec strings have to be exact

A short name like vp9 or h264 isn’t enough. WebCodecs wants the full string, such as vp09.00.40.08 or avc1.4d0034, which adds the profile and level (and for VP9 the bit depth) to the video codec name.

Check a configuration before you use it

A configuration the browser can’t handle doesn’t make configure() throw. The encoder closes shortly after and hands the error to your error callback (a NotSupportedError, per the specification), so ask isConfigSupported() first. The answer changes with the browser and the device, and the AV1 page runs that check for five codecs in yours.

Every frame has to be closed

Decoded frames can take up a lot of memory, so the specification asks you to call close() on each one as soon as you no longer need it. Encoding is queued as well: encodeQueueSize shows how many frames are still waiting for the encoder.

What Happens When You Encode Video With WebCodecs?

Raw frames go into an encoder and compressed chunks come out. With its first chunk, the encoder also returns a decoder configuration, which carries the codec string and anything else a decoder needs before it can read the chunks back into frames.

Run WebCodecs in your browser

Pick a codec and the tool encodes a two-second clip on your device, then decodes it again.

  1. 1. Goes in

    VideoFrame

    Frames
    60, 2 seconds at 30 fps
    Size
    320 × 180 pixels
    Pixel format
    After a run
    Raw data
    After a run
  2. 2. Encodes

    VideoEncoder

    Codec string
    After a run
    Bitrate asked for
    1 Mbps
    Key frames asked for
    The first frame only
  3. 3. Comes out

    EncodedVideoChunk

    Chunks
    After a run
    Types
    After a run
    Encoded data
    After a run
  4. 4. Decodes

    VideoDecoder

    Config from the encoder
    After a run
    Frames back
    After a run
    Pixel format
    After a run

The chunks are bytes with timestamps, not a video file: a muxing library has to wrap them in MP4 or WebM before a player can open them. Their size follows the bitrate the config asks for and depends on the browser's encoder.

How Do You Encode and Decode Video With WebCodecs?

Encoding takes four steps: check the configuration, create an encoder, feed it frames and flush it. Decoding mirrors it, starting from the chunks and decoder configuration a demuxing library reads from the file, and it has to begin at a key chunk, because every delta chunk depends on the frames since the last key frame.

1. Check the configuration
const config = {
  codec: 'vp09.00.40.08',
  width: 1280,
  height: 720,
  bitrate: 4_000_000,
  framerate: 30,
};
const { supported } = await VideoEncoder.isConfigSupported(config);
if (!supported) throw new Error(`No encoder for ${config.codec}`);
2. Create the encoder
const chunks = [];
let decoderConfig;
const encoder = new VideoEncoder({
  output(chunk, metadata) {
    chunks.push(chunk);
    if (metadata?.decoderConfig) decoderConfig = metadata.decoderConfig;
  },
  error: console.error,
});
encoder.configure(config);
3. Encode each frame, then close it
const canvas = new OffscreenCanvas(1280, 720);
const ctx = canvas.getContext('2d');
for (let i = 0; i < 90; i++) {
  drawScene(ctx, i); // your drawing code
  const timestamp = Math.round((i * 1_000_000) / 30);
  const frame = new VideoFrame(canvas, { timestamp });
  encoder.encode(frame, { keyFrame: i % 30 === 0 });
  frame.close();
}
4. Flush, then pass the chunks to a muxer
await encoder.flush();
encoder.close();
// chunks and decoderConfig go to a muxing library,
// which writes the MP4 or WebM file.
1. Start from what your demuxer returns
// A demuxing library reads the file and returns the track's
// decoderConfig and its chunks as EncodedVideoChunk objects.
const { supported } = await VideoDecoder.isConfigSupported(decoderConfig);
if (!supported) throw new Error(`No decoder for ${decoderConfig.codec}`);
2. Create the decoder
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const decoder = new VideoDecoder({
  output(frame) {
    ctx.drawImage(frame, 0, 0, canvas.width, canvas.height);
    frame.close();
  },
  error: console.error,
});
decoder.configure(decoderConfig);
3. Decode from a key chunk
const start = chunks.findIndex((chunk) => chunk.type === 'key');
for (const chunk of chunks.slice(start)) decoder.decode(chunk);
4. Flush and close
await decoder.flush();
decoder.close();

Which Browsers Support WebCodecs?

For video, support is close to complete. The gaps sit in single codecs, in audio, and in one mobile browser.

Every major desktop browser handles video

Chrome and Edge, Firefox on the desktop, Chrome on Android and Safari on the Mac, iPhone and iPad all expose VideoEncoder and VideoDecoder. Which codecs they encode still varies: MDN’s codec selection guide calls AV1 encoding limited in Safari and on Android, and HEVC encoding patchy outside Apple’s platforms.

Audio encoders vary by format

Opus has broad encoder support and is the standard audio for WebM. AAC, the audio of most MP4 files, has no WebCodecs encoder in Firefox on any system or in Linux builds of Chrome, and no major browser encodes MP3. Safari added audio encoding well after video.

Firefox for Android has no WebCodecs

There, VideoEncoder is undefined, so a web app that encodes or decodes with WebCodecs needs a fallback or a message for those visitors. Test for the API before you show a feature that depends on it.

How WebCodecs Works in CE.SDK

Video editing with CE.SDK decodes and exports through WebCodecs on the web, so the formats your users can export follow the encoders their browser ships. Neither Firefox nor Chrome on Linux ships an AAC encoder, and CE.SDK writes their MP4 exports with H.264 video and Opus audio. Firefox also converts every decoded frame on the CPU, and a scene with several large videos can show fewer frames while it plays.

Mobile browsers don’t run CE.SDK’s video editing, so phones and tablets get video through the native SDKs, as the platform notes explain, and the browser support docs list the browser versions video needs. Before you load a video scene, ask the engine. Where export isn’t available, the docs point to rendering it server-side with the CE.SDK Renderer.

Check video playback and export before loading a scene
// Each check encodes or decodes a few frames the first time you ask.
const canPlay = await cesdk.actions.run('video.decode.checkSupport', {
  dialog: false,
});
const canExport = await cesdk.actions.run('video.encode.checkSupport', {
  dialog: false,
});
if (canPlay) await cesdk.engine.scene.load(videoSceneUrl);
// No H.264 encoder with AAC or Opus here: export on a server instead.
exportButton.hidden = !canExport;
Actions API guide

WebCodecs, MediaRecorder or WebAssembly?

Pick by the job. A team building a browser video editor, for example, might record webcam clips with MediaRecorder, decode and export the edit with WebCodecs and a muxer, and keep a WebAssembly build of FFmpeg for files in a codec the browser doesn’t ship.

  Use Why
Record a webcam or the screen MediaRecorder It records a stream straight into a WebM or MP4 file, with little control over the encoder
Export an edited video WebCodecs and a muxer Frames encode as fast as the encoder can go, not at playback speed
Draw a video's frames one by one A demuxer and VideoDecoder Each decoded frame is yours to draw, scale or analyze
Send live video with little delay WebCodecs Each chunk comes out as soon as its frame is encoded, with no file wrapped around it
Use a codec the browser doesn't ship A WebAssembly build such as ffmpeg.wasm It brings its own codecs, which the page has to load first
Play a video file The video element The browser decodes and plays it in real time, with little control over the decoding

FAQs

In most mobile browsers, yes. Chrome on Android and Safari on the iPhone ship it, and Firefox for Android doesn't. Which codecs a phone can encode still varies by browser and device, and MDN notes that AV1 encoding is limited on Android, so check each configuration with isConfigSupported() before you offer it.

Not on its own. It returns encoded chunks with timestamps, and a muxing library such as Mediabunny writes them into a playable MP4 or WebM along with the decoder configuration. For the sound in an MP4, keep in mind that Firefox has no AAC encoder on any system, and Chrome has none on Linux.

Yes. The specification limits every WebCodecs interface to secure contexts, so on a site served over plain HTTP, VideoEncoder, VideoDecoder and the rest are missing.

Yes, in dedicated workers as well as on the page. The encoding itself already runs off the main thread, but Chrome's guide suggests handling frames and chunks in a worker too, since their callbacks can fire many times a second. A VideoFrame is transferable, so it can move between the page and a worker.

Often, because it uses the codecs the browser ships, and many of those run on the device's media hardware. The config's hardwareAcceleration hint takes no-preference, prefer-hardware or prefer-software. CE.SDK's compatibility notes add that a device has only so many hardware encoders and decoders, and once another app such as a video call holds them, the work falls back to slower software.