MediaMTX: streaming a Raspberry Pi camera via RTSP and WebRTC

MediaMTX turns a Raspberry Pi with a camera module into a fully fledged network camera. MediaMTX is a small media server in a single file that can talk to the Pi camera directly through libcamera and serves the picture simultaneously via RTSP (for VLC, Frigate, Home Assistant or an NVR), WebRTC (in the browser with very low latency) and HLS. Additional programs such as ffmpeg are not required, and video encoding is handled by the Pi’s hardware encoder.

Requirements

  • Raspberry Pi 3, 4, 5 or Zero 2 W
  • An official camera module (e.g. Camera Module 3 or HQ Camera) or a compatible libcamera camera
  • Raspberry Pi OS Lite (64-bit), “Bookworm” or newer – setting it up without a monitor is described in the article Raspberry Pi headless setup

Update the system first:

sudo apt update && sudo apt -y full-upgrade && sudo reboot

1. Check the camera

rpicam-hello --list-cameras

The camera should be listed here with its model and supported resolutions. (On older systems the command is libcamera-hello --list-cameras.) If nothing is found: check the ribbon cable – the contacts must face the right way, and the Pi 5 needs the narrower cable – and make sure that /boot/firmware/config.txt contains the line camera_auto_detect=1.

In addition, v4l2-ctl --list-devices lists all video devices (package v4l-utils).

2. Install MediaMTX

MediaMTX is not available in the package repositories; you download it as a ready-made binary from the release page on GitHub. For Raspberry Pi OS 64-bit the right file is …_linux_arm64.tar.gz (…_linux_arm64v8.tar.gz for older releases); for a 32-bit system take the armv7 variant. Replace the version number in the following example with the current one – at the time of my first installation that was v1.11.2:

VERSION=v1.11.2
cd /tmp
wget https://github.com/bluenviron/mediamtx/releases/download/${VERSION}/mediamtx_${VERSION}_linux_arm64v8.tar.gz
tar -xvf mediamtx_${VERSION}_linux_arm64v8.tar.gz

sudo mv mediamtx /usr/local/bin/
sudo mkdir -p /etc/mediamtx
sudo mv mediamtx.yml /etc/mediamtx/

3. Add the camera to the configuration

sudo nano /etc/mediamtx/mediamtx.yml

At the very bottom of the file you will find the paths: section. Add a path for the camera there. Indentation with spaces matters – YAML is strict about it:

paths:
  cam:
    source: rpiCamera
    rpiCameraWidth: 1920
    rpiCameraHeight: 1080
    rpiCameraFPS: 30
    # flip the image if the camera is mounted upside down:
    # rpiCameraHFlip: true
    # rpiCameraVFlip: true

  all_others:

The name cam becomes part of the stream address and can be chosen freely. all_others: already exists in the file and stays where it is. If two cameras are connected to a Pi 5, rpiCameraCamID: 1 selects the second one.

A first test right in the console:

sudo /usr/local/bin/mediamtx /etc/mediamtx/mediamtx.yml

If lines such as [path cam] [RPI Camera source] started appear without errors, everything is working. Quit with Ctrl+C.

4. Start automatically as a service

To have MediaMTX running after every reboot and restarting after a crash, create a systemd unit:

sudo nano /etc/systemd/system/mediamtx.service
[Unit]
Description=MediaMTX media server
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/bin/mediamtx /etc/mediamtx/mediamtx.yml
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now mediamtx.service
systemctl status mediamtx.service

The output ends up in the journal: journalctl -u mediamtx -f. More on Restart= and restarting services automatically can be found in the article Monitoring and automatically restarting services with systemd and monit.

5. Watch the stream

In the examples the Pi has the address 192.168.178.40:

ProtocolAddressSuitable for
WebRTChttp://192.168.178.40:8889/cam/Browser, very low latency
RTSPrtsp://192.168.178.40:8554/camVLC, Frigate, Home Assistant, NVR
HLShttp://192.168.178.40:8888/cam/Browser, smart TVs (a few seconds of delay)

In VLC: Media → Open Network Stream and enter the RTSP address. On the command line, ffplay rtsp://192.168.178.40:8554/cam does the job.

6. Protect access with a password

By default, anyone on the network may watch the stream. In mediamtx.yml the authInternalUsers section controls the permissions. Change the existing entry for the user any so that it may only read locally (from the Pi itself), and add a user of your own:

authInternalUsers:
  - user: any
    pass:
    ips: ['127.0.0.1', '::1']
    permissions:
      - action: read
      - action: publish
      - action: api
      - action: metrics
      - action: pprof
  - user: camera
    pass: a-secure-password
    ips: []
    permissions:
      - action: read
        path: cam

Then run sudo systemctl restart mediamtx. The RTSP address is now rtsp://camera:a-secure-password@192.168.178.40:8554/cam; in the browser MediaMTX asks for the credentials. Never expose the stream directly to the internet – for access on the go, use a VPN such as WireGuard instead.

7. Add it to Frigate and Home Assistant

Frigate takes the RTSP stream directly, without the picture having to be re-encoded:

cameras:
  pi_camera:
    ffmpeg:
      inputs:
        - path: rtsp://camera:a-secure-password@192.168.178.40:8554/cam
          roles:
            - detect
            - record
    detect:
      width: 1920
      height: 1080
      fps: 5

In Home Assistant the camera is added through the Generic Camera integration, with the RTSP address as the “Stream Source URL”. If Frigate is already running, it is usually easier to bring the camera in through the Frigate integration.

Useful extras