Skip to main content
Video sources are used to add video samples (frames) to output video tracks. All video sources extend the base VideoSource class.

CanvasSource

Captures frames from an HTML canvas or OffscreenCanvas element and encodes them for the output video track.

Constructor

HTMLCanvasElement | OffscreenCanvas
required
The canvas element to capture frames from.
VideoEncodingConfig
required
Configuration object that controls video encoding.

Methods

add

Captures the current canvas state as a video frame, encodes it, and adds it to the output.
number
required
The timestamp of the sample in seconds.
number
default:"0"
The duration of the sample in seconds.
VideoEncoderEncodeOptions
Additional WebCodecs encoding options.
Promise<void>
Resolves when the output is ready to receive more samples. Await this to respect backpressure.

close

Closes the source, preventing future samples and signaling no more data will be added to this track.
Calling close() is optional but recommended after adding the last sample for improved performance and reduced memory usage.

VideoSampleSource

Adds raw, unencoded video samples (frames) to an output video track with automatic encoding.

Constructor

VideoEncodingConfig
required
Configuration object that controls video encoding. See CanvasSource for detailed properties.

Methods

add

Encodes a video sample (frame) and adds it to the output.
VideoSample
required
The video sample to encode and add.
VideoEncoderEncodeOptions
Additional WebCodecs encoding options.
Promise<void>
Resolves when the output is ready to receive more samples. Await this to respect backpressure.

close

Closes the source. See CanvasSource.close() for details.

EncodedVideoPacketSource

Directly pipes pre-encoded video packets into the output file without additional encoding.

Constructor

VideoCodec
required
The codec used to encode the packets: 'avc', 'hevc', 'vp8', 'vp9', or 'av1'.

Methods

add

Adds an encoded packet to the output video track. Packets must be added in decode order, while timestamps must be presentation timestamps. B-frames are handled automatically.
EncodedPacket
required
The encoded video packet to add. Cannot be a metadata-only packet.
EncodedVideoChunkMetadata
Additional encoder metadata. You should pass this for the first call, including a valid decoder config.
Promise<void>
Resolves when the output is ready to receive more samples. Await this to respect backpressure.

close

Closes the source. See CanvasSource.close() for details.

MediaStreamVideoTrackSource

Encodes frames from a live MediaStreamVideoTrack (e.g., webcam or screen capture) and pipes them to the output. Frames are automatically captured once the connected Output is started.

Constructor

MediaStreamVideoTrack
required
The video MediaStreamTrack to capture frames from.
VideoEncodingConfig
required
Configuration object that controls video encoding. latencyMode is automatically set to 'realtime'. See CanvasSource for other properties.

Properties

Promise<never>
A promise that rejects upon any error within this source. This promise never resolves. You should handle this promise to catch internal errors.
boolean
Whether this source is currently paused as a result of calling pause().

Methods

pause

Pauses the capture of video frames. Frames emitted by the media stream are ignored while paused. This does not close the underlying track.

resume

Resumes the capture of video frames after being paused.

close

Stops capturing and closes the source. See CanvasSource.close() for details.
Make sure to handle the errorPromise field so that internal errors are properly surfaced.