archive2026

An old Pi 3B+ as an RTSP camera with MediaMTX

Hardware H.264 from rpicam-vid, republished through MediaMTX as RTSP so any player or script on the network can read it.

3 min read#edge#raspberry-pi#video

I had a Raspberry Pi 3B+ in a drawer and a camera module, and I wanted a stream that both a video player and a detection script could read at the same time. The 3B+ is good enough for this. Its GPU has a hardware H.264 encoder, so streaming costs almost no CPU, and RTSP is understood by VLC, ffmpeg, OpenCV and most NVR software.

rpicam-vid can listen on a TCP or UDP port by itself, and that has the lowest latency. But then only one client can connect, and restarting is awkward. A small media server fixes that: the camera publishes once, and any number of clients read from the server. MediaMTX is a single static binary that does this. It can also serve the same source as WebRTC or HLS, which I have not tried yet.

Prerequisites

You need the Pi with a camera module, Raspberry Pi OS (Bookworm or newer) and ffmpeg (sudo apt install ffmpeg).

Camera tools were renamed in Bookworm from libcamera-* to rpicam-*. Check yours with:

rpicam-hello --list-cameras

Run MediaMTX

Download the release for your OS from the project’s releases page (the armv7 build for a 32-bit OS, the 64-bit ARM build for a 64-bit OS; the exact file names have changed over time). Unpack it and start it:

./mediamtx

By default it accepts RTSP publishers and readers on port 8554.

Publish the camera

rpicam-vid -t 0 -n --inline --codec h264 \
  --width 1280 --height 720 --framerate 25 -o - \
| ffmpeg -loglevel warning -re -f h264 -i - -c:v copy \
  -f rtsp rtsp://127.0.0.1:8554/cam
  • -t 0 runs forever and -n disables the preview window.
  • --inline repeats the stream headers so a client that connects late can start decoding.
  • -c:v copy passes the already-encoded video through without re-encoding.

Watch it

From another machine:

ffplay -fflags nobuffer rtsp://raspberrypi.local:8554/cam

Or read it in Python:

import cv2

cap = cv2.VideoCapture("rtsp://raspberrypi.local:8554/cam")
ok, frame = cap.read()

For detection, run the model on another machine. A 3B+ is too slow to run it next to the stream; on a Pi 5 a small model can run on the board itself.

Expect a second or two of latency. Most of it comes from buffering in the player, so try player options like -fflags nobuffer before you change camera settings. It is fine for monitoring, not for anything that needs to react quickly.

Keep it running

Put the pipeline in a script and run it as a systemd service so it restarts after a crash or reboot:

# /etc/systemd/system/camera-stream.service
[Unit]
Description=Camera to RTSP
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/bin/camera-stream.sh
Restart=always
RestartSec=3
User=camera

[Install]
WantedBy=multi-user.target

camera is a system user for this service only. Since the default pi user is gone (2022 change) I create one per service. It must be in the video group to open the camera:

sudo useradd --system --no-create-home --groups video camera
sudo systemctl enable --now camera-stream

Run MediaMTX as its own service in the same way.

Notes

The Pi 3B+ and Pi 4 have a hardware H.264 encoder. The Pi 5 does not, so on a Pi 5 the same command encodes in software and uses real CPU. The -c:v copy in the ffmpeg command matters for the same reason: the video is encoded once, by the GPU, and only passed through after that.

RTSP has no encryption or login by default. I keep the port on the LAN, and for viewing from outside I use a VPN instead of opening the port. MediaMTX has its own authentication if you need it.

If you want a browser view without any player, MJPEG tools like motion work with a plain <img> tag, but they use far more bandwidth. And if the Pi itself has to react to what it sees, I would use picamera2 in Python instead, which gives you the frames directly.