Setting up multiple webcams in OctoPi the right way

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.

:warning: 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/.

  1. 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

  1. 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 in index0 rather than index1 are preferred, as that is usually the device with video streaming.

    :spiral_notepad: Note: Not sure which device is which? You can run lsusb to match a name to an ID.

  2. Then edit /boot/octopi.txt:

    sudo nano /boot/octopi.txt
    
  3. Set camera="usb" at the top, and the camera_usb_options line to indicate the device:

    camera_usb_options="-r 640x480 -f 10 -d /dev/v4l/by-id/<device long id>"
    

    :spiral_notepad: Note: Using a Raspberry Pi camera? Set 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. This no longer applies for OctoPi 1.0.0. Raspberry Pi cameras are treated the same as USB cameras, 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.

  1. Find the long ID of the second camera, as you did for the first one.

  2. Editing /boot/octopi.conf.d/webcam2.txt:

    sudo nano /boot/octopi.conf.d/webcam2.txt
    
  3. As above again, set camera="usb" and set camera_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>"
    
  4. 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 :slightly_smiling_face:.

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 :slightly_smiling_face:.

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

13 Likes

If you have a raspberry pi camera and a usb camera in the octopi.txt file all you need to change is to set
camera="raspi"
and in the webcam2.txt set
camera="usb"
and the camera_http_options="-n -p 8081"
you do not need to worry about running ls /dev/v4l/by-id and adding the -d /dev/v4l/by-id/<device long id> part to the camera_usb_options

4 Likes

Correct, this is more tailored towards 2 USB cameras. It's more simple with 1 RPi camera and 1 USB camera, since it is easy to tell which is which. I'll try and adjust the notes to keep it concise but point this out.

1 Like

Nice guide this will help alot of people who are not sure how to setup multiple camera feeds

1 Like

hi thanks this is brilliant , it works a treat , just trying to use it with multicam in octoprint ? not sure if thats possible ?

sure
just put the url with the port you assigned into the second cam field

Hi here is what I put

http// :8081 but this did not work ..
Thanks

It should have read
Http://pi IP address :8081

yep :slight_smile:

Yea I put that but says webcam stream error,guessing I am doing something wrong LOL but can’t figure it out ,if I put that into a browser I can see the camera feed from camera but in octoprint I just get that error

does http://IP-OF-THE-PI:8081/?action=stream work?

1 Like

Hi
Will try that in morn ,thanks for your help

1 Like

.that worked A treat thank so much,

1 Like

Thank you!

This really works, simple and easy.
Looking forward to that Haproxy guide ^.^

@PrintedWeezl , Yes. it show up very nice.

3 Likes

Hello @Charlie_Powell,

I am searching for a way to connect two cameras to my octoprint docker container. Unfortunately there is no Octoprint.txt-file. There is just a config.yaml, but I wasn't able to make this work.

Can you give me any advices on this topic?

Thanks!

Hi, this guide only works on OctoPi installs since it takes advantage of the code for multiple cameras that already exists.

If you are already using docker, why not create another dedicated streamer container? There's loads out there that use mjpg streamer, or some of the other streamers. Using containers is designed to split up tasks rather than one container to do everything (which would be a whole OS I guess.... :wink:)

Hi there,

Thank you for that tip. I was wondering to find something easy for set up my second usb camera but seems to do not work...
I have strictly followed your instructions but the second camera seems to do some issues.

On the Rpi3b i have one picam that works well. I wanted to add some more with an usb camera.
when i try to go to http://mypi:8080 there is blank page but i got the stream on the 8081 withe the picam ?!

Any idea for get it out of there ?

By default on OctoPi, the first instance of mjpg streamer (on port 8080) is only available through haproxy, so under /webcam/ there.

What i understood is to go http://ipofpi:8080 for see the picam and going further to http://ipofpi:8081 for get the stream from my webcam ...
I just roll back the whole thing and try to setup again ... passed my whole day on it ...

This setup does not change the original camera you had setup. So don't change the URL it is using. Nowhere in this guide does it say use :8080 for the first camera, just for the second. I suppose I could make it clearer that the existing camera streaming does not change.

Use http://ipofpi/webcam

Correct