Skip to content

Encode and save

Compress video with hardware H.264 and H.265 and audio with Opus, send the result to a relay or a WebRTC endpoint, save it to a file, and work with encoded media in Python.

A picture in a stream is a GPU surface, which means nothing outside the machine. To send video anywhere, or keep it, encode it first. The codecs are modules, each a node pair:

Module Nodes Encodes on Linux Encodes on macOS
tatolab-h264 H264Encoder, H264Decoder Vulkan Video VideoToolbox
tatolab-h265 H265Encoder, H265Decoder Vulkan Video VideoToolbox
tatolab-opus OpusEncoder, OpusDecoder libopus, built into the wheel the same

Add them from File › Marketplace… in Tato Studio, or from a terminal:

Terminal window
uv add tatolab-h264 tatolab-opus

Link the camera’s video into H264Encoder’s video. The encoder writes encoded_video: one bare Annex-B access unit per message, stamped with the frame’s own time, a keyframe opening each group. It states the stream’s facts on the port, from its SPS, so a reader downstream knows the codec and size before the first frame.

The encoder takes frames as they come: 8-bit RGBA or BGRA, from a camera, a test pattern, a shader node, a kernel or a decoder. On Linux it encodes with Vulkan Video on a device its own helper process creates, so a fault in the video engine ends that one stream and never the runtime’s GPU device. On macOS it encodes on Apple’s media engine.

Encoder settings, every one optional:

Setting
bitrate_bps Target bitrate. Without one, constant-QP encoding at the session’s balanced preset.
fps The frame rate rate control assumes. Default 60.
keyframe_interval_seconds Seconds between sync points. Default 2.
width, height The size you expect: a guardrail, not a resize. A frame of another size wins, with a warning.
effort_level The hardware encoder’s analysis budget. Refused on macOS.

Encode each: H264Encoder for the camera, OpusEncoder for the microphone. OpusEncoder’s audio input asks for 20 ms windows at 48 kHz, following the source’s channel count, so it takes any microphone without a conversion node; it writes one Opus packet per message on encoded_audio. Its settings are bitrate_bps and application: audio, voip or lowdelay.

Both outputs are stamped on the stream’s clock, so whatever reads them, a WebRTC endpoint or a stream on another machine, lines them up by timestamp.

  • To a MoQ relay, for other streams, other machines and any MoQ client: link each encoded output into a MoqTrackSend. See Share between streams.
  • To a WebRTC endpoint, such as Cloudflare Stream: link both into a WhipPublisher. See WebRTC.
  • To a virtual camera you don’t encode at all: it takes pictures. See Virtual camera.

There’s no file-writer node to install: a file is a few lines of Python. Concatenated, an H.264 encoder’s access units are a raw H.264 stream that any player or ffmpeg reads:

nodes/h264_file.py
from dataclasses import dataclass
from pathlib import Path
from tatolab.stream import Input, Packets, RuntimeContextFullAccess, RuntimeContextLimitedAccess, log, node
@dataclass
class H264FileConfig:
path: str = "recording.h264"
@node.meta(category="Output", description="Appends H.264 access units to a file.")
@node.process
class H264File:
encoded_video = Input(Packets, delivery="ordered", requires_codec=("h264",))
def __init__(self, config: H264FileConfig) -> None:
self.path = config.path
def setup(self, ctx: RuntimeContextFullAccess) -> None:
path = Path(ctx.stream_project_directory) / self.path
self.file = path.open("ab")
log.info("recording", path=str(path))
def process(self, ctx: RuntimeContextLimitedAccess) -> None:
while (unit := ctx.inputs.read(self.encoded_video)) is not None:
self.file.write(bytes(unit.payload))
def teardown(self, ctx: RuntimeContextFullAccess) -> None:
self.file.close()

ffmpeg -i recording.h264 -c copy recording.mp4 puts it in a container afterwards. To write an MP4 directly, use a muxing library such as PyAV in the same node, with each unit’s timestamp.

delivery="ordered" matters here: a recorder wants every unit, in order. If the disk can’t keep up, the link drops units and counts them, and the next group starts clean at a keyframe.

H264Decoder takes access units on encoded_video and writes pictures on video: an RGBA texture from the node’s own pool, in the colour the stream signals. It enters at a group start, and after a loss it discards to the next one, so it never shows a picture whose references it lacks. Its max_width and max_height settings bound the size it prepares for.

A decoder downstream of a MoqTrackReceive or a WhepPlayer is how a stream shows video that came from somewhere else.

A read through a Packets input returns a PacketsUnit:

Attribute
unit.payload The unit’s bytes, borrowed for this process call: bytes(unit.payload) copies them
unit.codec The codec, such as h264 or opus
unit.starts_group Whether a decoder can start here: a keyframe, or any Opus packet
unit.pts Its presentation time

To write encoded media, declare Output(Packets, codec="h264") and write each unit’s bytes, passing starts_group=False for a unit a decoder can’t start at.

  • Linux: a GPU and driver with Vulkan Video encode and decode for the codec. NVIDIA’s proprietary driver has it on recent GPUs; Mesa’s support varies by GPU. Without it, the encoder node fails at setup, saying why, and the rest of the stream runs.
  • macOS: every Apple silicon Mac encodes and decodes both.