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 :-)

28 Responses to RaspberryPi + Webcam = mjpg stream / CCTV?

  • Pingback: Use Zoneminder on the #RaspberryPi with the camera module | Raspberry PiPod

  • Wojtek says:

    I have problem with init script. I did everything what you wrote but when my Raspberry starting I received “[FAIL] startpar: service(s) returned failure: mjpg_streamer.sh … failed!”. Do you have any idea how to fix this ?

  • cctsao says:

    Great job!!

  • Pingback: Raspberry Pi + Wifi Dongle + Usb Webcam = remote control web bot | ZoneRobotics

  • Pingback: How to Stream video from your Raspberry Pi and control GPIO | ZoneRobotics

  • Damiano says:

    Hi, thanks a lot for the guide it was helpfull, I got my Microsoft LifeCam HD3000 to stream video over the net on the browser. The problem is that it is really slow! one pic every 15-20seconds! and even less than that! is this normal? How can I set resolution and fps? TIA

    • phillips321 says:

      Is it still slow when on a local network? The uplink bandwidth my be your bottleneck?

      • Damiano says:

        Well I noticed I’m not using a class10 SD card, and my network cable sometimes doesn’t work that well. By the way I googled a bit and found about I’ve got to set the resolution and framerate on the -i (input) parameter (didn’t know that, I tried to place -r and -f outside, as -i and -o are, but it didn’t work), it may help someone. By the way it’s amazing, I installed the ARM version of hamachi and I logged the rpi on a VPN, I streamed video over the internet to a friend in Ireland!

    • Prakash says:

      I’m using a LifeCam HD6000. Check the native resolution of the camera and adjust the -i parameter to match e.g. -i “/usr/lib/input_uvc.so -d /dev/video0 -r 1280×720″

  • sebog says:

    I had same issue from my Pi, most of commands works , but last command sux my Pi.
    web server didnt work, but camera run (light on).

    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
    Adding control for Pan (relative)
    UVCIOC_CTRL_ADD – Error: Inappropriate ioctl for device
    o: www-folder-path…: /var/www/mjpg_streamer/
    o: HTTP TCP port…..: 80
    o: username:password.: disabled
    o: commands……….: disabled

  • seccam says:

    Hi.
    I was curious whether you had any issues with this setup?
    There are lots of reports of people getting this working but then having issues:
    - the pictures seems to get screwed up with lines or other artifacts
    - the camera disconnects/disappears and does not connect again until the raspberry pi is cold-rebooted
    Most of these problems seem to be connected to the common USB issues (either the USB controller is too slow or it does not play nice with the ethernet controller)

    I very much would like to use a similar setup since I seem to be unable to get my lifecams working with 30+ ft USB cables, so I am looking for alternative solutions.

    Last but not least, I would love to know if you tried your setup with 2 webcams?

  • Tan Nguyen says:

    Hi Phillips,
    Thank you for your post, i have one question and i don’t know how to do now. What i need to do if my webcam don’t support MJPG?

  • Such a great guide!
    Bought rpi to make a RC car based drivable CCTV using my old logitech VGA camera, and mjpeg VGA@30 fps via WiFi dongle gives only 2.5-3.3% CPU load with ±290KBps upstream, so it will be even usable with 3G and there is a lot of CPU time left for rpi based web interface and driving motors without arduino.

  • mike says:

    I actually have an older Microsoft LifeCam that I had working on my old Debian box that I’m now going to move over to my Pi. Great guide.

  • Pingback: PiRider – What to install « Nutz&Boltz

  • Kmull says:

    An excellent guide that even covers starting a process at boot on the rpi. Invaluable!

  • ChieftainY2k says:

    Hello, thank you for this informative tutorial, saved a lot of my time! :-)
    I got my Pi working with Microsoft Studio and now it successfully streams 720p over Wifi (the USB dongle).
    What is really surprising (almost shocking to me :-) ) is that CPU load is as low as about 25-30% while streaming mjpeg 720p @ about 2 fps , nice! :-)

  • GPerks says:

    Yes – it tries to download the file!
    I’ll try VLC tomorrow – thanks.

  • Scriptmonkey says:

    Good effort mate, have to admit I didn’t turn on my computer all weekend. Meant to try and get on IRc and talk to you about this stuff :) did you progress it any further?

    • phillips321 says:

      So I’ve got it streaming. Which i can capture. Although im thinking using omxplayer so that i dont have to run x11 is the best bet. (omxplayer is the only media player for the pi at the mo that can directly use the gpu for rendering)
      Im pretty sure it’s all do able although i’m having difficulty figuring out who the main dev of omxplayer is?

      I want to directly play /dev/video0 using omxplayer but cant figure this out. You even booted your pi yet?

      • Nutz95 says:

        Thanks for your post => was very usefull to me.
        I tested with a Raspberry-pi 256Mb and it seems to be working fine.

        • Nutz95 says:

          I forgot to mention that if you are using your native webcam MJPEG resolution the CPU usage is very low!:
          - about 0.3% (with top command) for a 320×240 resolution @ 15 fps
          - about max 1.6% for a 640×480 resolution @15/25fps
          - about max 1.6% for a 1024×720 resolution @ 25/30fps
          - about 0.3% for 720p but i couldn’t have a smooth playback (1 or 2 fps only)
          I tried to use the -q command to change the mjpeg quality and the CPU usage was rising up to 25% for a 640×480 resolution this time because the application is reconverting the webcam stream in real time.
          I hope it gives people some ideas of what mjpg_Streamer is capable of

          • GPerks says:

            Thanks – got most of it working but I cannot get any browser to display the stream!
            Only can get 1 image on an iPad or different browsers try to save an ever increasing file!!

          • phillips321 says:

            Ok I had the same issue from my iPad and iphone. Have you tried accessing it froma normal browser on a pc?

Leave a Reply