Setting up a Security Camera for a Linux Webcam

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.



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


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

Thomas is the author of Advanced Microservices and is a prolific public speaker with a passion for reducing complex problems into simple language and diagrams. His career includes working at Fortune 50's in the Midwest, co-founding a successful startup, and everything in between.