RTSP IP Camera, Restreamer, HLS Support

I recently helped someone on OctoPrint's Discord server to get a wyze cam v3 to work with OctoPrint. Thanks to previous work by @Chudsaviet to get integrated HLS support it's fairly easy to transcode rtsp using restreamer. As of this posting the following steps worked to get this integration.

SSH to Pi using putty, run these commands

sudo apt update
sudo apt upgrade
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo groupadd docker
sudo usermod -aG docker $USER
sudo systemctl disable webcamd
sudo systemctl disable ffmpeg_hls
sudo service webcamd stop
sudo service ffmpeg_hls stop
logout

log back in to SSH, run this command. I'd recommend reading restreamer's documentation for optimizing the system to your needs. Make sure to replace the username and password from the default ones listed in the example command line below.

docker run -d --restart always \
     --name restreamer \
     -e "RS_USERNAME=admin" -e "RS_PASSWORD=datarhei" \
     -e "RS_SNAPSHOT_INTERVAL=10" \
     -p 8080:8080 -v /mnt/restreamer/db:/restreamer/db \
     --tmpfs /tmp/hls \
     datarhei/restreamer-armv7l:latest

open browser to http://<your pi's ip>:8080/ and login with username password specified in docker run command above.

add your rtsp stream url in restreamer, set options accordingly and start.

in octoprint's webcam & timelapse settings set the stream url to http://<your pi's ip>:8080/hls/live.stream.m3u8 and snapshot url to http://<your pi's ip>:8080/images/live.jpg

Example rtsp test stream of BigBuckBunny...

3 Likes

Awesome work!
Actually, you don’t need restreamer - ffmpeg can pickup RTSP stream directly.
https://ffmpeg.org/ffmpeg-protocols.html#rtsp

Also /tmp is on SD card in RaspberryPi OS, not in RAM. Use /var/run instead.

Thanks for the tip on RAM pathing.

1 Like

Thanks for the tips!
I notice a 8-12second delay on restreamer. I can't seem to figure out how to remove (well, drastically reduce) this delay. a couple seconds is fine.

That is the drawback of this approach. The streaming technology requires to create a buffer in order to stream, so doubt you'll ever get any lower.

1 Like

I'm noticing that... well, back to USB camera :smiley:
Thank you for the response!

Anyone coming here, I can strongly recommend rtsp2mjpg docker image for this purpose. I have tried all kind of solutions, until I found rtsp2mjpg . It works straight forward and is exactly what you need to use a RTSP camera in Octoprint.

2 Likes

The only drawback to that solution is the final stream is MJPG. Although this works great with OctoPrint, it still is not as network friendly as HLS or with a future release of OctoPrint webrtc and will consume bandwidth very quickly.

It also seemed I was too early with my comment. After trying it out some more. It seems the still image will not work with rtsp2mjpg. Octoprint says it cant capture the still image, while it does show the image when I press the Test button.

My solution now is using Motioneye, which also has an option to restream an rtsp stream. And with motioneye the timelapse does work correctly.

What is that webrtc? Will that allow Octoprint to consume rtsp stream natively?

You would still need something to convert the RTSP stream to something that WebRTC can work with. A quick google gave me this: deepch/RTSPtoWeb: RTSP Stream to WebBrowser (github.com).

The WebRTC handshake protocol has not been fully finalized & documented in OctoPrint just yet - there's still some ongoing discussion, so the above project may not just work.

Can confirm, I tried Restreamer first and not sure why but it wouldn't load in OctoPrint although the URLs seemed to work fine elsewhere, and did show up when I tried the Test button in Webcam settings.

After switching to rtsp2mjpg it worked right away, the setup is actually much simpler, there's no storage needed, and the video and snapshots have much less latency. Highly recommend as well!

1 Like

thanks for the solution but it seems you at least need a raspberry pi 4 with at least 2gb ram for this to work parallel with octoprint. my raspberry 3b can barely handle the workload.

i suspect this is nothing what can be "fixed" to make it less performance-heavy, right?

is there a way to set up restreamer on lower FPS? the lowest i can set it to is 24fps.
maybe setting a lower FPS would reduce performance impact on the system? for me 1 FPS or even lower (0,5 or 0,1 FPS) would be okay.

i haven't found anything on the official restreamer documentation about this... somebody found a solution here?

I get an error when I try the commands:

sudo apt update
sudo apt upgrade
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo groupadd docker
sudo usermod -aG docker $USER
sudo systemctl disable webcamd
sudo systemctl disable ffmpeg_hls
sudo service webcamd stop
sudo service ffmpeg_hls stop
logout

pi@octopi:~ $ sudo apt update
Hit:1 https://download.docker.com/linux/raspbian bullseye InRelease
Hit:2 http://raspbian.raspberrypi.org/raspbian bullseye InRelease
Hit:3 http://archive.raspberrypi.org/debian bullseye InRelease
Reading package lists... Done                 
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
pi@octopi:~ $ sudo apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
pi@octopi:~ $ curl -fsSL https://get.docker.com -o get-docker.sh
pi@octopi:~ $ sudo sh get-docker.sh
# Executing docker install script, commit: a8a6b338bdfedd7ddefb96fe3e7fe7d4036d945a
+ sh -c apt-get update -qq >/dev/null
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
+ sh -c mkdir -p /etc/apt/keyrings && chmod -R 0755 /etc/apt/keyrings
+ sh -c curl -fsSL "https://download.docker.com/linux/raspbian/gpg" | gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
+ sh -c chmod a+r /etc/apt/keyrings/docker.gpg
+ sh -c echo "deb [arch=armhf signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/raspbian bullseye stable" > /etc/apt/sources.list.d/docker.list
+ sh -c apt-get update -qq >/dev/null
+ sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-ce-rootless-extras docker-buildx-plugin >/dev/null
E: Sub-process /usr/bin/dpkg returned an error code (1)

what am I doing wrong?

EDIT: I did finally get all this working however I also have issues with the stream showing up in the Octoprint interface. I can stream it just fine from the restreamer interface. Anyone have a fix for this?