大网数据广告活动
Web 标准与前端开发

VideoFrame: VideoFrame() constructor

· 52CE 我爱测

The **`VideoFrame()`** constructor creates a new VideoFrame object representing a frame of a video.

Web Codecs APIwindow_and_dedicated

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: an SVGImageElement, an HTMLVideoElement, an HTMLCanvasElement, an ImageBitmap, an OffscreenCanvas, or another VideoFrame.
  • options
    • : An object containing the following:
      • alpha

        • : 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.
      • displayHeight

        • : The height of the VideoFrame when displayed after applying aspect-ratio adjustments.
      • displayWidth

        • : The width of the VideoFrame when displayed after applying aspect-ratio adjustments.
      • duration

        • : An integer representing the duration of the frame in microseconds.
      • flip

        • : A boolean. If true, horizontal mirroring is applied. Defaults to false.
      • metadata

        • : An object containing metadata describing the video frame, specified by the WebCodecs VideoFrame Metadata Registry, which can contain the following properties:

          • rtpTimestamp
            • : The RTP timestamp of the corresponding encoded frame. Only video frames originating from WebRTC sources should have rtpTimestamp metadata set.

          [!NOTE] A video frame's metadata can be returned using the VideoFrame.metadata() method.

      • rotation

        • : 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.
      • visibleRect

        • : An object representing the visible rectangle of the VideoFrame, containing the following:
          • x
            • : The x-coordinate.
          • y
            • : The y-coordinate.
          • width
            • : The width of the frame.
          • height
            • : The height of the frame.

The second type of constructor creates a new VideoFrame from an ArrayBuffer. Its parameters are:

  • data
    • : An ArrayBuffer, a TypedArray, or a DataView containing the data for the new VideoFrame.
  • options
    • : An object containing the following:
      • codedHeight
        • : Height of the VideoFrame in pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments.
      • codedWidth
        • : Width of the VideoFrame in 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.primaries property.
          • transfer
            • : A string representing the video color transfer function, described on the page for the VideoColorSpace.transfer property.
          • matrix
            • : A string representing the video color matrix, described on the page for the VideoColorSpace.matrix property.
          • fullRange
            • : A Boolean. If true, indicates that full-range color values are used.
      • displayHeight
        • : The height of the VideoFrame when displayed after applying aspect ratio adjustments.
      • displayWidth
        • : The width of the VideoFrame when displayed after applying aspect ratio adjustments.
      • duration
        • : An integer representing the duration of the frame in microseconds.
      • flip
        • : A boolean. If true, horizontal mirroring is applied. Defaults to false.
      • format
        • : A string representing the video pixel format. One of the following strings, which are fully described on the page for the VideoFrame.format property:
          • "I420"
          • "I420A"
          • "I422"
          • "I444"
          • "NV12"
          • "RGBA"
          • "RGBX"
          • "BGRA"
          • "BGRX"
      • layout
        • : A list containing the following values for each plane in the VideoFrame:
          • offset
            • : An integer representing the offset in bytes where the given plane begins.
          • stride
            • : An integer representing the number of bytes, including padding, used by each row of the plane. Planes may not overlap. If no layout is specified, the planes will be tightly packed.
      • metadata
        • : An object containing metadata describing the video frame, specified by the WebCodecs VideoFrame Metadata Registry, which can contain the following properties:
          • rtpTimestamp
            • : The RTP timestamp of the corresponding encoded frame.
      • rotation
        • : 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 that VideoFrame will detach and take ownership of. If the array contains the ArrayBuffer backing data, VideoFrame will use that buffer directly instead of copying from it.
      • visibleRect
        • : An object representing the visible rectangle of the VideoFrame, containing the following:
          • x
            • : The x-coordinate.
          • y
            • : The y-coordinate.
          • width
            • : The width of the frame.
          • height
            • : The height of the frame.

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

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。