Skip to main content

ImageDecoder

The ImageDecoder class decodes compressed image data (JPEG, PNG, WebP, GIF, AVIF) into VideoFrame objects that can be processed, edited, or re-encoded.
This implementation follows the W3C WebCodecs ImageDecoder specification.

Quick Example

Decode a JPEG or PNG image into a VideoFrame:
Always call frame.close() on decoded imagesDecoded VideoFrame objects hold native memory that is not tracked by JavaScript’s garbage collector. Forgetting to close frames will cause memory leaks.

Supported Formats

Use ImageDecoder.isTypeSupported() to check format support at runtime.

Constructor

Creates a new ImageDecoder instance.

Parameters

ImageDecoderInit
required
Configuration object for the decoder.

Example

Accessors

boolean
Whether all image data has been received and parsed.For images loaded from a Buffer, this is true immediately after construction. For images loaded from a ReadableStream, this becomes true when the stream ends.
Promise<void>
A promise that resolves when complete becomes true.Use this to wait for the image to be fully loaded before decoding:
string
The MIME type of the image being decoded.

Static Methods

isTypeSupported()

Check if a MIME type is supported for decoding.
Parameters:
  • type (string): The MIME type to check
Returns: Promise<boolean> - Resolves to true if the format is supported

Instance Methods

decode()

Decode an image frame.
Parameters:
ImageDecodeOptions
Optional decode options.
Returns: Promise<ImageDecodeResult> - Resolves to an object containing the decoded frame

reset()

Reset the decoder state. Aborts any pending decode operations.
Returns: void

close()

Close the decoder and release all resources. The decoder cannot be used after calling this method.
Returns: void

Interfaces

ImageDecodeResult

The result of a decode operation.
VideoFrame
required
The decoded image as a VideoFrame. You must call close() on this frame when done to release memory.
boolean
required
Whether the image is fully decoded. For animated images, this indicates whether this is the last frame.

ImageDecodeOptions

Options for the decode() method.
number
The index of the frame to decode (0-based). Defaults to 0.
boolean
Whether to only return complete frames. Defaults to true.

ImageDecoderInit

Configuration for creating an ImageDecoder.
BufferSource | ReadableStream<BufferSource>
required
The compressed image data.
string
required
The MIME type of the image.
'default' | 'none'
Color space conversion mode.
number
Desired output width for scaling.
number
Desired output height for scaling.
boolean
Whether to prefer animated representation.

See Also

VideoFrame

Learn about the VideoFrame class returned by decode()

VideoEncoder

Encode decoded images into video

Thumbnail Generation

Generate thumbnails from images and videos

Video Transcoding

Complete transcoding workflow