Skip to main content

VideoFrame

The VideoFrame class represents a single frame of video, containing raw pixel data along with timing and layout metadata. VideoFrames can be created from raw pixel buffers or by cloning existing frames.
VideoFrames hold significant memory resources. You must call close() when done with a frame to release its resources. Failing to do so will cause memory leaks.

Quick Example


Constructors

VideoFrame has two constructor overloads: one for creating frames from raw pixel buffers, and one for creating frames from existing VideoFrames.

From Buffer

Creates a new VideoFrame from raw pixel data.
data
BufferSource
required
The raw pixel data as an ArrayBuffer or TypedArray (e.g., Uint8Array). The data layout must match the specified format.
init
VideoFrameBufferInit
required
Configuration object for the video frame.

From Existing Frame

Creates a new VideoFrame by cloning an existing frame with optional modifications.
image
VideoFrame
required
An existing VideoFrame to copy. The source frame’s data and metadata are copied.
init
VideoFrameInit
Optional configuration to override properties from the source frame.

Properties

All properties are readonly after construction.
format
VideoPixelFormat | null
required
The pixel format of the frame data (e.g., 'I420', 'NV12', 'RGBA'). See Pixel Formats for all supported formats.
codedWidth
number
required
Width of the frame in pixels, including any codec-required padding. May be larger than displayWidth.
codedHeight
number
required
Height of the frame in pixels, including any codec-required padding. May be larger than displayHeight.
displayWidth
number
required
Width the frame should be displayed at, accounting for pixel aspect ratio. Use this for rendering.
displayHeight
number
required
Height the frame should be displayed at, accounting for pixel aspect ratio. Use this for rendering.
timestamp
number
required
Presentation timestamp in microseconds. Determines when this frame should be displayed relative to the start of the video.
duration
number | null
required
Duration of this frame in microseconds, or null if not specified during construction.
colorSpace
VideoColorSpace
required
Color space information for the frame, including primaries, transfer characteristics, and matrix coefficients. See VideoColorSpace.
visibleRect
DOMRectReadOnly | null
required
The visible portion of the frame. This defines the crop region that should be displayed. Contains x, y, width, and height properties.

Methods

allocationSize()

Calculates the number of bytes needed to hold the frame’s pixel data.
options
VideoFrameCopyToOptions
Optional configuration for the allocation calculation.
Returns: number - The size in bytes needed for the buffer.

copyTo()

Copies the frame’s pixel data to a destination buffer.
destination
BufferSource
required
An ArrayBuffer or TypedArray to copy the data into. Must have at least allocationSize() bytes available.
options
VideoFrameCopyToOptions
Optional configuration for the copy operation. See allocationSize() for options.
Returns: Promise<PlaneLayout[]> - Layout information for each plane in the output buffer.

clone()

Creates an independent copy of this frame.
Returns: VideoFrame - A new VideoFrame with the same data and metadata.
Both the original and cloned frames must be closed independently. Cloning does not share resources.

close()

Releases the resources held by this frame. After calling close(), the frame becomes unusable.
Always call close() when you’re done with a VideoFrame. Failure to do so will cause memory leaks, as video frames can hold significant amounts of pixel data.

Pixel Formats

VideoFrame supports the following pixel formats:

Type Definitions

PlaneLayout

Describes the layout of a single plane in a pixel buffer.

VideoFrameInit

Configuration for creating a VideoFrame from an existing frame.

VideoFrameBufferInit

Configuration for creating a VideoFrame from raw pixel data.

VideoFrameCopyToOptions

Options for allocationSize() and copyTo() methods.

Usage with VideoEncoder

VideoFrames are typically passed to VideoEncoder.encode() for compression:

Usage with VideoDecoder

VideoFrames are produced by VideoDecoder when decoding encoded chunks:

See Also

VideoEncoder

Encodes VideoFrames into compressed video chunks

VideoDecoder

Decodes compressed video chunks into VideoFrames

VideoColorSpace

Color space metadata for video frames

EncodedVideoChunk

Represents compressed video data