VideoEncoder
TheVideoEncoder 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 newVideoEncoder with output and error callbacks.
Initialization callbacks for handling encoded output and errors.
TypeError if callbacks are not functions.
Example: Creating an encoder
Example: Creating an encoder
Properties
Current encoder state. One of:
'unconfigured'- Not yet configured, orreset()was called'configured'- Ready to encode frames'closed'- Encoder has been closed and cannot be used
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.Configuration to test for support.
Promise<VideoEncoderSupport> with supported boolean and normalized config.
Example: Checking codec support
Example: Checking codec support
configure()
Configures the encoder with codec parameters. Must be called before encoding frames.Encoder configuration specifying codec, dimensions, bitrate, and other parameters.
DOMExceptionif encoder is closedDOMExceptionif codec is not supportedDOMExceptionif dimensions are invalid
Example: Basic configuration
Example: Basic configuration
Example: HDR encoding with VP9
Example: HDR encoding with VP9
encode()
Encodes a video frame. The frame is queued for encoding and the output callback is invoked when complete.The
VideoFrame to encode.Optional encoding parameters.
DOMExceptionif encoder is not configuredDOMExceptionif frame is invalid or closed
Example: Encoding with keyframe control
Example: Encoding with keyframe control
flush()
Waits for all pending encode operations to complete.Promise<void> that resolves when all frames have been encoded.
Throws:
DOMExceptionif encoder is not configuredDOMExceptionif an encoding error occurs
Example: Flushing before close
Example: Flushing before close
reset()
Resets the encoder to unconfigured state, aborting any pending operations.DOMException if encoder is closed.
Example: Reconfiguring encoder
Example: Reconfiguring encoder
close()
Closes the encoder and releases all resources. The encoder cannot be used after callingclose().
Example: Proper cleanup
Example: Proper cleanup
addEventListener()
Adds an event listener for encoder events.Event type. Currently only
'dequeue' is supported.Callback to invoke when the event fires.
removeEventListener()
Removes an event listener.Backpressure Management
When encoding video faster than it can be output (e.g., writing to disk), useencodeQueueSize 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 fromisConfigSupported().
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