Video
A stream of images (like those produced by a camera) can be logged to Rerun in several different ways:
- Uncompressed, as many
Image
s - Compressed as many
EncodedImage
s, using e.g. JPEG. - Compressed as a single
AssetVideo
, using e.g. MP4. - Compressed as a series of encoded video samples using
VideoStream
, using e.g. H.264 encoded frames.
These alternatives range on a scale of "simple, lossless, and big" to "complex, lossy, and small".
If you want lossless encoded images (with no compression artifacts), then you should log each video frame as Image
.
This will use up a lot of space and bandwidth. You can also encode them as PNG and log them as EncodedImage
,
though it should be noted that PNG encoding usually does very little for the file size of photographic images.
If you want to reduce bandwidth and storage cost, you can encode each frame as a JPEG and log it using EncodedImage
. This can easily reduce the file sizes by almost two orders of magnitude with minimal perceptual loss.
This is also very simple to do, and the Python logging SDK has built-in support for it using Image.compress
.
Finally, for the best compression ratio, you can encode the images as an encoded video. There are two options to choose from:
- Raw video frames
VideoStream
- Video files using
AssetVideo
ā ļø Do not use compressed video if you need accurate pixel replication: this is not only due to the obvious detail loss on encoding, but also since the exact display of the same video is not consistent across platforms and decoder versions.
Streaming video / raw encoded video frames streaming-video--raw-encoded-video-frames
The following example illustrates how to encode uncompressed video frames (represented by numpy
arrays)
using pyAV
into H.264 and directly log them to Rerun using VideoStream
.
"""Video encode images using av and stream them to Rerun."""
import av
import numpy as np
import numpy.typing as npt
import rerun as rr
fps = 30
duration_seconds = 4
width = 480
height = 320
ball_radius = 30
def create_example_video_frame(frame_i: int) -> npt.NDArray[np.uint8]:
img = np.zeros((height, width, 3), dtype=np.uint8)
for h in range(height):
img[h, :] = [0, int(100 * h / height), int(200 * h / height)] # Blue to purple gradient.
x_pos = width // 2 # Center horizontally.
y_pos = height // 2 + 80 * np.sin(2 * np.pi * frame_i / fps)
y, x = np.ogrid[:height, :width]
r_sq = (x - x_pos) ** 2 + (y - y_pos) ** 2
img[r_sq < ball_radius**2] = [255, 200, 0] # Gold color
return img
rr.init("rerun_example_video_stream_synthetic", spawn=True)
# Setup encoding pipeline.
av.logging.set_level(av.logging.VERBOSE)
container = av.open("/dev/null", "w", format="h264") # Use AnnexB H.264 stream.
stream = container.add_stream("libx264", rate=fps)
stream.width = width
stream.height = height
# TODO(#10090): Rerun Video Streams don't support b-frames yet.
# Note that b-frames are generally not recommended for low-latency streaming and may make logging more complex.
stream.max_b_frames = 0
# Log codec only once as static data (it naturally never changes). This isn't strictly necessary, but good practice.
rr.log("video_stream", rr.VideoStream(codec=rr.VideoCodec.H264), static=True)
# Generate frames and stream them directly to Rerun.
for frame_i in range(fps * duration_seconds):
img = create_example_video_frame(frame_i)
frame = av.VideoFrame.from_ndarray(img, format="rgb24")
for packet in stream.encode(frame):
if packet.pts is None:
continue
rr.set_time("time", duration=float(packet.pts * packet.time_base))
rr.log("video_stream", rr.VideoStream.from_fields(sample=bytes(packet)))
# Flush stream.
for packet in stream.encode():
if packet.pts is None:
continue
rr.set_time("time", duration=float(packet.pts * packet.time_base))
rr.log("video_stream", rr.VideoStream.from_fields(sample=bytes(packet)))
Using VideoStream
requires deeper knowledge of the encoding process
but unlike AssetVideo
,
allows the Rerun Viewer to show incomplete or open ended video streams.
In contrast, AssetVideo
requires the entire
video asset file to be in Viewer memory before decoding can begin.
Refer to the video camera streaming example to learn how to stream live video to Rerun.
Current limitations of VideoStream
:
- #9815: Decoding on native is generally slower than decoding in the browser right now. This can cause increased latency and in some cases may even stop video playback.
- #10184, #10185, #10186:
VideoStream
only supports H.264 at this point. - #10090: B-frames are not yet supported for
VideoStream
. - #10422:
VideoFrameReference
does not yet work withVideoStream
.
Remuxing video streams remuxing-video-streams
Sample data from VideoStream
can be queried
and remuxed to mp4 without re-encoding the video as demonstrated in this sample.
Check the doc page on retrieving data to learn more about dataframe queries in general.
Video files video-files
You can use AssetVideo
to log readily encoded video files.
Rerun ignores the timestamp at which the video asset itself is logged and requires you
to log VideoFrameReference
to establish a
correlation of video time to the Rerun timeline.
To ease this, the SDK's read_frame_timestamps_nanos
utility allows to read out timestamps from in-memory video assets:
"""Log a video asset using automatically determined frame references."""
import sys
import rerun as rr
if len(sys.argv) < 2:
# TODO(#7354): Only mp4 is supported for now.
print(f"Usage: {sys.argv[0]} <path_to_video.[mp4]>")
sys.exit(1)
rr.init("rerun_example_asset_video_auto_frames", spawn=True)
# Log video asset which is referred to by frame references.
video_asset = rr.AssetVideo(path=sys.argv[1])
rr.log("video", video_asset, static=True)
# Send automatically determined video frame timestamps.
frame_timestamps_ns = video_asset.read_frame_timestamps_nanos()
rr.send_columns(
"video",
# Note timeline values don't have to be the same as the video timestamps.
indexes=[rr.TimeColumn("video_time", duration=1e-9 * frame_timestamps_ns)],
columns=rr.VideoFrameReference.columns_nanos(frame_timestamps_ns),
)
#7354: Currently, only MP4 files are supported.
Codec support in detail codec-support-in-detail
Overview overview
Codec support varies in the web & native viewer:
Browser | Native | |
---|---|---|
AV1 | ā | š§ |
H.264/avc | ā | ā |
H.265/hevc | š§ | ā |
VP9 | ā | ā |
Details see below.
When choosing a codec, we recommend AV1, as it seems to have the best overall playback support while also having very high compression quality.
Since AV1 can have very long encoding times, it is often not suitable for streaming. In cases where encoding time matters, we recommend H.264/avc.
Native viewer native-viewer
AV1
AV1 is supported out of the box using a software decoder paired with gpu based image conversion.
Current limitations:
- #7755: AV1 is supported on all native builds exception on Linux ARM.
- #10184: AV1 is not yet supported for the
VideoStream
archetype.
H.264/avc
H.264/avc is supported via a separately installed FFmpeg
binary, requiring a minimum version of 5.1
.
The viewer does intentionally not come bundled with FFmpeg
to avoid licensing issues.
By default rerun will look for a system installed FFmpeg
installation in PATH
,
but you can specify a custom path in the viewer's settings.
If you select a video that failed to play due to missing or incompatible FFmpeg
binaries it will offer a download link to a build of FFmpeg
for your platform.
Web viewer web-viewer
Video playback in the Rerun Web Viewer is done using the browser's own video decoder, so the exact supported codecs depend on your browser.
Overall, we recommend using Chrome or another Chromium-based browser, as it seems to have the best video support as of writing.
For decoding video in the Web Viewer, we use the WebCodecs API. This API enables us to take advantage of the browser's hardware accelerated video decoding capabilities. It is implemented by all modern browsers, but with varying levels of support for different codecs, and varying levels of quality.
When it comes to codecs, we aim to support any codec which the browser supports, but we currently cannot guarantee that all of them will work. For more information about which codecs are supported by which browser, see Video codecs on MDN.
We tested the following codecs in more detail:
Linux Firefox | Linux Chrome^1 | macOS Firefox | macOS Chrome | macOS Safari | Windows Firefox | Windows Chrome^2 | |
---|---|---|---|---|---|---|---|
AV1 | ā | ā | ā | ā | š§^3 | ā | ā |
H.264/avc | ā | ā | ā | ā | ā | ā | ā |
H.265/hevc | ā | ā | ā | ā | š§^4 | ā | š§^5 |
hvc1
but working fine with hevc1
. Despite support being advertised Safari 16.5 has been observed not support H.265 decoding. ^1Beyond this, for best compatibility we recommend:
- prefer YUV over RGB & monochrome formats
- don't use more than 8bit per color channel
- keep resolutions at 8k & lower (see also #3782)
Other limitations other-limitations
There are still some limitations to encoded Video in Rerun which will be addressed in the future:
- #7594: HDR video is not supported
- #5181: There is no audio support
- There is no video encoder in the Rerun SDK, so you need to create the video stream or file yourself.
Refer to the video camera streaming example to learn how to encode video using
pyAV
.