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

VideoFrame

· 52CE 我爱测

The **`VideoFrame`** interface of the Web Codecs API represents a frame of a video.

Web Codecs APIwindow_and_dedicated

The VideoFrame interface of the Web Codecs API represents a frame of a video.

VideoFrame is a transferable object.

Constructor

  • VideoFrame.VideoFrame
    • : Creates a new VideoFrame object.

Instance properties

  • VideoFrame.format
    • : Returns the pixel format of the VideoFrame.
  • VideoFrame.codedWidth
    • : Returns the width of the VideoFrame in pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments.
  • VideoFrame.codedHeight
    • : Returns the height of the VideoFrame in pixels, potentially including non-visible padding, and prior to considering potential ratio adjustments.
  • VideoFrame.codedRect
    • : Returns a DOMRectReadOnly with the width and height matching codedWidth and codedHeight.
  • VideoFrame.visibleRect
    • : Returns a DOMRectReadOnly describing the visible rectangle of pixels for this VideoFrame.
  • VideoFrame.displayWidth
    • : Returns the width of the VideoFrame when displayed after applying aspect ratio adjustments.
  • VideoFrame.displayHeight
    • : Returns the height of the VideoFrame when displayed after applying aspect ratio adjustments.
  • VideoFrame.duration
    • : Returns an integer indicating the duration of the video in microseconds.
  • VideoFrame.timestamp
    • : Returns an integer indicating the timestamp of the video in microseconds.
  • VideoFrame.colorSpace
    • : Returns a VideoColorSpace object.
  • VideoFrame.flip
    • : Returns whether the VideoFrame is horizontally mirrored.
  • VideoFrame.rotation
    • : Returns the rotation (0, 90, 180, or 270) in degrees clockwise applied to the VideoFrame. Arbitrary numbers (including negatives) are rounded to the next quarter turn.

Instance methods

  • VideoFrame.allocationSize()
    • : Returns the number of bytes required to hold the VideoFrame as filtered by options passed into the method.
  • VideoFrame.copyTo()
    • : Copies the contents of the VideoFrame to an ArrayBuffer.
  • VideoFrame.clone()
    • : Creates a new VideoFrame object with reference to the same media resource as the original.
  • VideoFrame.close()
    • : Clears all states and releases the reference to the media resource.
  • VideoFrame.metadata()
    • : Returns the metadata associated with the VideoFrame.

Description

A VideoFrame object can be created or accessed in a number of ways. The MediaStreamTrackProcessor breaks a media track into individual VideoFrame objects.

A VideoFrame is an image source and has a constructor that accepts any other canvas source ( an SVGImageElement, an HTMLVideoElement, an HTMLCanvasElement, an ImageBitmap, an OffscreenCanvas, or another VideoFrame). This means that a frame can be created from an image or video element.

A second constructor enables the creation of a VideoFrame from its binary pixel representation in an ArrayBuffer, a TypedArray, or a DataView.

Created frames may then be turned into a media track, for example with the MediaStreamTrackGenerator interface that creates a media track from a stream of frames.

Examples

In the following example frames are returned from a MediaStreamTrackProcessor, then encoded. See the full example and read more about it in the article Video processing with WebCodecs.

let frameCounter = 0;

const track = stream.getVideoTracks()[0];
const mediaProcessor = new MediaStreamTrackProcessor(track);

const reader = mediaProcessor.readable.getReader();
while (true) {
  const result = await reader.read();
  if (result.done) break;

  let frame = result.value;
  if (encoder.encodeQueueSize > 2) {
    // Too many frames in flight, encoder is overwhelmed
    // let's drop this frame.
    frame.close();
  } else {
    frameCounter++;
    const insertKeyframe = frameCounter % 150 === 0;
    encoder.encode(frame, { keyFrame: insertKeyframe });
    frame.close();
  }
}

Specifications

Browser compatibility

See also

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

资料来源与适用范围

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

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

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