VideoFrame
TheVideoFrame 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.
This class follows the W3C WebCodecs VideoFrame specification.
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 newVideoFrame from raw pixel data.
The raw pixel data as an
ArrayBuffer or TypedArray (e.g., Uint8Array).
The data layout must match the specified format.Configuration object for the video frame.
From Existing Frame
Creates a newVideoFrame by cloning an existing frame with optional modifications.
An existing
VideoFrame to copy. The source frame’s data and metadata are copied.Optional configuration to override properties from the source frame.
Properties
All properties are readonly after construction.The pixel format of the frame data (e.g.,
'I420', 'NV12', 'RGBA').
See Pixel Formats for all supported formats.Width of the frame in pixels, including any codec-required padding.
May be larger than
displayWidth.Height of the frame in pixels, including any codec-required padding.
May be larger than
displayHeight.Width the frame should be displayed at, accounting for pixel aspect ratio.
Use this for rendering.
Height the frame should be displayed at, accounting for pixel aspect ratio.
Use this for rendering.
Presentation timestamp in microseconds. Determines when this frame should be
displayed relative to the start of the video.
Duration of this frame in microseconds, or
null if not specified during construction.Color space information for the frame, including primaries, transfer characteristics,
and matrix coefficients. See VideoColorSpace.
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.Optional configuration for the allocation calculation.
number - The size in bytes needed for the buffer.
Example: Calculate buffer size
Example: Calculate buffer size
copyTo()
Copies the frame’s pixel data to a destination buffer.An
ArrayBuffer or TypedArray to copy the data into. Must have at least allocationSize() bytes available.Optional configuration for the copy operation. See
allocationSize() for options.Promise<PlaneLayout[]> - Layout information for each plane in the output buffer.
Example: Copy frame data
Example: Copy frame data
clone()
Creates an independent copy of this frame.VideoFrame - A new VideoFrame with the same data and metadata.
Example: Clone a frame
Example: Clone a frame
close()
Releases the resources held by this frame. After callingclose(), the frame becomes unusable.
Example: Proper resource management
Example: Proper resource management
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 forallocationSize() and copyTo() methods.
Usage with VideoEncoder
VideoFrames are typically passed toVideoEncoder.encode() for compression:
Usage with VideoDecoder
VideoFrames are produced byVideoDecoder 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