There's a lot of tutorials and youtube videos around about 'How to setup multicam on OctoPi', and in most of these they involve copying the service files and webcamd, editing some paths and ending up duplicating all the code and manually editing it. However, since OctoPi 0.17 support for configuring multiple cameras is available out of the box, which is much simpler to get going and a lot less likely to cause issues.
Multicam setup
You can setup as many cameras as you like using this method, just changing the parameters each time. However, watch your CPU usage as streaming can be quite resource-intensive.
Prerequisites
You need:
- OctoPi 0.17 or later
Warning: this is tested with 0.18, it should work with 0.17 but there may be some incompatibilities. - SSH access or some alternative way of accessing the command line
- Of course, more than one camera!
Make a backup first. This guide touches several configuration files with more than one point to go wrong. Don't take risks, make a backup.
Warning: this guide is not compatible with Obico (Formerly 'The Spaghetti Detective') premium webcam streaming. You'll need to switch the plugin into 'Compatibility mode' for this guide to work.
Not compatible with the new webcam stack using camera-streamer
Coming soon: an updated guide for setting up multiple webcams with the new stack.
1. Creating the configuration files
OctoPi supports an infinite number of octopi.txt
-style configuration files. They can be placed in /boot/octopi.conf.d/
.
-
Create the second one from the terminal:
cd /boot sudo mkdir octopi.conf.d sudo cp octopi.txt octopi.conf.d/webcam2.txt
Now you should have two identical files, one at
/boot/octopi.txt
and one at
/boot/octopi.conf.d/webcam2.txt
.
2. Editing the original configuration file for a specific camera
-
Find out the path to the camera by ID. This makes it much easier to stop
/dev/video0
and/dev/video1
from switching on you.Run
ls /dev/v4l/by-id
, then save somewhere the long name of your camera. For example:pi@ender5pro:~ $ ls /dev/v4l/by-id/ usb-046d_0825_88C56B60-video-index0 usb-046d_0825_88C56B60-video-index1
I'll use the first one,
usb-046d_0825_88C56B60-video-index0
. Camera devices ending inindex0
rather thanindex1
are preferred, as that is usually the device with video streaming.Note: Not sure which device is which? You can run
lsusb
to match a name to an ID. -
Then edit
/boot/octopi.txt
:sudo nano /boot/octopi.txt
-
Set
camera="usb"
at the top, and thecamera_usb_options
line to indicate the device:camera_usb_options="-r 640x480 -f 10 -d /dev/v4l/by-id/<device long id>"
Note: Using a Raspberry Pi camera?
SetThis no longer applies for OctoPi 1.0.0. Raspberry Pi cameras are treated the same as USB cameras,camera="raspi"
at the top of the file, and you can ignore referencing the RPi cam by device ID. You'll still need to do this for the USB camera.camera="raspi"
is no longer supported.
3. Editing the second configuration file
There's two things that need adjusting here: the port mjpg_streamer will be running under, and the device again.
-
Find the long ID of the second camera, as you did for the first one.
-
Editing
/boot/octopi.conf.d/webcam2.txt
:sudo nano /boot/octopi.conf.d/webcam2.txt
-
As above again, set
camera="usb"
and setcamera_usb_options
but this time using the ID for the second camera:camera_usb_options="-r 640x480 -f 10 -d /dev/v4l/by-id/<device long id>"
-
Adjust the port, by editing
camera_http_options
:camera_http_options="-n -p 8081"
4. Test it works
Restart webcamd using sudo service webcamd restart
, and try and find your second camera under http://<your-ip>:8081
. If it doesn't work, check in the webcam log, /var/webcamd.log
for details.
If it works, you can use the stream & snapshot URLs in OctoPrint:
- Stream:
http://<your-ip>:8081/?action=stream
- Snapshot:
http://<your-ip>:8081/?action=snapshot
OPTIONAL: Haproxy configuration.
The first webcam can be accessed from /webcam/?action=stream
. This can also be done for the second webcam as well, so that you can use https connections, some remote access services and don't need to worry about the IP address changing.
This part of the guide is optional, and it can easily go wrong breaking access to the entire server. So, make sure you get it right .
1. Backup the haproxy configuration
sudo cp /etc/haproxy/haproxy.cfg ~/haproxy.cfg.bak
(Just in case it goes wrong and you have to revert later, you can easily copy it back)
2. Open the haproxy configuration.
sudo nano /etc/haproxy/haproxy.cfg
3. Add the required sections
Under the frontend public
section, add the following line in (without the + symbol!):
use_backend webcam if { path_beg /webcam/ }
+ use_backend webcam2 if { path_beg /webcam2/ }
use_backend webcam_hls if { path_beg /hls/ }
use_backend webcam_hls if { path_beg /jpeg/ }
At the end of the file, add a new section (again, no pluses):
+ backend webcam2
+ reqrep ^([^\ :]*)\ /webcam2/(.*) \1\ /\2
+ server webcam2 127.0.0.1:8081
+ errorfile 503 /etc/haproxy/errors/503-no-webcam.http
4. Validate the configuration
Run the command haproxy -c -V -f /etc/haproxy/haproxy.cfg
to test the configuration. It should reply with 'Configuration file is valid', or the errors that need fixing.
If all is good, restart haproxy using sudo service haproxy restart
.
5. Test and URLs to use
You can test this works by going to /webcam2/?action=stream
, and hopefully see the same image you saw earlier at port 8081.
In OctoPrint, or any multicam plugins etc. you can now use this relative URL /webcam2/?action=stream
, and it should work just fine .
YouTube Video Demonstration
Chris Riley did a video demonstrating the steps of this post: OctoPi - Multiple Camera Setup - Raspberry Pi Camera - Chris's Basement - 2023 - YouTube