Setup a webcam security system with Ubuntu Linux and Motion

May 17
2009
Snap from Office Security Cam

Snap from Office Security Cam

So, now that I’m in Morgantown – my home is too small to comfortably work on side gigs and personal projects – especially now that my family is getting bigger with the baby!  I’ve been using the office space I leased out more and more.  While exploring video conferencing with Matt last week, I had the thought “wouldn’t it be cool to have a security camera in the office?”.  So I did just that, and it’s actually quite easy for Ubuntu linux users.

What you need:
  • Ubuntu Linux ( I was using 8.04.1 at the time of installation )
  • one or more USB web cameras
What you can do:
  • Motion detection – record video/and or frames if there is motion.
  • Snapshot intervals – take time interval snapshots regardless of motion detection.
  • Live video IP stream in mjpeg format.
  • Specify recorded video to be saved in your choice mpeg, avi, flv, swf format.
  • When motion exists, have frames and videos draw a box around the specific motion for more obvious recognition of subtle movements ( this actually shows the shadow of the janitor near the door around 6 a.m. every morning – I wouldn’t have noticed otherwise! )
  • Easily send all data to a backup server in a variety of ways – I keep it simple by saving data to my Dropbox directory, a wonderful cross-platform data syncronization and sharing utility.
Steps:

1.  Plugin your webcam.
For me, the Logitech QuickCam® Pro 9000 worked right out of the box, and was only 105$.

2.  Install Motion – software motion detector, and turn it on.

sudo apt-get install motion
sudo motion

3. Configure Motion

Everything really works out of the box with this – but isn’t quite organized to my liking, and probably not yours either. Global configuration is located inside /etc/motion.conf ( You’ll notice there are multiple threadN.conf files in this directory – which can be used for custom configured individual cameras if you are setting up more than one ).

Note: Be sure to restart the Motion server everytime you make a configuration change.

sudo /etc/init.d/motion restart

Take a look at the files, they are well documented. Below are a few helpful configurations to get your data organized quicker:

#/etc/motion/motion.conf

# Locate and draw a box around the moving object.
locate on

# Draws the timestamp using same options as C function strftime(3)
text_right %Y-%m-%dn%T-%q

# Text is placed in lower left corner
text_left SECURITY CAMERA %t - Office

Organize the filesytem to save data by date, instead of all in one directory.

# File path for snapshots (jpeg or ppm) relative to target_dir
snapshot_filename %Y%m%d/camera-%t/snapshots/hour-%H/camera-%t-%v-%Y%m%d%H%M%S-snapshot

# File path for motion triggered images (jpeg or ppm) relative to target_dir
jpeg_filename %Y%m%d/camera-%t/motions/hour-%H/camera-%t-%v-%Y%m%d%H%M%S-%q-motion

# File path for motion triggered ffmpeg films (mpeg) relative to target_dir
movie_filename %Y%m%d/camera-%t/movies/hour-%H/camera-%t-%v-%Y%m%d%H%M%S-movie

# File path for timelapse mpegs relative to target_dir
timelapse_filename %Y%m%d/camera-%t/timelapses/hour-%H/camera-%t-%Y%m%d-timelapse

4.  (Optional)  Setup a backup solution

a. Easy solution, get and install Dropbox — instructions on the Dropbox site.  Then update your motion.conf to save to your Dropbox directory:

#/etc/motion/motion.conf
...
target_dir /path/to/dropbox/security_camera
...

b. A more granular solution is to take advantage of hooks configurable in motion.conf. Using these, you can create bash scripts to do anything your heart desires ( like trigger a silent alarm on motion detection outside business hours ). Available hooks: on_event_start, on_event_end, on_picture_save, on_motion_detected, on_movie_start, on_movie_end.

If you have wput installed, you can easily upload files to a remote backup server with these hooks:

#motion.conf
...
on_picture_save wput ftp://user@pass@server %f
...

However, this solution is somewhat less secure, as it uses FTP. In a future post I will detail how to secure this up using encrypted transfer and phrase free keys. ( Stay tuned! )

5. Live feed

This comes working out of the box with Motion. Check out your live stream in your web browser by navigating to: http://localhost:8081

That’s it! Webcam security made easy :-)

38 Responses to “Setup a webcam security system with Ubuntu Linux and Motion”

  1. mickos says:

    how to play mp3 on motion

  2. Raman B.V. says:

    How can I stop motion without shutting down my computer? It keeps running even after I exit the terminal.

  3. chriswpage says:

    I haven't used Motion in several months now, but I would imagine you can stop the daemon via: /etc/init.d/motion stop

  4. anaon says:

    @mickos
    did you try on_motion?

  5. Linux Fanatics says:

    [...] on Ubuntu?Red Hat Profits SoarSony Makes A Linux ComebackUbuntu Switches Back To GoogleObviously Motion as webcam security would be vastly more attractive with a GUI. Either GTK or QT. But even without a clean UI to work [...]

  6. Guest says:

    How can I enable the text_left command?
    I erase the ” ; '' the were before text_left, but it still not appear on the jpeg file

  7. Matthew says:

    Thanks, very helpful tutorial.

    The motion daemon dies quite silently if it can't write to its output directory. Daemon runs as user “motion”. Took a few minutes to figure that out.

  8. Khach La Vip says:

    program that supports sending captured images to an address email?

  9. Jayashleysmith says:

    Hi, How do i setup multible cams? I have one camera working just as i like. But have another cam plugged into /dev/video1, at the moment motion is only picking up /dev/video0 unless i change the video capture to either or. I can get either working one at a time. But not both at once. I also have no thred#.conf files?

    Regards

  10. Nikolay says:

    how to auto run motion, example script . . . . . please

  11. os says:

    did u test with multiple cams? i just tried with two different cams and it does not work. the second cam never seems to work (the second video device – /dev/videoX). both cams work alone, but not together.

  12. rblevin says:

    @Raman You need to terminate the process. In Terminal, enter ps -A and you will see motion listed with a process ID. Then type sudo kill -s SIGTERM ##### where #### = the process ID of motion.

  13. Jvrramirez says:

    Hi, I have easycap (/dev/easycap0 ) with channel, each one for each camera.
    how I can configure Motion ?
    best regards
    Javier

  14. Mark2741 says:

    Anyone manage to get this work with Ubuntu 10.10 and a Logitech Quickcam Pro 4000? The green led indicator on the webcam comes on when Motion is started, but no images get saved. Also, trying to stop the motion process always results in the terminal just hanging. I'm trying the daemon now with the same result : (

  15. Mark2741 says:

    I wound up getting it to work (it works great, too!) – my problem was I had an old Hauppage WinTV PCI card in my PC and wasn't working for any app. Even though the Logitech webcam was /etc/video1 and I changed the motion.conf file to reflect that, on a hunch I just pulled the WinTV card out of my PC, rebooted, and tried Motion and it works flawlessly now.

  16. Aasdf says:

    you have to use threads. Read a bit in the documentation about it. you'll need three files.

  17. eagleapex says:

    I just set this up. There's so many options! Thanks for the intro. Now I can find out what the sneaky neighbour is up to.

  18. Johannes Kingma says:

    the sample script is the init.d script. After configuring (updating the motion.conf file according to the suggestions above) you can run sudo /etc/rc.d/motion restart or /etc/init.d/motion restart to (re) start motion. Look for a motion process with ps aux | grep motion

  19. Johannes Kingma says:

    Btw. works fine on arch with kernel 2.6.38-ARCH x86_64

  20. Cantstopengland says:

    I think this site copied your post: http://xtremediary.blogspot.co…

  21. Guest says:

    thank you Chris! very useful and succinct explanation… just the right amt of info to get one started..

  22. Setup a webcam security system with Ubuntu and Motion | TurboLinux Blog says:

    [...] Here is a good tutorial show you how to Setup a webcam security system with Ubuntu and Motion: [...]

  23. Thalass T'Kynn says:

    It works quite nicely but i can't seem to get the live feed  over the internet. On the local network it works fine, but outside that it never connects. I've set my router to forward port 8081 to my laptop (just testing with the on-board webcam at the moment), but nothing. Must be a setting somewhere i've missed.

  24. Wayne says:

    Hi Guys

      iam running ubuntu 10.04 i have ordered a “wireless network dvr 4 channel” setup on ebay as you know it only has windows drivers.

    I do not find any linux drivers for this setup.

    It is a usb aplication that then connects either wireless or via cable to the not sure if i can call it router.

    how do i get pc to recognise this “router”?

  25. Popp0inlockin says:

    how do i quit in the terminal? i am trying everything and it won't stop

  26. Popp0inlockin says:

    how do i quit in the terminal? i am trying everything and it won't stop

  27. nikosm says:

    I i’ve managed to set it up, it works great, but now i want to stream audio to.
    Any ideas?

  28. ez says:

    yeah, started motion and saves lots of pics–Control C

    ^C[1] File of type 1 saved to: ./01-20111017081937-07.jpg
    [1] Thread exiting
    [1] Calling vid_close() from motion_cleanup
    [1] Closing video device /dev/video0
    [0] Motion terminating

  29. hanhdo says:

    @Raman: You can stop “Motion” by pressing CTRL+C.

  30. hanhdo says:

    @Raman: You can press Ctrl + C to stop motion

  31. jlevarre says:

    can someone submit an example of a motion.conf and thread1.conf
    thread2.conf for multiple camera operation.

    i have been trying for 3 months to get more than one camera working.

    thanks greatly

  32. Bird says:

    Many thanks, this was really useful.

    One quick note:
    on_picture_save wput ftp://user@pass@server %f

    should be:
    on_picture_save wput ftp://user:pass@server %f

    Some people have already mentioned, but to re-iterate motion just fails silently if you don’t have permission to write to the directory. The daemon runs as user motion so you will need to set permissions on your system correctly.

  33. kwak says:

    Hello,

    Can somebody please tell me where is the file in which I can setup motion start at startup of a system?
    I know that there is one file where you can write option
    start on boot=yes
    But I forget where is it ;)
    The script was starting but failed after few hours (normaly three or four ) Did anyone maybe have the same problem?

    Perhaps there is other option to start motion on boot up? Many thanks for help!!

  34. Jeff Kline says:

    Interesting piece of work! I’ve got it running in Ubuntu 10.10LTS so far without a hitch and it’s seeing two cameras from two video cards. Motion detection appears a bit strange though as though it only works on the one camera and not the other. But this is further investigation.

  35. Peter Libman says:

    Hi, Do you think I’ll have any problems running this on a raspberry pi with the new cam board? Looks like just what I want, take photo on a movement. I’m planning to build a gizmo to catch people dumping rubbish!

  36. Motion – Motion Detection Software On Ubuntu | Danny Tsang says:

    [...] Setup a webcam security system with Ubuntu Linux and Motion [...]

Leave a Reply

Visit Other Sites!

Find me on other sites...

Archives

All entries, chronologically...

Pages List

General info about this site...