Setting up a Security Camera for a Linux Webcam

Multithreaded JavaScript has been published with O'Reilly!

Use this script to take a photo using the webcam on your computer. It makes use of and requires the installation of mplayer. When it takes pictures, it will take 20 of them, and delete the first 19. I have to do this because my netbook has a really crappy camera, and it takes a while to adjust to the light and focus. You might be able to get away with a lower setting, so tweak the number until something looks good.

/home/USERNAME/webcam/grab.sh:

#!/bin/bash

# Take 20 frames worth of pictures
mplayer -vo png -frames 20 tv://

# Copy the 20th frame to the webcam directory as the current time
mv /home/USERNAME/00000020.png /home/USERNAME/webcam/`date +"%Y-%m-%d_%H:%M:%S"`.png

# Delete the first nineteen frames
rm /home/USERNAME/000000*.png

I wouldn't be surprised if mplayer has some sort of command to trim the first 20 frames, and specify output location so you don't have to run this awkward delete, but I wasn't able to figure it out.

The images will be saved into a folder called webcam, and the filenames will be in the format YYYY-MM-DD_HH:MM:SS.

I'm using absolute paths for everything because I'm running this as a CRON job (to take a picture once per minute). Basically, I use it as a security camera. If you'd like to do the same, feel free to snag this crontab entry (which you can add by running crontab -e):

crontab:

* * * * * /home/USERNAME/webcam/grab.sh >/dev/null 2>&1
Tags: #linux
Thomas Hunter II Avatar

Thomas has contributed to dozens of enterprise Node.js services and has worked for a company dedicated to securing Node.js. He has spoken at several conferences on Node.js and JavaScript and is an O'Reilly published author.