VideoFrame
The WebCodecs VideoFrame, its rectangles, color space and the dictionaries that create and copy it.
- class webrtc.models.video_frame.DOMRectReadOnly(x=0, y=0, width=0, height=0)
An immutable rectangle, like the coded or visible part of a frame.
The width and height may be negative. The edges are then measured from
xandybackwards.See MDN: DOMRectReadOnly.
- Parameters:
- property top: float
The smaller of
yandyplusheight.- Type:
- property right: float
The larger of
xandxpluswidth.See MDN: DOMRectReadOnly/right.
- Type:
- property bottom: float
The larger of
yandyplusheight.See MDN: DOMRectReadOnly/bottom.
- Type:
- property left: float
The smaller of
xandxpluswidth.See MDN: DOMRectReadOnly/left.
- Type:
- classmethod from_rect(other=None)
Creates a rectangle with the origin and size of another one.
See MDN: DOMRectReadOnly/fromRect_static.
- Parameters:
other (
DOMRectInitorDOMRectReadOnly, optional) β The rectangle to copy. When left out, the result is an empty rectangle at the origin.- Returns:
DOMRectReadOnlyβ The new rectangle.- Return type:
- class webrtc.models.video_frame.DOMRectInit(x=0, y=0, width=0, height=0)
A rectangle passed as an option, like the visible part of a new frame.
Wherever one is accepted, a
DOMRectReadOnlyis accepted too.
- class webrtc.models.video_frame.PlaneLayout(offset, stride)
The position of one plane of pixels in a buffer.
- class webrtc.models.video_frame.VideoColorSpace(primaries=None, transfer=None, matrix=None, full_range=None)
The color space of a frame, read from
VideoFrame.color_space. A member isNonewhen unknown.Frames received from a track carry no color space of their own and report BT.601 (
smpte170m) with a limited range. Thatβs the default of the software decoders.See MDN: VideoColorSpace.
- Parameters:
primaries (
webrtc.VideoColorPrimaries, optional) β The color primaries.transfer (
webrtc.VideoTransferCharacteristics, optional) β The transfer characteristics.matrix (
webrtc.VideoMatrixCoefficients, optional) β The matrix that converts between RGB and YUV.full_range (
bool, optional) β Whether samples span the full range of their bits. If not, they use the limited (studio) range.
- class webrtc.models.video_frame.VideoColorSpaceInit(primaries=None, transfer=None, matrix=None, full_range=None)
A color space passed to a new frame. A
VideoColorSpaceis accepted too.See MDN: VideoColorSpace/VideoColorSpace.
- Parameters:
primaries (
webrtc.VideoColorPrimaries, optional) β The color primaries.transfer (
webrtc.VideoTransferCharacteristics, optional) β The transfer characteristics.matrix (
webrtc.VideoMatrixCoefficients, optional) β The matrix that converts between RGB and YUV.full_range (
bool, optional) β Whether samples span the full range of their bits. If not, they use the limited (studio) range.
- class webrtc.models.video_frame.VideoFrameMetadata(rtp_timestamp=None)
Extra facts about a frame, returned by
VideoFrame.metadata().
- class webrtc.models.video_frame.VideoFrameBufferInit(format, coded_width, coded_height, timestamp, duration=None, layout=None, visible_rect=None, rotation=0, flip=False, display_width=None, display_height=None, color_space=None, metadata=None, transfer=<factory>)
Options to create a
VideoFramefrom a buffer of pixels.Only the pixels of the visible rectangle are kept. They become the whole frame, so its coded size is the size of
visible_rect.See MDN: VideoFrame/VideoFrame.
- Parameters:
format (
webrtc.VideoPixelFormat) β The pixel format of the buffer.coded_width (
int) β The width of the pixels in the buffer, which must be positive.coded_height (
int) β The height of the pixels in the buffer, which must be positive.timestamp (
int) β The presentation time in microseconds.duration (
int, optional) β The duration in microseconds.layout (
listofPlaneLayout, optional) β The offset and stride of each plane in the buffer. By default, the planes are tightly packed one after another.visible_rect (
DOMRectInit, optional) β The part of the pixels to keep. Its coordinates are integers aligned to the chroma subsampling. All the pixels are kept by default.rotation (
float, optional) β The clockwise rotation to apply when showing the frame, rounded to the nearest multiple of 90.flip (
bool, optional) β Whether to mirror the frame horizontally when showing it, before rotating.display_width (
int, optional) β The width to show the frame at, given together withdisplay_height. By default, the display size is the visible size after rotation.display_height (
int, optional) β The height to show the frame at, given together withdisplay_width.color_space (
VideoColorSpaceInit, optional) β The color space. By default, itβs sRGB for RGB formats and BT.709 with a limited range for the others.metadata (
VideoFrameMetadata, optional) β Extra facts about the frame, deep-copied.transfer (
listof bytes-like buffers, optional) β Buffers to give up to the frame. The pixels are copied regardless. Transferredmemoryviewobjects are released, but other buffers stay usable.
- class webrtc.models.video_frame.VideoFrameInit(timestamp=None, duration=None, alpha=AlphaOption.keep, visible_rect=None, rotation=0, flip=False, display_width=None, display_height=None, metadata=None)
Options to create a
VideoFramefrom another one. Anything left out is taken from the source frame.The new frame shares the pixels of the source and always keeps its color space.
See MDN: VideoFrame/VideoFrame.
- Parameters:
timestamp (
int, optional) β The presentation time in microseconds.duration (
int, optional) β The duration in microseconds.alpha (
webrtc.AlphaOption, optional) β Whether to keep the alpha channel. Withdiscard, the frame switches to the matching format without alpha.visible_rect (
DOMRectInit, optional) β The part of the sourceβs coded pixels to show. Its coordinates are integers aligned to the chroma subsampling. Unless the display size is given, itβs scaled to match.rotation (
float, optional) β A clockwise rotation added to the sourceβs, rounded to the nearest multiple of 90.flip (
bool, optional) β Whether to mirror the frame horizontally. It toggles the sourceβs flip.display_width (
int, optional) β The width to show the frame at, given together withdisplay_height.display_height (
int, optional) β The height to show the frame at, given together withdisplay_width.metadata (
VideoFrameMetadata, optional) β Extra facts that replace the sourceβs, deep-copied.
- class webrtc.models.video_frame.VideoFrameCopyToOptions(rect=None, layout=None, format=None, color_space=None)
Options for
VideoFrame.copy_to()andVideoFrame.allocation_size().- Parameters:
rect (
DOMRectInit, optional) β The part of the coded pixels to copy. Its coordinates are integers aligned to the chroma subsampling. By default, the visible rectangle is copied.layout (
listofPlaneLayout, optional) β Where to write each plane in the destination. By default, the planes are tightly packed one after another.format (
webrtc.VideoPixelFormat, optional) β The format to write. Itβs either the frameβs own format, or one ofRGBA,RGBX,BGRAandBGRXto convert to RGB.color_space (
webrtc.PredefinedColorSpace, optional) β The color space of an RGB conversion. Onlysrgbis supported, and itβs the default.
- class webrtc.models.video_frame.VideoFrame(image: VideoFrame, /, init: VideoFrameInit | None = None)
- class webrtc.models.video_frame.VideoFrame(data: Buffer, /, init: VideoFrameBufferInit)
One frame of video. It holds the pixels along with how and when to show them.
A frame holds its pixels until
close()is called or awithblock around it ends. Close every frame once done with it, including frames read from a track. A frame that is garbage collected while open emits aResourceWarning.See MDN: VideoFrame.
- Parameters:
source (Buffer | VideoFrame) β A bytes-like buffer of pixels, which is copied into the frame. It can also be another
VideoFrame, whose pixels are shared without copying.init (
VideoFrameBufferInitorVideoFrameInit, optional) β The options. A buffer requires aVideoFrameBufferInit, and a frame takes an optionalVideoFrameInit.
- Raises:
TypeError β If the source or the options are invalid, or the buffer is too small for them.
webrtc.InvalidStateError β If the source frame is closed.
Example:
frame = webrtc.VideoFrame( i420, webrtc.VideoFrameBufferInit(format='I420', coded_width=640, coded_height=480, timestamp=0) )- property format: VideoPixelFormat | None
The pixel format, or
Noneonce closed.- Type:
webrtc.VideoPixelFormat, optional
- property coded_width: int
The width of the stored pixels, or 0 once closed.
See MDN: VideoFrame/codedWidth.
- Type:
- property coded_height: int
The height of the stored pixels, or 0 once closed.
See MDN: VideoFrame/codedHeight.
- Type:
- property coded_rect: DOMRectReadOnly | None
A rectangle covering all the stored pixels, or
Noneonce closed.See MDN: VideoFrame/codedRect.
- Type:
DOMRectReadOnly, optional
- property visible_rect: DOMRectReadOnly | None
The part of the stored pixels to show, or
Noneonce closed.See MDN: VideoFrame/visibleRect.
- Type:
DOMRectReadOnly, optional
- property rotation: int
The clockwise rotation to apply when showing the frame, which is 0, 90, 180 or 270.
Frames from a track carry the rotation that the sender signalled. The pixels themselves are not rotated.
- Type:
- property flip: bool
Whether to mirror the frame horizontally when showing it, before
rotation.See MDN: VideoFrame/flip.
- Type:
- property display_width: int
The width to show the frame at after rotation, or 0 once closed.
See MDN: VideoFrame/displayWidth.
- Type:
- property display_height: int
The height to show the frame at after rotation, or 0 once closed.
See MDN: VideoFrame/displayHeight.
- Type:
- property timestamp: int
The presentation time in microseconds, kept after closing.
See MDN: VideoFrame/timestamp.
- Type:
- property duration: int | None
The duration in microseconds, or
Noneif unknown, as for frames from a track.- Type:
int, optional
- property color_space: VideoColorSpace
The color space of the pixels, kept after closing.
See MDN: VideoFrame/colorSpace.
- Type:
- metadata()
Returns a copy of the frameβs extra facts, like the RTP timestamp of a frame received from a peer.
- Returns:
VideoFrameMetadataβ A deep copy, so changing it leaves the frame untouched.- Raises:
webrtc.InvalidStateError β If the frame is closed.
- Return type:
- allocation_size(options=None)
Computes the minimum destination size for
copy_to()with the same options.See MDN: VideoFrame/allocationSize.
- Parameters:
options (
VideoFrameCopyToOptions, optional) β What to copy and how.- Returns:
intβ The number of bytes.- Raises:
webrtc.InvalidStateError β If the frame is closed.
TypeError β If the options are invalid, like a rectangle outside the frame or overlapping planes.
webrtc.NotSupportedError β If the format or color space canβt be converted to.
- Return type:
- copy_to(destination, options=None)
Copies the pixels into a buffer, converting them to RGB if asked.
The copy happens before this returns, so the future is already done. Call it from a running event loop. Errors are set on the future and not raised. RGB conversion uses the frameβs matrix and range.
- Parameters:
destination (
bytearrayor writablememoryview) β The buffer to write to, at leastallocation_size()bytes long.options (
VideoFrameCopyToOptions, optional) β What to copy and how.
- Returns:
asyncio.Futureβ Resolves to alistofPlaneLayoutthat says where each plane was written. It fails with the errors ofallocation_size(), orTypeErrorif the buffer is too small.- Return type:
Future[list[PlaneLayout]]
- clone()
Creates another frame that shares these pixels without copying them. Each frame must be closed separately.
- Returns:
VideoFrameβ The new frame.- Raises:
webrtc.InvalidStateError β If the frame is closed.
- Return type:
- close()
Releases this frameβs hold on the pixels. Closing a closed frame does nothing.
After that, the size and rectangle attributes read as empty, and most methods raise
webrtc.InvalidStateError.