(Tutorial) Adding multiple USB webcams to Octoprint, using new Webcam stack

I know a lot of people have been asking for this, and i have not seen a full post about how to do this, but i finally got it working after about ~2 hours. Mostly with only using the built in options for octoprint.

(if you want to quickly switch, you will need to download a single plugin "MultiCam", but it does work without it)

First i need to mention this wouldnt be possible without the help of @Charlie_Powell, @foosel, or @Wit4r7 .

As a quick pre requisite,

Running mulitple USB cameras off of a single Pi, puts a lot of stress on the internal voltage regulator for the USB ports. I highly recommend that you only use multiple cameras when they are plugged into a compatible, externally powered, USB hub.

Additionally, Each camera can add anywhere from 3-6% background CPU usage when accessing the web UI. I did have this working on my Raspi 3 B+, but i mainly use a Raspi 4. YMMV.

Most USB cameras consume around 600ma of current draw, but this is just an estimate.

Model Max power output of USB ports
Raspberry Pi Zero, 1 500mA per port1
Raspberry Pi 2, 3, 4 1200mA total across all ports
Raspberry Pi 5 600mA if using a 3A supply, 1600mA if using a 5A supply

Also, you shouldnt really be powering your printer with the USB cable either, you can follow the tutorial outlined here by @PrintedWeezl


So to start off, you need the newest build of OctoPi (New Camera Stack)

image

Theres a great introduction on this by @foosel here

So lets get started.

  1. Flash OctoPi (new camera stack) using Raspberry Pi Imager.
    (I recommend setting the advanced options by pressing CTRL+SHIFT+X, and setting the username and password for SSH under the General tab, as we will be using it later. otherwise the default is pi/raspberry)

  1. Once that is done, Setup Octoprint as you normally would, or restore a backup.

Were going to remove the default camera, and then add our own cameras. That way we can configure our camera's settings individually.

Lucky for us @foosel provided us with instructions.

The configuration shipped on the image will support running either a libcamera device or a USB camera device. To support multiple cameras (one libcamera and/or one or more USB cameras), you'll need to add configuration files to tell the camera setup what to start up.

The image ships with some helper scripts to assist in the initial multicam setup. You'll need to log into your Pi via SSH or with a display and keyboard to use these.

  1. With all cameras unplugged, Lets SSH into your Pi.

  1. Lets start by running sudo remove-usb-camera default

  1. Plug in your first USB camera.

  2. Then run sudo add-usb-camera <camera name> Replacing <camera name> with the name of your first camera.

  1. Unplug your first camera, and plug in your second one.

  2. run sudo add-usb-camera <camera name> Replacing <camera name> with the name of your second camera.

  3. rinse and repeat for as many as you like.

Now we need to configure the settings for each camera individually, by navigating to the camera streamer config folder.

  1. Lets type cd /etc/camera-streamer.conf.d

We need to edit all files in this directory.
For camera 1, we need to change the port to 8080 (octoprints default camera),
and for camera 2, we need to change the port to 8081,
and for libcamera we need to change the port to 8082.

We will be editing the usb-<camera name>.conf files using nano.
Replace <camera name> with the names of your cameras.

In my case, its by running

  1. sudo nano usb-logitech.conf Changing the port to 8080.
    (I also Chnaged the format here to MJPEG because Octoprint was not displaying my second camera with them both being YUYV, as mentioned here by @Wit4r7)
    and
  2. sudo nano usb-razer.conf changing the port to 8081

make sure to change the libcamera to a higher port than your highest camera.

  1. sudo nano libcamera.conf changing the port to 8082

You can find a description on the additonal camera options here


Now that we have all the ports configured, we need to forward the second camera, and any additional cameras to the haproxy service, so octoprint can see it.

First lets backup the haproxy configuration
(Just in case it goes wrong and you have to revert later, you can easily copy it back)

  1. run sudo cp /etc/haproxy/haproxy.cfg ~/haproxy.cfg.bak

Open the haproxy configuration.

  1. sudo nano /etc/haproxy/haproxy.cfg

Add the required sections to this file

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/ }

(add any additional lines for more cameras)

+ use_backend webcam2 if { path_beg /webcam2/ }
+ use_backend webcam3 if { path_beg /webcam3/ }
+ use_backend webcam4 if { path_beg /webcam4/ }
+ use_backend webcam5 if { path_beg /webcam5/ }
  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 backend webcam2 (again, no pluses):

+backend webcam2
+       http-request replace-path ^([^\ :]*)\ /webcam2/(.*)     \1\ /\2
+       server webcam2  127.0.0.1:8081
+       errorfile 503 /etc/haproxy/errors/503-no-webcam.http

(add any additional sections for more cameras)

+backend webcam2
+       http-request replace-path ^([^\ :]*)\ /webcam2/(.*)     \1\ /\2
+       server webcam2  127.0.0.1:8081
+       errorfile 503 /etc/haproxy/errors/503-no-webcam.http
+backend webcam3
+       http-request replace-path ^([^\ :]*)\ /webcam3/(.*)     \1\ /\2
+       server webcam2  127.0.0.1:8083
+       errorfile 503 /etc/haproxy/errors/503-no-webcam.http
+backend webcam4
+       http-request replace-path ^([^\ :]*)\ /webcam4/(.*)     \1\ /\2
+       server webcam2  127.0.0.1:8084
+       errorfile 503 /etc/haproxy/errors/503-no-webcam.http
+backend webcam5
+       http-request replace-path ^([^\ :]*)\ /webcam5/(.*)     \1\ /\2
+       server webcam2  127.0.0.1:8085
+       errorfile 503 /etc/haproxy/errors/503-no-webcam.http
  1. ctrl+o, then Enter, then ctrl+x

Lets validate the configuration

Run the command

  1. haproxy -c -V -f /etc/haproxy/haproxy.cfg

It should reply with 'Configuration file is valid', or with the errors that need fixing.

If all is good, restart haproxy using

  1. sudo service haproxy restart.

  2. Test and use

You can test if this works by going to /webcam/?action=stream or /webcam2/?action=stream, within the classic webcam plugin, and hopefully see your webcams coming up! If not, you may need to reboot your Pi.

Webcam 1

Webcam 2


  1. Switching webcams - Download the plugin MultiCam from the repository.

  1. Add your streams to MultiCam!
/webcam/?action=stream
/webcam2/?action=stream

  1. (Optional) Disable classic Webcam plugin from the plugin manager.

  1. Profit! You can now choose which webcam you want, Happy Printing!!
3 Likes

Looks like a great guide! Thanks @Kold for putting it together.

1 Like

@Charlie_Powell
Any chance you might be willing to add a link to this tutorial in your Guide here?
Setting up multiple webcams in OctoPi the right way - Get Help / Guides - OctoPrint Community Forum

I've ran through this guide on a new installation, and can confirm it works. It took a lot of work and searching to find this tutorial, and when you search for help on this, your older guide seems to come up first, so it might help some folks out and prevent some posts asking for assistance.

@Kold This is a fantastic guide, and has fixed the only issue I've had with OctoPrint. Thank you so much for taking the time to write this up, especially with the level of detail provided!

1 Like

Thank you for the kind words! I appreciate it!

Thank you! I'm glad you found it useful!