---
title: "WebCodecs API: Encode and Decode Video in the Browser - CE.SDK Glossary"
description: "WebCodecs gives web apps the browser's own video and audio encoders and decoders. See what the API does, what it leaves to your code and where it runs."
url: "https://img.ly/glossary/webcodecs/"
type: "page"
---

> This is the markdown version of [WebCodecs API: Encode and Decode Video in the Browser - CE.SDK Glossary](https://img.ly/glossary/webcodecs/). For all pages in one file, see [llms-full.txt](https://img.ly/llms-full.txt). For an index of all available pages, see [llms.txt](https://img.ly/llms.txt).

---

# 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](https://img.ly/glossary/video-codec.md) 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](https://img.ly/glossary/av1.md) 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.

The tool draws 60 frames of 320 by 180 pixels at 30 fps, wraps each in a VideoFrame and encodes them with VideoEncoder at the 1 Mbps the config asks for, using whichever of H.264, H.265 (HEVC), VP8, VP9, AV1 your browser offers. It then decodes the chunks with VideoDecoder from the config the encoder returns and reports the pixel formats, the raw and encoded sizes, how many chunks are key or delta, and how many frames came back.

## 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.

#### Encode

1\. Check the configuration

```typescript
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

```typescript
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

```typescript
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

```typescript
await encoder.flush();
encoder.close();
// chunks and decoderConfig go to a muxing library,
// which writes the MP4 or WebM file.
```

#### Decode

1\. Start from what your demuxer returns

```typescript
// 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

```typescript
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

```typescript
const start = chunks.findIndex((chunk) => chunk.type === 'key');
for (const chunk of chunks.slice(start)) decoder.decode(chunk);
```

4\. Flush and close

```typescript
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](https://img.ly/glossary/av1-vs-hevc.md) encoding patchy outside Apple's platforms.

### Audio encoders vary by format

[Opus](https://img.ly/glossary/opus-codec.md) has broad encoder support and is the standard audio for WebM. [AAC](https://img.ly/glossary/aac-vs-mp3.md), 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](https://img.ly/products/video-sdk.md) 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](https://img.ly/glossary/h264.md) 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](https://img.ly/glossary/platform-notes.md) explain, and the [browser support docs](https://img.ly/docs/cesdk/js/compatibility-139ef9.md) 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](https://img.ly/glossary/cesdk-renderer.md).

**Check video playback and export before loading a scene**

```typescript
// 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](https://img.ly/docs/cesdk/js/actions-6ch24x.md)

## 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.

- **Record a webcam or the screen**: Use: MediaRecorder, Why: It records a stream straight into a WebM or MP4 file, with little control over the encoder

- **Export an edited video**: Use: WebCodecs and a muxer, Why: Frames encode as fast as the encoder can go, not at playback speed

- **Draw a video's frames one by one**: Use: A demuxer and VideoDecoder, Why: Each decoded frame is yours to draw, scale or analyze

- **Send live video with little delay**: Use: WebCodecs, Why: 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**: Use: A WebAssembly build such as ffmpeg.wasm, Why: It brings its own codecs, which the page has to load first

- **Play a video file**: Use: The video element, Why: The browser decodes and plays it in real time, with little control over the decoding

## FAQs

### WebCodecs

**Q: Does WebCodecs work on phones?**

A: 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.

**Q: Can WebCodecs create an MP4 file?**

A: 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.

**Q: Does WebCodecs need HTTPS?**

A: 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.

**Q: Can WebCodecs run in a web worker?**

A: 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.

**Q: Is WebCodecs hardware accelerated?**

A: 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.

## Links

- [Video Editor SDK](https://img.ly/products/video-sdk.md): Your users trim, edit and export video in their own browser, with the encoders it ships.
- [Browser Support](https://img.ly/docs/cesdk/js/compatibility-139ef9.md): See which browsers CE.SDK video needs and where MP4 exports carry Opus audio.
- [To WebM](https://img.ly/docs/cesdk/js/export-save-publish/export/to-webm-764271.md): Write WebM files with the browser's own VP8, VP9 and AV1 encoders and Opus for the sound.

---

## More Resources

- **[IMG.LY Website](https://img.ly/index.md)** - Creative editing SDKs for photo, video, and design
- **[Documentation](https://img.ly/docs/cesdk/)** - CE.SDK developer documentation
- **[Build with a coding agent](https://img.ly/SKILL.md)** - A skill that adds a CE.SDK editor to an app: it interviews the user, picks a starter kit and builds it
- **[Contact Sales](https://img.ly/forms/contact-sales.md)** - Get a custom quote. A public JSON API accepts the request directly, no account or key needed. Ask your user for consent and their details first.
- **[Free Trial](https://img.ly/pricing.md)** - Start building with CE.SDK
