Skip to main content

VideoEncoder

The VideoEncoder class encodes raw VideoFrame objects into compressed EncodedVideoChunk objects. It supports multiple codecs including H.264, H.265/HEVC, VP8, VP9, and AV1, with optional hardware acceleration.
This class implements the W3C WebCodecs VideoEncoder specification. Hardware acceleration is available via platform-specific encoders (VideoToolbox on macOS, NVENC on NVIDIA GPUs, QuickSync on Intel).

Quick Example

Hardware Acceleration Example

Use platform-specific codec names for hardware-accelerated encoding:

Codec Reference


Constructor

Creates a new VideoEncoder with output and error callbacks.
init
VideoEncoderInit
required
Initialization callbacks for handling encoded output and errors.
Throws: TypeError if callbacks are not functions.

Properties

state
CodecState
required
Current encoder state. One of:
  • 'unconfigured' - Not yet configured, or reset() was called
  • 'configured' - Ready to encode frames
  • 'closed' - Encoder has been closed and cannot be used
encodeQueueSize
number
required
Number of pending encode operations in the queue. Useful for implementing backpressure to prevent memory exhaustion when encoding faster than output can be processed.

Methods

isConfigSupported()

Static method to check if a configuration is supported before creating an encoder.
config
VideoEncoderConfig
required
Configuration to test for support.
Returns: Promise<VideoEncoderSupport> with supported boolean and normalized config.

configure()

Configures the encoder with codec parameters. Must be called before encoding frames.
config
VideoEncoderConfig
required
Encoder configuration specifying codec, dimensions, bitrate, and other parameters.
Throws:
  • DOMException if encoder is closed
  • DOMException if codec is not supported
  • DOMException if dimensions are invalid

encode()

Encodes a video frame. The frame is queued for encoding and the output callback is invoked when complete.
frame
VideoFrame
required
The VideoFrame to encode.
options
VideoEncoderEncodeOptions
Optional encoding parameters.
Throws:
  • DOMException if encoder is not configured
  • DOMException if frame is invalid or closed
Always call frame.close() after encoding to prevent memory leaks! The encoder takes a snapshot of the frame data, so the original frame can be closed immediately after encode() returns.

flush()

Waits for all pending encode operations to complete.
Returns: Promise<void> that resolves when all frames have been encoded. Throws:
  • DOMException if encoder is not configured
  • DOMException if an encoding error occurs

reset()

Resets the encoder to unconfigured state, aborting any pending operations.
Throws: DOMException if encoder is closed.

close()

Closes the encoder and releases all resources. The encoder cannot be used after calling close().

addEventListener()

Adds an event listener for encoder events.
type
string
required
Event type. Currently only 'dequeue' is supported.
listener
() => void
required
Callback to invoke when the event fires.
options
object

removeEventListener()

Removes an event listener.

Backpressure Management

When encoding video faster than it can be output (e.g., writing to disk), use encodeQueueSize and dequeue events to implement backpressure:
The dequeue event fires whenever a frame completes encoding, reducing encodeQueueSize. This allows you to throttle input to prevent unbounded memory growth.

Interfaces

VideoEncoderConfig

Configuration options for the encoder.

VideoEncoderInit

Callbacks for encoder initialization.

VideoEncoderOutputMetadata

Metadata returned with encoded chunks.

VideoEncoderEncodeOptions

Options for encoding a single frame.

VideoEncoderSupport

Result from isConfigSupported().

Type Aliases

LatencyMode

BitrateMode

AlphaOption


See Also

VideoFrame

Raw video frame data for encoding

VideoDecoder

Decodes EncodedVideoChunks back to VideoFrames

EncodedVideoChunk

Compressed video data output from encoder

Hardware Acceleration Guide

Platform-specific hardware encoding setup