So I already have CCTV around the house and I was wondering just how cheaply I could add a few more cameras to my existing system using some webcams I have left over.

So the first thing you’ll need to do is set up a basic raspbian system, I wont cover the details of those steps as they are outlined in numerous other locations.

For reference this setup was built on a 512MB or a raspberry pi using raspbian image 2012-10-28-wheezy-raspbian.zip

The webcam I have available for testing is a Microsoft LifeCam 720p 30fps , the important thing to note is that it outputs as mjpg which means the raspberrt pi will not need to do any CPU intensive encoding.

1
2
phillips321@raspberrypi /etc/init.d $ lsusb
Bus 001 Device 004: ID 045e:075d Microsoft Corp. LifeCam Cinema

The first thing to do is install some required dependencies:

1
sudo apt-get install subversion libv4l-dev libjpeg8-dev imagemagick fswebcam

Then check that your webcam supports mjpg:

1
2
3
4
5
6
7
8
9
10
phillips321@raspberrypi ~ $ sudo fswebcam --verbose
--- Opening /dev/video0...
Trying source module v4l2...
/dev/video0 opened.
<SNIP>
src_v4l2_set_pix_format,541: Device offers the following V4L2 pixel formats:
src_v4l2_set_pix_format,554: 0: [0x56595559] 'YUYV' (YUV 4:2:2 (YUYV))
src_v4l2_set_pix_format,554: 1: [0x47504A4D] 'MJPG' (MJPEG)
Using palette MJPEG
<SNIP>

If it does that’s great, otherwise you’ll need to do a small bit of messing around to ensure the stream gets converted.

Now we need to install mjpg_streamer, to do this download the source via svn and then compile it:

1
2
3
4
5
svn checkout http://svn.code.sf.net/p/mjpg-streamer/code/ mjpg-streamer-code
cd mjpg-streamer-code/mjpg-streamer
make USE_LIBV4L2=true clean all
sudo make DESTDIR=/usr install
cd ../.. ; rm -rf mjpg-streamer-code

Now test that you can stream your webcam using:

1
mjpg_streamer -i "/usr/lib/input_uvc.so -d /dev/video0" -o "/usr/lib/output_http.so -p 80 -w /var/www/mjpg_streamer -n"

and access the stream using http://IP/?action=stream.

If you get bitched at about user per missing accessing V4L then you’ll need to add your user to the video group like so:

1
2
3
4
5
6
7
8
9
10
phillips321@raspberrypi ~ $ mjpg_streamer -i "/usr/lib/input_uvc.so -d /dev/video0" -o "/usr/lib/output_http.so -p 80 -w /var/www/mjpg_streamer -n"
MJPG Streamer Version: svn rev: 3:172
 i: Using V4L2 device.: /dev/video0
 i: Desired Resolution: 640 x 480
 i: Frames Per Second.: 5
 i: Format............: MJPEG
ERROR opening V4L interface: Permission denied
 Init v4L2 failed !! exit fatal
 i: init_VideoIn failed
phillips321@raspberrypi ~ $ sudo usermod -a -G video phillips321

Once you have it working you can adjust the webcam resolution and fps using:

1
2
[-r | --resolution ]...: the resolution of the video device, can be one of the following strings:  QSIF QCIF CGA QVGA CIF VGA SVGA XGA SXGA or a custom value like the following example: 640x480
[-f | --fps ]..........: frames per second

Now once you have your command to start the stream you need to set it to run at boot. To do this we’ll need to create an init script. Copy the following to a file called /etc/init.d/mjpg_streamer.sh and make is executable. Here’s mine which you can copy/paste and then change for your own use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/sh
# /etc/init.d/mjpg_streamer.sh
# v0.2 phillips321.co.uk
### BEGIN INIT INFO
# Provides:          mjpg_streamer.sh
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: mjpg_streamer for webcam
# Description:       Streams /dev/video0 to http://IP/?action=stream
### END INIT INFO

f_message(){
        echo "[+] $1"
}

# Carry out specific functions when asked to by the system
case "$1" in
        start)
                f_message "Starting mjpg_streamer"
                mjpg_streamer -b -i "/usr/lib/input_uvc.so -d /dev/video0" -o "/usr/lib/output_http.so -p 80 -w /var/www/mjpg_streamer -n"
                sleep 2
                f_message "mjpg_streamer started"
                ;;
        stop)
                f_message "Stopping mjpg_streamer..."
                killall mjpg_streamer
                f_message "mjpg_streamer stopped"
                ;;
        restart)
                f_message "Restarting daemon: mjpg_streamer"
                killall mjpg_streamer
                mjpg_streamer -b -i "/usr/lib/input_uvc.so -d /dev/video0" -o "/usr/lib/output_http.so -p 80 -w /var/www/mjpg_streamer -n"
                sleep 2
                f_message "Restarted daemon: mjpg_streamer"
                ;;
        status)
                pid=`ps -A | grep mjpg_streamer | grep -v "grep" | grep -v mjpg_streamer. | awk '{print $1}' | head -n 1`
                if [ -n "$pid" ];
                then
                        f_message "mjpg_streamer is running with pid ${pid}"
                        f_message "mjpg_streamer was started with the following command line"
                        cat /proc/${pid}/cmdline ; echo ""
                else
                        f_message "Could not find mjpg_streamer running"
                fi
                ;;
        *)
                f_message "Usage: $0 {start|stop|status|restart}"
                exit 1
                ;;
esac
exit 0

Once that is done you’ll need to instruct the command to be run at boot, you can do that using the following:

1
2
sudo chmod +x /etc/init.d/mjpg_streamer.sh
sudo update-rc.d mjpg_streamer.sh defaults

Now reboot and test that it’s working 🙂

Leave a Reply to Anonymous Cancel reply