VideoFrame: VideoFrame() constructor
Baseline
2024
*
Newly available
Since September 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Want more support for this feature? Tell us why.
* Some parts of this feature may have varying levels of support.
Note: This feature is available in Dedicated Web Workers.
The VideoFrame() constructor creates a new VideoFrame object representing a frame of a video.
Syntax
new VideoFrame(image)
new VideoFrame(image, options)
new VideoFrame(data, options)
Parameters
The first type of constructor creates a new VideoFrame from an image. Its parameters are:
image-
An image containing the image data for the new
VideoFrame. It can be one of the following objects: anSVGImageElement, anHTMLVideoElement, anHTMLCanvasElement, anImageBitmap, anOffscreenCanvas, or anotherVideoFrame. optionsOptional-
An object containing the following:
alphaOptional-
A string, describing how the user agent should behave when dealing with alpha channels. The default value is "keep".
"keep": Indicates that the user agent should preserve alpha channel data."discard": Indicates that the user agent should ignore or remove alpha channel data.
displayHeightOptional-
The height of the
VideoFramewhen displayed after applying aspect-ratio adjustments. displayWidthOptional-
The width of the
VideoFramewhen displayed after applying aspect-ratio adjustments. durationOptional-
An integer representing the duration of the frame in microseconds.
flipOptional-
A boolean. If
true, horizontal mirroring is applied. Defaults tofalse. metadataOptional-
An object containing metadata describing the video frame, specified by the WebCodecs VideoFrame Metadata Registry, which can contain the following properties:
rtpTimestampOptional-
The RTP timestamp of the corresponding encoded frame. Only video frames originating from WebRTC sources should have
rtpTimestampmetadata set.
Note: A video frame's
metadatacan be returned using theVideoFrame.metadata()method. rotationOptional-
An integer representing the rotation (0, 90, 180, or 270) in degrees clockwise. Defaults to
0. Arbitrary numbers (including negatives) are rounded to the next quarter turn. timestamp-
An integer representing the timestamp of the frame in microseconds.
visibleRectOptional-
An object representing the visible rectangle of the
VideoFrame, containing the following:
The second type of constructor creates a new VideoFrame from an ArrayBuffer. Its parameters are:
data-
An
ArrayBuffer, aTypedArray, or aDataViewcontaining the data for the newVideoFrame. options-
An object containing the following:
codedHeight-
Height of the
VideoFramein pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments. codedWidth-
Width of the
VideoFramein pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments. colorSpace-
An object representing the color space of the
VideoFrame, containing the following:primaries-
A string representing the video color primaries, described on the page for the
VideoColorSpace.primariesproperty. transfer-
A string representing the video color transfer function, described on the page for the
VideoColorSpace.transferproperty. matrix-
A string representing the video color matrix, described on the page for the
VideoColorSpace.matrixproperty. fullRange-
A Boolean. If
true, indicates that full-range color values are used.
displayHeightOptional-
The height of the
VideoFramewhen displayed after applying aspect ratio adjustments. displayWidthOptional-
The width of the
VideoFramewhen displayed after applying aspect ratio adjustments. durationOptional-
An integer representing the duration of the frame in microseconds.
flipOptional-
A boolean. If
true, horizontal mirroring is applied. Defaults tofalse. format-
A string representing the video pixel format. One of the following strings, which are fully described on the page for the
formatproperty:"I420""I420A""I422""I444""NV12""RGBA""RGBX""BGRA""BGRX"
layoutOptional-
A list containing the following values for each plane in the
VideoFrame: metadataOptional-
An object containing metadata describing the video frame, specified by the WebCodecs VideoFrame Metadata Registry, which can contain the following properties:
rtpTimestampOptional-
The RTP timestamp of the corresponding encoded frame.
rotationOptional-
An integer representing the rotation (0, 90, 180, or 270) in degrees clockwise. Defaults to
0. Arbitrary numbers (including negatives) are rounded to the next quarter turn. timestamp-
An integer representing the timestamp of the frame in microseconds.
transfer-
An array of
ArrayBuffers thatVideoFramewill detach and take ownership of. If the array contains theArrayBufferbackingdata,VideoFramewill use that buffer directly instead of copying from it. visibleRectOptional-
An object representing the visible rectangle of the
VideoFrame, containing the following:
Examples
The following examples are from the article Video processing with WebCodecs. In this first example, a VideoFrame is created from a canvas.
const cnv = document.createElement("canvas");
// draw something on the canvas
// …
const frameFromCanvas = new VideoFrame(cnv, { timestamp: 0 });
In the following example a VideoFrame is created from a TypedArray.
const pixelSize = 4;
const init = {
timestamp: 0,
codedWidth: 320,
codedHeight: 200,
format: "RGBA",
};
const data = new Uint8Array(init.codedWidth * init.codedHeight * pixelSize);
for (let x = 0; x < init.codedWidth; x++) {
for (let y = 0; y < init.codedHeight; y++) {
const offset = (y * init.codedWidth + x) * pixelSize;
data[offset] = 0x7f; // Red
data[offset + 1] = 0xff; // Green
data[offset + 2] = 0xd4; // Blue
data[offset + 3] = 0x0ff; // Alpha
}
}
init.transfer = [data.buffer];
const frame = new VideoFrame(data, init);
Specifications
| Specification |
|---|
| WebCodecs> # dom-videoframe-videoframe> |