![](https://code.trev.id.au/wp-content/uploads/2024/05/DALL·E-2024-05-27-19.38.45-A-Raspberry-Pi-4-setup-showing-a-webcam-connected-via-USB-streaming-live-video-to-a-web-browser-using-MJPG-Streamer.-The-Raspberry-Pi-is-shown-with-i.webp)
WebCam on Raspberry Pi 4
Setting up a WebCam using USB on a Raspberry Pi 4 turns out to be quite easy. This setup that involves building the application MJPG Streamer will stream the webcam to a web browser that you can access from anywhere you want it to be accessed from. The main homepage for this application is at https://github.com/jacksonliam/mjpg-streamer
First update/upgrade the RPi using
sudo apt update; sudo apt upgrade -y
Now install a set of libraries and applications that allow us to build the application:
sudo apt -y install libjpeg62-turbo-dev imagemagick libv4l-dev libjpeg-dev cmake
Change Directory into your home directory where you would build applications.
cd <enter>
Download the source code from github
wget https://github.com/jacksonliam/mjpg-streamer/archive/master.zip
Unzip the zip file and change into the created directory
unzip master.zip
cd mjpg-streamer-master/mjpg-streamer-experimental/
Now build the application and install it.
make clean all
sudo make install
Make it work
Check that your webcam is being seen by the OS
ls -al /dev/ | grep video
You should see some video0, video1, video2. If you don’t see single digit video, there is a problem to fix.
v4l2-ctl --list-devices
You should see /dev/video0 and maybe more /dev/video* with single digits which is good. No single digit /dev/video is a problem to fix.
Now create a bash script file somehwere in the path. I did mone using:
sudo nano /usr/local/sbin/stream-camera.sh
Paste the following into the script:
#!/bin/bash
mjpg_streamer -i "/usr/local/lib/mjpg-streamer/input_uvc.so -y -d /dev/video0 -n -f 6 -r 640x480" -o "/usr/local/lib/mjpg-streamer/output_http.so -p 8084 -w /usr/local/share/mjpg-streamer/www"
To start the application, run the bash script and use your browser to go to http://<ip address of rpi>:8084
Leave a Reply
You must be logged in to post a comment.