Raspberry Pi Webcam Server for OBS Streaming

by jacobalze in Circuits > Raspberry Pi

565 Views, 8 Favorites, 0 Comments

Raspberry Pi Webcam Server for OBS Streaming

IMG_4004.JPEG

I was looking for a solution to live stream with a camera on a Raspberry Pi to OBS Studio. I was trying to install OBS directly on the Pi, but I learned that OBS is not directly supported by Raspberry Pi. I found a tutorial here to set up the Pi to steam the cameras to a webserver, then I would pick up the webserver on my PC running OBS. I will use the tutorial I linked above for setting up the server then I will show you how to get it working with OBS Studio.


*Note - If you would like this project to just have the webcam servers as security cameras or something, then skip any of the parts dealing with OBS Studio.

Supplies

This project requires just some basic components for Raspberry Pi:

  1. Raspberry Pi (I'm using a 3B but any standard Pi model will work)
  2. Micro SD Card (For Raspberry Pi OS)
  3. As Many Cameras as you want that can fit on the Pi:
  4. Raspberry Pi Camera Module AND/OR
  5. USB Camera
  6. Some way to interact with your Pi for setup:
  7. USB Keyboard and Mouse OR
  8. Touchscreen
  9. Case and Power Supply
  10. Windows PC Or Mac

Installing Raspberry Pi OS and OBS Studio

  1. If you haven't already, install the Raspberry Pi Imager and flash the Raspberry Pi OS onto the Micro Sd Card.
  2. After setting up, connect your Raspberry Pi to the same network as your PC or Mac.
  3. Confirm your Pi is up to date:
sudo apt update
sudo apt upgrade
  1. Now on your PC or Mac, install OBS Studio through any of these ways:
  2. OBS Studio Website (PC or Mac)
  3. Windows App Store (PC only)
  4. Open the app and configure it how you would like.

Setting Up the Webcam Server on the Raspberry Pi

  1. Open a terminal on your Raspberry Pi.
  2. Use the following command to install all needed packages:
sudo apt install autoconf automake build-essential pkgconf libtool git libzip-dev libjpeg-dev gettext libmicrohttpd-dev libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev default-libmysqlclient-dev libpq-dev libsqlite3-dev libwebp-dev libcamera-v4l2
  1. Run the following commands to install Motion from GitHub:
sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.6.0/$(lsb_release -cs)_motion_4.6.0-1_$(dpkg --print-architecture).deb

sudo dpkg -i $(lsb_release -cs)_motion_4.6.0-1_$(dpkg --print-architecture).deb

Configuring Motion

listdevicesoutput.png
  1. Before configuring, check your camera sources by running the following command:
v4l2-ctl --list-devices
  1. Make note of the one(s) you plan to use. Note the first line under your video source (usually something like /dev/video0)
  2. USB Cams are listed under the device name. If you are unsure of the device name, hook the camera up to your PC or Mac to get the name.
  3. Pi cam should be listed under something like unicam.
  4. See the image attached for example.
  5. You will have to have a separate configuration for each camera. Go to Step 4 to find instructions for doing this.

*Note - If using a Raspberry Pi camera, additional configuration needs to be done. Go to Step 5 for instructions.

  1. To open the configuration file for Motion, run the following command:
sudo nano /etc/motion/motion.conf
  1. Find the following lines and ensure they are set to the values below:
  2. daemon off
  3. stream_localhost off
  4. picture_output off
  5. movie_output off
  6. stream_maxrate 100
  7. video_device /dev/video0 (change to respective video device)
  8. Optional (Don’t include the text after the #):
  9. framerate 100 # Changing this option will allow for 100 frames to be captured per second, allowing for smoother video. Default is 50
  10. width 640 # This line changes the width of the image displayed. Default is 640
  11. height 480 # This option changes the height of the image displayed. Default is 480
  12. text_left YOUR TEXT and text_right YOUR TEXT # These will add text to the bottom corners. change YOUR TEXT to whatever you want or remove the YOUR TEXT part. Defaults are Camera1 and Date and Time
  13. Altering these options can drastically affect the performance. You may need to tinker to get the best results.
  14. IF USING MULTIPLE CAMERAS:
  15. stream_port 8081 # This is the port your camera feed will go on. Take note of this number because you will need it to access the stream. If adding additional cameras, change it to 8082, 8083, etc..
  16. Once done, save and exit by pressing CTRL + X then Y, then ENTER.
  17. Enable the motion service to start at boot:
sudo systemctl enable motion
  1. Start Motion:
sudo systemctl start motion
  1. If you need to stop Motion, run the following line:
sudo systemctl stop motion
  1. Now, time to check if your server is running.
  2. First, Get the IP address of your Raspberry Pi:
hostname -I
  1. Navigate to the following website (replace placeholders with your data):
YOUR_PI_IP_ADRESS:YOUR_PORT_NUMBER
  1. Example:
192.168.1.121:8081
  1. You should now see your live camera feed!

Adding Additional Cameras

  1. First, duplicate the configuration file:
sudo cp /etc/motion/motion.conf /etc/motion/motion_cam2.conf
  1. (You can change cam2 to whatever you want just make sure you remember it and use a new one for each additional camera)
  2. Next, duplicate the service file:
sudo cp /lib/systemd/system/motion.service /lib/systemd/system/motion_cam2.service
  1. Open the new service file:
sudo nano /lib/systemd/system/motion_cam2.service
  1. Edit the following line to use the new configuration file:
ExecStart=/usr/bin/motion -c /etc/motion/motion_cam2.conf
  1. Save and exit by pressing CTRL + X then Y, then ENTER.
  2. Reload systemd to recognize the new service:
sudo systemctl daemon-reload
  1. Now you have another camera setup! Now repeat Step 3 with the new configuration file, and make sure that replace every command that has motion to motion_cam2 or whatever you called your configuration file, and make sure you change the stream port.
  2. If using a Pi cam, follow Step 5 to configure (unless your first camera was also a Pi cam, the configuration would have copied over)

Additional Steps for Using a Raspberry Pi Camera

  1. Since the Raspberry Pi camera relies on a library called libcamera we will need to edit the service file. To do this, enter the following line:
sudo nano /lib/systemd/system/motion.service
  1. In this file, find the line that contains the following text:
ExecStart=/usr/bin/motion
  1. Or if you added another camera it might look like this:
ExecStart=/usr/bin/motion -c /etc/motion/motion_cam2.conf
  1. Update the line to the following text:
ExecStart=/usr/bin/libcamerify /usr/bin/motion
  1. Or if you have the multi camera setup:
ExecStart=/usr/bin/libcamerify /usr/bin/motion -c /etc/motion/motion_cam2.conf
  1. Save and exit by pressing CTRL + X then Y, then ENTER.
  2. Reload the systemctl daemon and restart the Motion service:
sudo systemctl daemon-reload
sudo systemctl restart motion

Configuring OBS Studio

  1. Now we will configure OBS Studio to use these Cameras.
  2. At the bottom of the screen there will be a whole bunch of different controls. Find the one named "Sources"
  3. Hit the + Button under sources.
  4. Select Browser.
  5. Select Create New and type in an identifier for your camera (Examples: Front Camera, Camera 1, USB Camera)
  6. Under URL, Enter the source of your camera (YOUR_PI_IP_ADRESS:YOUR_PORT_NUMBER)
  7. Scroll down and check the box Refresh browser when scene becomes active.
  8. Hit OK.
  9. Adjust The Camera video how you would like it on your screen.

Repeat these steps for multiple cameras.

All Done!

Congratulations! You now have an Raspberry Pi Camera Setup with OBS Studio! You are now able to stream with all of your cameras connected to the Raspberry Pi.

If you are having issues with basic camera configuration, view the tutorial I used to make this here.

If you are having issues when adding multiple cameras or with OBS Studio or are confused with the instructions, let me know in the comments below and I will try to help you.

Thanks for reading!