files/en-us/web/api/videoframe/videoframe/index.md
{{APIRef("Web Codecs API")}}{{AvailableInWorkers("window_and_dedicated")}}
The VideoFrame() constructor creates a new {{domxref("VideoFrame")}} object representing a frame of a video.
new VideoFrame(image)
new VideoFrame(image, options)
new VideoFrame(data, options)
The first type of constructor (see above) creates a new {{domxref("VideoFrame")}} from an image. Its parameters are:
image
VideoFrame. It can be one of the following objects:
an {{domxref("SVGImageElement")}},
an {{domxref("HTMLVideoElement")}},
an {{domxref("HTMLCanvasElement")}},
an {{domxref("ImageBitmap")}},
an {{domxref("OffscreenCanvas")}},
or another {{domxref("VideoFrame")}}.options {{Optional_Inline}}
duration {{Optional_Inline}}
timestamp
alpha {{Optional_Inline}}
"keep": Indicates that the user agent should preserve alpha channel data."discard": Indicates that the user agent should ignore or remove alpha channel data.visibleRect {{Optional_Inline}}
VideoFrame, containing the following:
x
y
width
height
displayWidth {{Optional_Inline}}
VideoFrame when displayed after applying aspect-ratio adjustments.displayHeight {{Optional_Inline}}
VideoFrame when displayed after applying aspect-ratio adjustments.flip {{optional_inline}}
true, horizontal mirroring is applied. Defaults to false.rotation {{optional_inline}}
0. Arbitrary numbers (including negatives) are rounded to the next quarter turn.The second type of constructor (see above) creates a new {{domxref("VideoFrame")}} from an {{jsxref("ArrayBuffer")}}. Its parameters are:
data
VideoFrame.options
format
"I420""I420A""I422""I444""NV12""RGBA""RGBX""BGRA""BGRX"codedWidth
VideoFrame in pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments.codedHeight
VideoFrame in pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments.timestamp
duration {{Optional_Inline}}
layout {{Optional_Inline}}
VideoFrame:
offset
stride
layout is specified, the planes will be tightly packed.visibleRect {{Optional_Inline}}
VideoFrame, containing the following:
x
y
width
height
displayWidth {{Optional_Inline}}
VideoFrame when displayed after applying aspect ratio adjustments.displayHeight {{Optional_Inline}}
VideoFrame when displayed after applying aspect ratio adjustments.colorSpace
VideoFrame, containing the following:
primaries
transfer
matrix
fullRange
true, indicates that full-range color values are used.transfer
VideoFrame will detach and take ownership of. If the array contains the {{jsxref("ArrayBuffer")}} backing data, VideoFrame will use that buffer directly instead of copying from it.flip {{optional_inline}}
true, horizontal mirroring is applied. Defaults to false.rotation {{optional_inline}}
0. Arbitrary numbers (including negatives) are rounded to the next quarter turn.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 {{jsxref("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}}
{{Compat}}