fswebcam
fswebcam -d /dev/video0 -r 640x480 --jpeg 85 -F 5 test.jpg
This uses a compression factor of 85 (good tradeoff of quality/size) and captures 5 frames (for less noise in image).
MPlayer
mplayer tv:// -tv driver=v4l2:device=/dev/video0:width=640:height=480 -frames 3 -vo jpeg
- MPlayer documentation
- archLinux article on MPlayer
VLC
vlc -I dummy v4l2:///dev/video0 --video-filter scene --no-audio --scene-path /home/stoppal/test --scene-prefix image_prefix --scene-format png vlc://quit --run-time=1
Some notes about the Logitech Quickcam Pro 9000
If you are using the Logitech Quickcam Pro 9000 it has an advertised maximum resolution of 1600x1200. Let's try to run that with fswebcam.
stephen@ubuntu:~$ fswebcam -d /dev/video1 -r 1600x1200 --jpeg 85 -F 5 /home/stephen/webcamphotos/$(date +\%Y\%m\%d\%H\%M).jpeg
--- Opening /dev/video1...
Trying source module v4l2...
/dev/video1 opened.
No input was specified, using the first.
Adjusting resolution from 1600x1200 to 960x720.
--- Capturing 5 frames...
Captured 5 frames in 0.40 seconds. (12 fps)
--- Processing captured image...
Setting output format to JPEG, quality 85
Writing JPEG image to ~/home/stephen/webcamphotos/201304051633.jpeg.
Wait a second! Why did it adjust the resolution to 960x720?
It turns out we need to force it to use a YUYV palette instead of the default
stephen@ubuntu:~$ fswebcam -d /dev/video1 -p YUYV -r 1600x1200 --jpeg 85 -F 5 /home/stephen/webcamphotos/$(date +\%Y\%m\%d\%H\%M).jpeg
Configure crontab (make a cronjob) to take a picture every minute or hour
Crontab is a popular *nix utility that executes a command on a user defined interval. Maybe you just want to take a picture every minute, or maybe you want to shutdown your computer Monday through Friday at 10pm. Or maybe you want to run some scripts that backup your data once every 3 months. If you want to run multiple commands you can do so by chaining them with the && keyword, but it's also sometimes worth making a bash script (or maybe a simple Python/Perl/Ruby script) that gets executed as part of the cronjob.
To view the current cron jobs for the current user, type crontab -e
. To view the current cron jobs for the super user, type sudo crontab -e
.
Here are some cronjobs I have set up:
# To take a picture every minute
# */1 * * * * streamer -f jpeg -s 1024x768 -o /home/stephen/timelap/$(date +\%m\%d\%k\%M).jpeg# To take a picture every hour on the 15 minute mark using a different tool
# 15 * * * * fswebcam -r 1024x768 --jpeg 85 -D 4 -F 10 /home/stephen/webcamphotos/$(date +\%Y\%m\%d\%k\%M).jpeg# Take a picture and upload it to the webserver every hour
@hourly bash /home/stephen/scripts/take_photo_and_push.sh
The last cronjob calls a bash script that looks like this:
#!/bin/bash
#Take a picture, then push it to a remote webserver#Take a photo
fswebcam -d /dev/video1 -p YUYV -r 1600x1200 --jpeg 85 -D 2 -F 15 /home/stephen/webcamphotos/$(date +\%Y\%m\%d\%H\%M).jpeg#Navigate to the directory
cd /home/stephen/webcamphotos/#Find the most recent jpeg
NEW_JPEG=$(ls -t | grep '\>.jpeg?s=#39; | head -1)#Push it to the remote webserver
scp /home/stephen/webcamphotos/$NEW_JPEG stephen@netinstructions.com:/home/stephen/netinstructions.com/homeserver/latest.jpeg
For more information on cronjobs and crontab, take a look at this guide.
If you want to view any logs for the the cron job you can view the logs by typing:
$ grep CRON /var/log/syslog
Viewing/Transferring the Pictures
Okay, so you found a command line utility that takes pictures, and perhaps a cronjob that runs that command every 5 minutes or 10 minutes or every hour or once a day, but how do you look at the picture?
There are a couple of ways of doing this. If you're using the desktop version of Ubuntu (with a nice graphical user interface) you just double click on the photo. For the rest of us who are SSH'ing in to a remote machine or are using the server version of Ubuntu or some other Linx distro, we have a few options:
- FileZilla to grab the files and transfer them to our local machines
- If you have a web server (Apache, ngnix, or something else) on the server, move the file to the web directory
- SCP the file to a remote web server. For example, I have a few websites (such as this one) hosted by Dreamhost, and they provide shell access
The command to securely transfer a file on one machine to another looks like this:
scp /home/stephen/webcamphotos/$NEW_JPEG stephen@netinstructions.com:/home/stephen/netinstructions.com/homeserver/latest.jpeg
Good luck!