Picamera2 RTSP Streaming with multiple resolution feeds

Using Raspberry Pi OS - 11 (bullseye) Python camera library

This is an update to my previous blog post where I wrote about how to stream a high and low resolution hardware encoded H.264 video feed from a Raspberry Pi and Camera Module over RTSP.

With the release of Raspberry Pi OS based on Debian Bullseye in November 2021, there were some significant changes to the camera driver that deprecated1 the Picamera V1 Python library that I had been using in my previous code. At the time of publishing this post, Picamera2, which is the new library that is compatible with the Bullseye based OS is still a Beta release. With the release of version 0.3.10, the library finally has support for running “multiple encoders, either on the same or different streams”2. This seemed like an appropriate time upgrade the OS on the Pi and redo the script to work with the new Python library.

Below are my setup notes for a new script using the Picamera2 library, FFmpeg (instead of gstreamer) and MediaMTX (formerly rtsp-simple-server).

1.) Start with a clean install of Raspberry Pi OS Lite - Debian version: 11 (bullseye)

2.) Install FFmpeg

1
apt update && apt install ffmpeg

The python3-picamera2 library should already be pre-installed.

3.) Create and edit /root/picam_stream.py

Review the Picamera2 Library manual and Github project page for additional documentation.

4.) Download and install MediaMTX (formerly rtsp-simple-server). Have a look at the Systemd setup instructions in the project README.

5.) Edit the /usr/local/etc/mediamtx.yml configuration.
The main thing that needs to be changed is the section under paths:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
paths:
  all:
    source: publisher
    publishUser: myuser
    publishPass: mypass
    publishIPs: []
    readUser:
    readPass:
    readIPs: []
  hqstream:
    runOnInit: python3 /root/picam_stream.py
    runOnInitRestart: yes

Additionally I’ve disabled RTMP and HLS support since I don’t need it.
My complete config is available in the GitHub Gist.

Then enable and start the systemd service:

1
2
systemctl enable mediamtx
systemctl start mediamtx


6.) Access the RTSP stream.
If everything goes well you should be able to access two streams at the following URLs: rtsp://IP_Address_OF_PI:8554/hqstream and rtsp://IP_Address_OF_PI:8554/lqstream

Some additional optional steps

  • Disable swap
    apt remove dphys-swapfile

  • Save logs to RAM disk to reduce SD card writes.
    Install log2ram

  • Install nginx to serve jpg snapshot

1
2
3
apt install nginx
ln -s /dev/shm/camera.jpg /var/www/html/camera.jpg
# Access the snapshot at http://IP_Address_OF_PI/camera.jpg

  1. https://www.raspberrypi.com/news/bullseye-camera-system/ ↩︎

  2. https://github.com/raspberrypi/picamera2/releases/tag/v0.3.10 ↩︎