PiEye - webcam streaming in M-JPG format with Raspberry Pi
- Created on 24 September 2012
- , Written by Calin Crisan
With Raspberry Pi and a cheap web-camera connected via USB you can do video streaming in just a few simple steps. This solution is based on the mjpg-streamer server application. This solution is very simple as it uses existing streaming technologies. Everything happnes over HTTP, a standard protocol found everywere and very accesible today.
Practicly, a mini-webserver that runs on Raspberry Pi, serves video as a M-JPG (Motion Jpeg, more info here) file. M-JPG is actually a set of JPG images as frames of a movie. M-JPG's advantage is that it requires no plugins or browser extension to play. A simple HTML IMG tag, with right dimmensions and source attribute set to point to the served M-JPG woudl be enough to show the video in a standard web page.
The whole solution is based on mjpg-streamer server application (you can find it here). The code was written by Tom Stöveken and has GPLv2 licence. I have compiled and prepared a version of this code for Raspberry PI. See below required steps to get a simple & efficient video streaming in a fiew minutes:
- Make sure you have an updated version of Raspberry PI's OS.
- Install libv4l-0 package, available in Raspbian: sudo aptitude install libv4l-0.
- Connect the web camera to USB. The web camera must be Linux compatible; to check this, make sure /dev/video0 file is available on Raspberry PI, else the camera does not have a Linux driver or required extra configuration to work (this issue is not discussed here).
- Download mjpg-streamer-rpi.tar.gz archive on Raspberry PI and extract it. Destination folder is not relevant. You don't need root access if you are using the default pi user. Go to mjpg-streamer folder, where you extracted the tar.gz file.
- Open mjpg-streamer.sh file; this is a simple bash script to control the mini-webserver. The header contains some writable parameters, as refresh rate or resolution. The default settings should work in most situations.
- Start the server with ./mjpg-streamer.sh start command in the current folder.
- Runt your prefered web browser and go to http://raspberrypi:8080/?action=stream (where raspberrypi is it's IP address). You should see the image from the webcam. Current version has some issues with Chrome, just use Firefox if the image is not refreshed.
- If the system doesn't work, see the mjpg-streamer.log file for debug info.
See below required commands for not-Linux geeks (install,download,rename,extract,configure,run):
sudo aptitude install libv4l-0 ls /dev/video0 wget http://www.bobtech.ro/get?download=36:mjpg-streamer-rpi mv get\?download\=36\:mjpg-streamer-rpi mjpg-streamer-rpi.tar.gz tar -zxvf mjpg-streamer-rpi.tar.gz cd mjpg-streamer sudo nano mjpg-streamer.sh ./mjpg-streamer.sh start
Change video parameters
To change video parameters you have to install v4l2-ctl:
sudo apt-get install v4l-utils v4l2-ctl --list-ctrls v4l2-ctl --set-ctrl brightness=200 v4l2-ctl --set-ctrl saturation=32
I have setup a webcam for live video stream from my aquarium at 5fps. Plants are moved by the water pump jet and a fish or shrimp can be seen from time to time.
Using the M-JPG format you can embedd live video in any web page with just a simple img tag:
<img src="http://raspberrypi:8080/?action=stream" />
- automatic restart after 3 seconds if "error grabing frames"
- added YUV option for webcams without MJPG protocol (default disabled; if you activate it, Raspberry Pi is used to process the MJPG files instead of webcam's CPU)
- 6 pcs Rasbora (Trigonostigma heteromorpha)
- bugfix for frequent crash (now should be online permanently)