Skip to main content

VideoSample

Represents a raw, unencoded video frame. Provides methods for creating, manipulating, and converting video frames across different formats.

Constructor

VideoSampleInit
required
Initialization options for the video sample.

Creating VideoSamples

From VideoFrame

Creates a VideoSample from a VideoFrame.

From Canvas

Creates a VideoSample by rendering a canvas element.

From Raw Pixels

Creates a VideoSample from raw pixel data.

Properties

VideoSamplePixelFormat
The internal pixel format in which the frame is stored. One of 21 supported formats including I420, NV12, RGBA, etc.
number
The width of the frame in pixels.
number
The height of the frame in pixels.
number
The display width accounting for rotation and pixel aspect ratio.
number
The display height accounting for rotation and pixel aspect ratio.
Rotation
The rotation of the frame in degrees (0, 90, 180, 270), clockwise.
number
The presentation timestamp in seconds.
number
The duration in seconds.
VideoSampleColorSpace
The color space information for the frame.

Methods

toVideoFrame()

Converts this sample to a VideoFrame.

copyToCanvas()

Draws this video sample onto a canvas.

copyPixelsTo()

Copies the raw pixel data of this sample to a destination buffer.

clone()

Creates a copy of this video sample.

close()

Releases underlying resources. You must call this when you’re done using the sample.
Always call close() on VideoSamples when you’re done using them to prevent memory leaks. VideoSamples hold underlying resources that won’t be freed automatically.

AudioSample

Represents a raw, unencoded audio buffer. Provides methods for creating, manipulating, and converting audio data.

Constructor

AudioSampleInit
required
Initialization options for the audio sample.

Creating AudioSamples

From AudioData

Creates an AudioSample from an AudioData.

From AudioBuffer

Creates an AudioSample from an AudioBuffer.

From Raw Audio Data

Creates an AudioSample from raw audio data.

Properties

AudioSampleFormat
The internal format of the audio data (e.g., ‘s16’, ‘f32’, ‘s32’, ‘f32-planar’).
number
The number of audio channels (e.g., 2 for stereo).
number
The number of audio frames in this sample.
number
The sample rate in Hz (e.g., 48000).
number
The presentation timestamp in seconds.
number
The duration in seconds.

Methods

toAudioData()

Converts this sample to an AudioData.

toAudioBuffer()

Converts this sample to an AudioBuffer.

copyDataTo()

Copies the raw audio data to a destination buffer.

clone()

Creates a copy of this audio sample.

close()

Releases underlying resources. You must call this when you’re done using the sample.
Always call close() on AudioSamples when you’re done using them to prevent memory leaks.

VideoSamplePixelFormat

The internal pixel format with which a VideoSample is stored. See WebCodecs pixel formats for more details.

VideoSampleColorSpace

Color space information for video samples.

Example

See also