Aug 312010

sorveglianzaIn these days all the new netbooks and most laptops have built-in webcam, or at least add one external costs very little, apart from the traditional use in video calls you can use our Linux box to create a monitoring device, it can be used to monitor your child while you are at another PC, or your pet while you are at work, or have a home-made video surveillance system for your  properties.

To accomplish this we will use the software Motion, the software is present in many distributions, I will give commands using my Ubuntu 10.04.

Feature that made me choose this software is the ability to remain “asleep” until it detects a noise or movement, and then activates and records, plus the ability to define custom actions when it start (send an email, an sms , Etc.).

The installation is fairly easy (just become root):

aptitude install motion

Now open the configuration file with an editor, I will use gedit

gedit /etc/motion/motion.conf

This will open a file very long and full of options. Some of them are best left as is, unless you know exactly what you’re doing. I’ll show you how to tweak some options to save images in a directory more convenient to consult (default is /tmp), how to configure some parameters of recording and, finally, how to use the web interface with username and password protecting it (do not enable the default !).

First activate motion in daemon mode, so it can be activated regardless of our session.

# Start in daemon (background) mode and release terminal (default: off)
daemon on

Change the option that indicates where to write the pid of the daemon, put it in /home/username/ so you have the proper permissions.

# File to store the process ID, also called pid file. (default: not defined)
process_id_file /HOME/USERname/

Change now the settings for the image resolution, i’m using 640×480

# Image width (pixels). Valid range: Camera dependent, default: 352
width 640
# Image height (pixels). Valid range: Camera dependent, default: 288
height 480

And now we decide how many images taken every second, i’ll take 4 (may vary from 2 to 100).

# Maximum number of frames to be captured per second.
# Valid range: 2-100. Default: 100 (almost no limit).
framerate 5

After that we can adjust the brightness, contrast and saturation to be used in the acquisition of images. These options have a reason to exist only if the camera has no auto-detection, as with the cheaper camera, I try to let the default values.

# Let motion regulate the brightness of a video device (default: off).
# The auto_brightness feature uses the brightness option as its target value.
# If brightness is zero auto_brightness will adjust to average brightness value 128.
# Only recommended for cameras without auto brightness
auto_brightness off

# Set the initial brightness of a video device.
# If auto_brightness is enabled, this value defines the average brightness level
# which Motion will try and adjust to.
# Valid range 0-255, default 0 = disabled
brightness 0

# Set the contrast of a video device.
# Valid range 0-255, default 0 = disabled
contrast 0

# Set the saturation of a video device.
# Valid range 0-255, default 0 = disabled
saturation 0

leave a bit of options as they are for the moment, and change the codec to use for the films that we’ll produce, I do not like the default (flash) and so I’ve put mpeg4

# Codec to used by ffmpeg for the video compression.
# Timelapse mpegs are always made in mpeg1 format independent from this option.
# Supported formats are: mpeg1 (ffmpeg-0.4.8 only), mpeg4 (default), and msmpeg4.
# mpeg1 – gives you files with extension .mpg
# mpeg4 or msmpeg4 – gives you files with extension .avi
# msmpeg4 is recommended for use with Windows Media Player because
# it requires no installation of codec on the Windows client.
# swf – gives you a flash film with extension .swf
# flv – gives you a flash video with extension .flv
# ffv1 – FF video codec 1 for Lossless Encoding ( experimental )
# mov – QuickTime ( testing )
ffmpeg_video_codec mpeg4

The last option that I changed is the destination where the files are written, the default is /tmp, but since Ubuntu erase it at every boot, I think is not a good idea, give a directory where you can write as a user and have a lot of space (I put /home/myuser/motion/)

# Target base directory for pictures and films
# Recommended to use absolute path. (Default: current working directory)
target_dir /home/zeno/motion

Motion presents images captured through a web interface, set a username and password

# Authentication for the http based control. Syntax username:password
# Default: not defined (Disabled)
control_authentication username:PASSWORD

Now save the file, and add to our username the group motion:

adduser USER motion

Note that in order for change to take effect it will have to restart the X server, just do a logout and then a login, remove also motion from autostart at boot with this command:

update-rc.d -f motion remove

From now on we will decide when to start motion and video surveillance, with a simple terminal command as simple user: motion

When the daemon is started, the management of motion can be made through web interface. Open your browser and point to this address:


You will have access to at small Motion interface with which you can edit the configuration file, noting the current settings and start or pause the motion detection. Also, you can monitor in real time what your webcam is capturing from another web interface. Just point your browser to this other address:


Finally, if you want to associate some action with a motion detection you should act on the motion configuration file (/etc/motion/motion.conf) and change these parameters:

# Command to be executed when an event starts. (default: none)
# An event starts at first motion detected after a period of no motion defined by gap
; on_event_start value
# Command to be executed when an event ends after a period of no motion
# (default: none). The period of no motion is defined by option gap.
; on_event_end value

You must put your scripts instead of “value”, could be an email, sms or any action that may be initiated from your PC.

Have fun capturing what’s going in front of your webcam when you are away.

Popular Posts:

Flattr this!

  9 Responses to “Webcam server on Linux”

  1. […] Følg guiden her. Related Tags: Linux, motion, webcam Previous Topic: Ringsted: Succes med Ubuntu XP-look […]

  2. […] 第一份方案是基于Motion软件的。能够在特定环境下触发,如声音、特定操作等,并能用邮件、SMS远程操纵,同时支持远程调节亮度、对比度等。  原文 […]

  3. […] mode,change the image resolution,adjust the brightness, contrast and saturation,etc.. (Read the original post to do these […]

  4. […] mode,change the image resolution,adjust the brightness, contrast and saturation,etc.. (Read the original post to do these […]

  5. […] 第一份方案是基于 Motion 软件的。能够在特定环境下触发,如声音、特定操作等,并能用邮件、SMS远程操纵,同时支持远程调节亮度、对比度等。  原文 […]

  6. this is cool. thanks for the info.

  7. Not every Linux distribution runs aptitude.

    • I usually do example based on my Ubuntu, i’m sure that the users of other distro know their package manager and so have no difficulty in installing it.

      Also Ubuntu and Debian have large repository this is not true for all distro, and sometime you’ll have to go from the source.


  8. Thanks for sharing! I played around with ffserver for 7 hours yesterday but could not get it to work. Your instructions for motion worked instantly and the ability to create masks is excellent (about to try that out). Next I’ll read your newer blog “WEBCAM SERVER ON LINUX 2”. Thanks again!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>