Skip to content

Instantly share code, notes, and snippets.

@bakman2
Last active September 19, 2024 21:01
Show Gist options
  • Save bakman2/521fd7333b4fad6602b9f9955c4fbc2c to your computer and use it in GitHub Desktop.
Save bakman2/521fd7333b4fad6602b9f9955c4fbc2c to your computer and use it in GitHub Desktop.
Octoprint IP Camera

How to use Octoprint with an IP camera that has an RTSP stream available

Optional: For Wyze cam only

Install Dafang Hacks on the Wyze Cam.

Once running and all is functional, continue. Set video bitrate to 1500, VBR, 30fps.

Installation

If OctoPrint is running on a Raspberry Pi (3B minimum):

ssh pi@<raspberry_ip>
sudo apt update
sudo apt install vlc

VLC will be used to transcode the rtsp stream to a http stream which OctoPrint can use as streaming URL

nano http_stream.sh

paste the following line:

cvlc -I dummy rtsp://ip_of_yourcam --sout "#transcode{vcodec=mjpg,quality=3,fps=5,acodec=none,width=1280,height=720}:standard{access=http{mime=multipart/x-mixed-replace; boundary=7b3cc56e5f51db803f790dad720ed50a},mux=mpjpeg,dst=:8888/videostream.cgi}" 2>/dev/null

Change the ip_of_yourcam to match the full rtsp url from your camera

CTRL+x to save

chmod +x http_stream.sh
./http_stream.sh

Now you should be able to open your browser:

http://<raspberryip>:8888/videostream.cgi

If it works, you can use this url in OctoPrint for the Stream URL

For the snapshot URL use the stills url from your camera

Change your user/pass and ip to match your setup Click advanced options and uncheck Validate SSL on snapshot URL (if applicable)

As this process is cpu intensive you will need to play around with the quality=x,fps=x (fps = frames per second) options in the cvlc command. These settings work for me, but as I wanted better quality I have this process running on a more powerful device.

@GitHubEmploy
Copy link

This code consumes a lot of CPU resources and due to the delay octoprint cannot grab the jpg from the link. Is there any possibility to save the jpg locally and then octoprint to retrieve it asyncronously from one http address?

You're getting the hit of Python loops and openCV stream conversion. The VLC command that started this thread can be run from terminal w/out the overhead of Python. The command creates a HTTP endpoint and the framerate is adjustable. I found on a RP3 FPS=1 is about max framerate before the video feed starts to lag.
I've been using this command to create the stream and it's worked flawless for months.
cvlc -R rtsp://user:pwd@IPADDRESS/live --sout-x264-preset fast --sout='#transcode{acodec=none,vcodec=MJPG,vb=100,fps=1}:standard{mux=mpjpeg,access=http{mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a},dst=:8888/videostream.cgi}' --sout-keep

Good point, thanks for this, I will try it. But what about the snapshot url? I need this for octolapse and my rtsp camera doens't have a link of its own.

By the way, what is the CPU consumption on raspberry pi with your script?

You can view live video at raspberryipadress:8080, and snapshots at raspberryipadress:8080/video_feed

The CPU consumption is around 12 - 14% estimation

@GitHubEmploy
Copy link

GitHubEmploy commented Feb 17, 2021

This code consumes a lot of CPU resources and due to the delay octoprint cannot grab the jpg from the link. Is there any possibility to save the jpg locally and then octoprint to retrieve it asyncronously from one http address?

it's actually set to only display the picture when asked, so averaged for around 12 - 14% CPU consumption usage, otherwise, it runs in the backround at around 1.3% CPU usage

The only real problem is 24/h livestreams, those can really wear down on your CPU, so i dont suggets them, tho it still seems fine.

@kdavid2
Copy link

kdavid2 commented Feb 17, 2021

Anyways, with this solution, you can view live video at raspberryipadress:8080, and snapshots at raspberryipadress:8080/video_feed

Partial success. While I can view the stream and snapshot on my browser:

  1. If I am viewing the first stream on one browser tab and then I try to open a second tab to view for example the snapshot stream, the latter never opens and the program crashes (which means the first instance stops showing the stream and the second never opens). After some time the script crashes.
  2. The links don't work on octopi. In webcam preview mode there is no picture.

I guess this is due to big processing times. I guess one suggested solution is to run the script constantly and cache the jpg locally every 0.5 seconds. On HTTP GET request it should serve the saved jpg instead of doing real time processing. The way it is now it cannot work with octopi.

Hi GitHubEmploy. Thanks for your reply. The script you suggested doesn't work for me at all inside Octoprint. Please see above. If there is any possibility to debug it let me know.

@kdavid2
Copy link

kdavid2 commented Feb 18, 2021

vlc

Unfortunately, I didn't have luck even with vlc. I receive the errors shown in the picture BUT the stream works flawlessly on a browser via the link "http://raspberrypiaddress:8888/videostream.cgi" but nothing appears in octoprint. The message "webcam stream loading" is blinking and that's all. I rest my case... I don't know what's wrong. I even tried octoprint's webcam iframe plugin, but no luck either... Any thoughts?

@GitHubEmploy
Copy link

GitHubEmploy commented Feb 18, 2021

Honestly, at this point I'm just streaming it to youtube and using the embed url along with iframe

@JoeDante
Copy link

Yea,

I was able to get it to run. This is what I did:
Start by log in to your Raspberry Pi.
To create the http_stream.sh file write:

sudo nano http_stream.sh

Paste in this in your http_stream.sh:

cvlc -R rtsp://USERNAME:PASSWORD@IP_OF_WYZECAM/live --sout-x264-preset fast --sout='#transcode{acodec=none,vcodec=MJPG,vb=100,fps=0.5}:standard{mux=mpjpeg,access=http{mime=multipart/x-mixed-replace; boundary=--7b3cc56e5f51db803f790dad720ed50a},dst=:8888/videostream.cgi}' --sout-keep

Press CTRL + X to save the file
To get it to run every time I reboot the Pi this is what I did:
Created a service file in /etc/systemd/system/ called wyze.service by writing:

sudo nano /etc/systemd/system/wyze.service

Write this in the wyze.service file:

[Unit]
Description=Start Wyze stream
After=network.target

[Service]
ExecStart=/bin/sh /home/pi/http_stream.sh
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Save the file by pressing CTRL + X
Write:

systemctl daemon-reload

Enable the service by writing:

systemctl enable wyze.service

Confirm that the service is running by writing:

systemctl --all | grep wyze.service

Start the service by writing:

systemctl start wyze.service

Now go to your browser and type in:
http://IP_OF_YOUR_PI:8888/videostream.cgi
and you should see the picture from your camera!

Yea, thanks, this was really helpful!

I was able to get it to run. This is what I did:

Start by log in to your Raspberry Pi.
To create the http_stream.sh file write:

sudo nano http_stream.sh

Paste in this in your http_stream.sh (make sure you change the USERNAME and PASSWORD and IP_OF_WYZECAM to whatever you have in the app under the RTSP settings) :

cvlc -R rtsp://USERNAME:PASSWORD@IP_OF_WYZECAM/live --sout-x264-preset fast --sout='#transcode{acodec=none,vcodec=MJPG,vb=100,fps=0.5}:standard{mux=mpjpeg,access=http{mime=multipart/x-mixed-replace; boundary=--7b3cc56e5f51db803f790dad720ed50a},dst=:8888/videostream.cgi}' --sout-keep

Press CTRL + X to save the file

To get it to run every time I reboot the Pi this is what I did:

Created a service file in /etc/systemd/system/ called wyze.service by writing:

sudo nano /etc/systemd/system/wyze.service

Write this in the wyze.service file:

[Unit]
Description=Start Wyze stream
After=network.target

[Service]
ExecStart=/bin/sh /home/pi/http_stream.sh
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Save the file by pressing CTRL + X

Write:

systemctl daemon-reload

Enable the service by writing:

systemctl enable wyze.service

Confirm that the service is running by writing:

systemctl --all | grep wyze.service

Start the service by writing:

systemctl start wyze.service

Now go to your browser and type in:
http://IP_OF_YOUR_PI:8888/videostream.cgi
and you should see the picture from your camera!

I did that, and it worked fine so far. BUT: my videofeed is incredible slow. Like one frame every 10 seconds, so one minute of realtime takes up 10 minutes of video. CPU soesn't seem to be the problem, I got a Pi 4 and it shows about 33% CPU activity. Does anyone know what could cause the Problenm? BTW, I got a Yi cam with Yi Hack firmware that supports RTSP streams.

@GitHubEmploy
Copy link

Okay, so update on this issue, I have no clue how to stream RTSP to octoprint anymore. So far, iv been using the RTSP to be converted locally using TinyCam on a 10 dollar tablet, then restreamed locally. But that's not the ideal solution, and I need to come up with a new one. IF anyone has any ideas that would be great.

@JoeDante
Copy link

We definititely need a plugin for that....

@GitHubEmploy
Copy link

UPDATE: Half a year later, a solution was made. https://github.com/aler9/rtsp-simple-server is what I'm currently using, and it works great, no problems yet.

@bakman2
Copy link
Author

bakman2 commented Oct 29, 2021

UPDATE: Half a year later, a solution was made. https://github.com/aler9/rtsp-simple-server is what I'm currently using, and it works great, no problems yet.

Can you share your settings for this server ?

@Cjkeenan
Copy link

@GitHubEmploy I also would appreciate if you could share your settings. I have it outputting my RTSP stream on my Reolink E1 Pro to my RPi, but I would like to stream that out to my network as a mp4 or something readable by OctoPrint.

@RonaldCoutinho
Copy link

RonaldCoutinho commented Feb 25, 2022

Hi, guys!

I solved my problem in a simple way, after a lot of research, I entered this post with this smart solution and very easy to execute! Before running the .BAT it is necessary to have VLC installed on your computer, then just replace your rtsp:// link inside the BAT and that's it!

:: The following line is neccessary if you need an ability to restart the streams with this batch file
:: Kill all existing streams (the command actually suspends ALL the vlc processes):
taskkill /f /im "vlc.exe"

:: Run two instances of VLC. These would transcode MP4 rtsp-stream to Motion JPEG http-stream:
start vlc -vvv -Idummy rtsp://login:[email protected]/streaming/channels/2/preview --sout #transcode{vcodec=MJPG,venc=ffmpeg{strict=1},fps=10,width=640,height=360}:standard{access=http{mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a},mux=mpjpeg,dst=:9911/}
start vlc -vvv -Idummy rtsp://login:[email protected]/streaming/channels/2/preview --sout #transcode{vcodec=MJPG,venc=ffmpeg{strict=1},fps=10,width=640,height=360}:standard{access=http{mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a},mux=mpjpeg,dst=:9912/}

:: In order to execute VLC with `vlc` as in exapmle above, you have to add corresponding value to the PATH variable.
:: Otherwise you have to use full path e.g `"C:\Program Files\VLC\vlc.exe"`.

:: Such rtsp-stream uri as rtsp://login:[email protected]/streaming/channels/2/preview is specific for Hikvision cameras.

:: Access the http-streams like that:
:: For the first camera: http://127.0.0.1:9911 
:: For the the second camera: http://127.0.0.1:9912 
:: For external access change `127.0.0.1` to the address of the server (or PC) you are running VLC on.
:: Your server's Ports you are using for streaming (9911 and 9912 from current example) must be open for external access.

by @JonesTwink/cctv-startup.bat

@bastel-123
Copy link

UPDATE: Half a year later, a solution was made. https://github.com/aler9/rtsp-simple-server is what I'm currently using, and it works great, no problems yet.

Hi,

can you please give us some hints how you integrated this with octorprint.

That you be very fine
Frank

@JDIacobbo
Copy link

UPDATE: Half a year later, a solution was made. https://github.com/aler9/rtsp-simple-server is what I'm currently using, and it works great, no problems yet.

Hi,

can you please give us some hints how you integrated this with octorprint.

That you be very fine Frank

I am also interested in how @bastel-123 was able to get this working with Octoprint.

@emaklav
Copy link

emaklav commented Oct 6, 2023

Hi! I'm start rtsp-simple-server localy on ubuntu server that connected to printer, so I have access to HTPP stream from rtsp-simple-server in my browser, but Octo didnt saw this stream ...

@xam464
Copy link

xam464 commented Jul 14, 2024

For me I need to use the following stream URL:

/usr/bin/vlc -vvv --intf=dummy --no-audio rtsp://username:password@CAMERAIP:CAMERAPORT/STREAM --sout "#transcode{vcodec=mjpg,venc=ffmpeg{strict=-2,threads=1},scale=1.0,acodec=none}:standard{access=http{mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a},mux=mpjpeg,dst=:10001}" --network-caching=50 --sout-mux-caching=50 --file-caching=50 --live-caching=50

I put it in the following service code:

[Unit]
Description=rtsp2mjpeg
After=network.target

[Service]
User=pi
Type=simple
ExecStart=/usr/bin/vlc -vvv --intf=dummy --no-audio rtsp://username:password@CAMERAIP:CAMERAPORT/STREAM --sout "#transcode{vcodec=mjpg,venc=ffmpeg{strict=-2,threads=1},scale=1.0,acodec=none}:standard{access=http{mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a},mux=mpjpeg,dst=:10001}" --network-caching=50 --sout-mux-caching=50 --file-caching=50 --live-caching=50

[Install]
WantedBy=multi-user.target

After that, you put the url in OctoPrint:

NOTE: I installed the following. No Idea you will need to...

sudo apt-get install libavcodec-extra
sudo apt-get install ffmpeg
sudo apt-get install libavcodec-dev

Credits to:https://github.com/Tom-Hirschberger/vlc-rtsp2mjpeg-wrapper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment