Home Learn Linux Linux Tutorials How to Operate Linux Spycams With Motion

How to Operate Linux Spycams With Motion

fig1 spycamWhen you want something a little simpler and more lightweight than Zoneminder for operating surveillance cameras, try Motion.

Motion is a nice lightweight, yet capable application for operating surveillance cameras on Linux. It works with any Linux-supported video camera, including all V4L Webcams, many IP cameras, Axis cameras, and it controls pan and tilt functions. Motion records movies and snapshots in JPEG, PPM, and MPEG formats, and you can view these remotely in a Web browser thanks to Motion's built-in HTTP server. It stores image files in a directory of your choosing, and it does not require a database, though it supports MySQL and PostgreSQL if you do want to use one.

First let's look at how to get an IP camera working with Motion using my trusty Foscam FI8905W (figure 1), and then we'll add a USB Webcam.

Installation is easy on Debian and Debian derivatives, because Motion is included in the standard software repositories. So all you need to do is run apt-get install motion. You also need libav-tools, which is a fork of ffmpeg. Many moons ago, Debian dropped ffmpeg and replaced it with libav-tools (See Is FFmpeg missing from the official repositories in 14.04? to learn the gory details, and how to get ffmpeg itself if that's what you really want). On other distros, check the downloads page and installation guide for instructions. Most other distros still include ffmpeg.

The installer should create a motion group and user, and add the motion user to the video group. If it doesn't, then you must create them yourself. Add yourself to the video group as well, to get around permissions hassles.

Now run motion to see if it works:

$ sudo motion
[0] Processing thread 0 - config file /etc/motion/motion.conf
[0] Motion 3.2.12 Started
[0] Thread 1 is from /etc/motion/motion.conf
[1] Thread 1 started
[0] motion-httpd/3.2.12 running, accepting connections
[1] Failed to open video device /dev/video0: No such file or directory
[0] motion-httpd: waiting for data on port TCP 8080
[1] Could not fetch initial image from camera
[1] Motion continues using width and height from config file(s)
[1] Resizing pre_capture buffer to 1 items
[1] Started stream webcam server in port 8081

It will go on for many more lines, until you see:

[1] Failed to open video device /dev/video0: No such file or directory
[1] Video signal lost - Adding grey image

Point your Web browser to localhost:8081 and you will see a gray image:

fig2 gray image

This is good, as it means Motion is installed correctly, and all you have to do is configure it. Press Ctrl+C to stop it. Then create a .motion directory in your home directory, copy the default configuration file into it, and change ownership to you:

~$ mkdir .motion
~$ sudo cp /etc/motion/motion.conf .motion/
~$ sudo chown carla:carla .motion/motion.conf

You also need a directory to store images captured by motion:

~$ mkdir motion-images

When you start Motion it looks for a configuration file in the current directory, then in ~/.motion, and finally /etc/motion. Now edit your ~/.motion/motion.conf file-- this example includes basic configurations, and the lines relevant to my Foscam IP camera:

# Start in daemon (background) mode and release terminal (default: off)
daemon on
# Output 'normal' pictures when motion is detected (default: on)
output_normal off
# File to store the process ID, also called pid file. (default: not defined)
process_id_file /var/run/motion/ 
# Image width (pixels). Valid range: Camera dependent, default: 352
width 640
# Image height (pixels). Valid range: Camera dependent, default: 288
height 480
# Maximum number of frames to be captured per second.
# Valid range: 2-100. Default: 100 (almost no limit).
framerate 7
# URL to use if you are using a network camera, size will be autodetected (incl http:// ftp:// or file:///)
# Must be a URL that returns single jpeg pictures or a raw mjpeg stream. Default: Not defined
netcam_url value http://
# Username and password for network camera (only if required). Default: not defined
# Syntax is user:password
netcam_userpass admin:mypassword
# Target base directory for pictures and films
# Recommended to use absolute path. (Default: current working directory)
target_dir /home/carla/motion-images
# Codec to used by ffmpeg for the video compression.
ffmpeg_video_codec mpeg4

You need to create the directory for storing the PID file, as it says in motion.conf:

$ sudo mkdir /var/run/motion

Now try starting it up again:

$ sudo motion
[0] Processing thread 0 - config file /home/carla/.motion/motion.conf
[0] Motion 3.2.12 Started
[0] Motion going to daemon mode

Good so far, now try localhost:8081 again:

fig3 driveway

Well look, there is my driveway. Now I will have plenty of warning when visitors come, so I can loose the moat monsters. Run around in front of your camera to trigger motion detection, and when you come back your images directory should have some .avi movies in it. You should also find a simple Motion control panel at localhost:8080.

IP Camera Settings

How to Operate Your Spycams with ZoneMinder on Linux (part 1) goes into some detail on setting up your camera. You must follow the vendor's instructions for the initial setup, such as assigning a login and password, and setting the IP address. You may have other options as well, such as frame size, motion sensitivity, and color depth or black and white.

Getting the correct netcam_url is sometimes a hassle. For my Foscam I brought up its control panel in Firefox, right-clicked on the image (figure 4), then left-clicked View Image Info. This opens a screen like figure 5, which shows the exact URL of the videostream. In the Chrome browser use "Inspect element."

fig4 control panel

fig5 foscam

Fine-tuning Configuration Values

You can make all kinds of adjustments in your configuration file such as image size, image quality, frame rate, sensitivity to movement, greater sensitivity in selected areas of the frame, file paths, HTTP server settings, and time stamp formats. Motion Guide - Alphabetical Option Reference Manual gives detailed information on each option. Remember to harmonize your Motion settings with the settings in your camera's control panel, if it has one.

USB Cameras

Any V4l-supported USB Webcam should work with little fuss. The video device will be /dev/video0/dev/video0 will be present only when a video camera is connected directory to your computer. This is a basic example configuration for my Logitech Webcam:

videodevice /dev/video0
width 640
height 480
framerate 24
output_normal off
ffmpeg_video_codec mpeg4
target_dir /home/carla/motion

And again, remember that settings such as frame rate and size are dependent on what your camera supports.

Daemonizing Motion

Once you have everything working, make Motion run as a daemon by editing /etc/default/motion, and changing start_motion_daemon=no to start_motion_daemon=yes. Now Motion will start automatically when you start your computer, and you can start and stop it like any other daemon.

Controlling Multiple Cameras

Motion manages multiple cameras with ease -- all you do is give each camera its own configuration file, named thread1.confthread1.conf, and so on. You still need your main motion.conf for common options such as daemon on and filepaths. Then each "thread" file has configurations specific to each camera.



Subscribe to Comments Feed
  • Zydrunas Said:

    Don't sudo motion, i does not root permissions. To grab v4l just add user to video group

  • Michael T. Babcock Said:

    I've been using motion for years when I'm out of the house to monitor activity and it works great, when it works. Some versions have notoriously crashed on me regularly requiring a lot of monitoring and restarting.

  • Jason Said:

    Sorry to hear that. What distro are you running? I've had my server running for two years and haven't had Motion crash on me once. I've actually been extremely impressed by its reliability, as I've had stability issues with other CCTV related software. These days I've opted for full time recording as I had something happen that Motion did not pick up (no fault to Motion really, it was really dark and in the distance, but Motion's 24/7 1 FPS timelapse picked it up). Now I use Motion to grab the "best" frame of an event and save it, while the cameras simply record 24/7 to my server over Samba. That way I can page through the JPG's of Motion to see what took place. If something is suspicious, I can check the timestamp and pull up the full time feed. After shuffling around with different solutions, this combination is a pretty nice one and suits my needs perfectly.

  • Bob_Robertson Said:

    One of these days, I'm going to get this set up. One day..... Thanks, Carla, this is a great guide.

  • Tyee Cambron Said:

    Is there a way to auto startup 2-cam motion on ubuntu server? Going to try this out or might try xeoma. I tried Zoneminder and the database had crashed on me and 250GB hard drive filled up just only running on Christmas week. I had rsync with erase source all the zone minder images and will deal with that later.

  • Tyee Cambron Said:

    Motion seems to be glitched big time in ubuntu server 14.04.1. First setup, yes it was capturing jpg images and was on its way to 7 million files (experience with zone minder) but I stopped it at 60 files...... I changed output_normal to off as described in the manual about saving jpg. And I changed ffmpeg_cap_new to on as described to record video files?... Now motion is complaining about palettes and to choose YUYV or MJPG. I change the settings multi times to use YUYV or MJPG and restart motion, still does the same errors and even right after the error line it says I selected YUYV or MJPG. Or actually it never said I selected MJPG, so I rebooted the ubuntu server and started sudo motion again. It finally showed I selected MJPG but still gave the same palettes error.

  • Larry Reeder Said:

    I was looking at the huge motion man page and doc set and scratching my head. Did another web search and luckily found your tutorial. Had me up and running in about 15 minutes. Thanks!

  • 37//D1on@R@P//73 Said:

    How i can stop motion with php website if i am in home. I would like motion start when I press the button "i am away from home" on my web page and stop when i press "i am home" button. Sorry my bad english:)

  • tyee cambron Said:

    Php has an exec command you can use to run commands. Lookup the php exec or php execute commands... Side note. I got motion working good by removing it all and install again following no guides. Only going by the manual.

  • Chris Said:

    I for the life of me cannot get this working. I have a Foscam fi8905e. I have tried netcam_url as both and I can access the snapshot.cgi stream using the web browser just fine, but trying videostream.cgi just brings up a ton of gobbled up characters. The same gobbled up characters appear when I first start motion via terminal and then try to access http://localhost:8081. I'm pulling my hair out over this one. Please help!

  • Dirk Said:

    I have the same problem with my Foscam FI8905W

  • Fabio C Bastos Said:

    Great article, Carla. Worked smoothly for me! Thank you.

  • Jacques Malaprade Said:

    Thanks for the tip to right click the image in the browser selecting view image info to get the full ip address for the video stream.

  • silvina Said:

    hola! quiero acceder al servidor motion, pero a travez de un cliente que yo he creado en c (client.c), es decir conectarme por consola, el cliente ya lo he hecho,pero me aparece "coneccion refuse", creo q el problema es que no puedo acceder al puerto 8080. alguien ha tratado de hacer eso alguna vez? podrían ayudarme?

  • George Said:

    I'm doing everything according to the tutorial. I have a Sricam AP009 which is an exact copy of your Foscam but I can't connect it. I get the grey interface when I connect to localhost but no stream. I even got to connect a USB cam to the Pi sucessfully however, I'm solely interested in connecting my existing IPcamera... What am I possibly doing wrong? I right click on the image of my IPcamera to get its url and I get a very long address which has a form as folows: http://IPADDRESS:PORT/snapshot.cgi?user=[USERNAME]pwd=[PASSWORD]14288801551460.43631208719864745 In short,I get a load of numbers after ''password'' and most importantly, these numbers change each time I refresh my browser! I tried to transfer this long url but there's no point as, even if i got this to work, the url changes after field ''password'' very frequently. I would appreciate any help by Carla -if she's still here- or by whoever else could possibly know.

  • Graham Said:

    Hi Sorry to asking for help on here but I'm really stuck. Don't know what I've done but I cant seem to get the video. The webcam led light comes on but I cant get video. Can anybody here help please?

  • Subhash Said:

    Hey when i start motion it does not broadcast video on localhost:8081 any solution please

  • uRock Said:

    You may have to edit your firewall to be able to connect. If you have UFW enabled in ubuntu, then you'll need to use CLI or GUFW to open port 8081.

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board