Time lapse Webcam With The Raspberry Pi, fswebcam and mencoder

A nice easy tutorial now. This one will have you making your own time lapse movies in minutes using a button to start the camera.
I will also show you how to encode them into a short movie like the one below:

Here is a list of things that you will need:

  • Rapsberry Pi
  • Debian “Wheezy” Operating System
  • USB webcam (I have used the Trust HD 1080)
  • wiringPi gpio library

Download and install the following packages:

sudo apt-get install fswebcam
sudo apt-get install mencoder

To install wiringPi type the following into the terminal:

git clone git://git.drogon.net/wiringPi

Once downloaded then type:

cd wiringPi
./build

This will install wiringPi on your system.

That is the software taken care of. Now lets take some photos as a time-lapse.
We have a button attached to wiringPi pin 16 on the GPIO header.
Type the following into the terminal:

sudo nano timeplapse.sh

Copy the following into the file:

#!/bin/sh
# Time-lapse capture script
# Andy Ibbitson 17-03-2013
###############################

button=16
 setup ()
 {
 echo Setup
 gpio mode 0 out
 }
 # waitButton:
 # Wait for the button to be pressed. Because we have the GPIO
 # pin pulled high, we wait for it to go low to indicate a push.
 #####################################################
 waitButton ()
 {
 echo "Press the button to begin the sequence ... "
 while [ `gpio read $button` = 1 ]; do
 sleep 0.5
 done
 }
 beginCapture ()
 {
 i=0
 echo "CAPTURING... [CTRL+C] to cancel..."
 while [ $i -lt 750 ]
 do
 fswebcam -r 1280x720 -S 15 --jpeg 99 -p MJPEG --shadow --title "IbboCam" --subtitle "BN11 1BY Looking North..." --info "Author: Andy Ibbitson" --save photos/home$i.jpg -q
 gpio write 0 1
 sleep 0.01
 gpio write 0 0
 sleep 0.5
 i=$(($i+1))
 done
 }
 setup
 while true;
 do
 waitButton
 beginCapture
 done

Save as timelapse.sh and create a directory called “photos”. The script will take up to 750 photos and save them to “photos”. You can terminate the script early with CTRL+C.

Once you have taken all of your photos you can then use encode.sh to make your mini movie:

sudo nano encode.sh

Copy the following into the file:

echo "This script will encode a time lapse movie..."
echo "\n"
echo "ENCODE THE VIDEO NOW? [y/n]..."
read inputmake
filedate=$(date +"%d-%m-%Y-%H-%M")
if [ $inputmake == "y" ]; then
cd photos
ls -1tr | grep -v files > $filedate.txt
echo "Enter the desired frame rate of the finished movie (15-20 is normal)..."
read frames
mencoder -nosound -mf fps=24 -o filedate.avi -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=8000 mf://@filedate.txt
cd ..
echo "\n"
echo "Your time lapse movie has been created. It has been saved to the photos directory."
rm *.jpg rm *.txt else
echo "OK THIS SCRIPT WILL NOW EXIT... Nothing has been done."
exit
fi

And there we have it! Take a look at your time lapse movie. One tip: Clouds make great subject matter as do people. 😀

This entry was posted in GPIO, raspberry pi, Tutorial and tagged , , , , . Bookmark the permalink.

7 Responses to Time lapse Webcam With The Raspberry Pi, fswebcam and mencoder

  1. arungopal says:

    Hi Sir,,

    Iam work on the raspberry pi..My project is when the pushbutton is pressed and take the pictures..that is project..Please give me some ideas how to do this…iam new to raspberry pi…

    Thanks&Regards,
    K.Arungopal

  2. arungopal says:

    Hi Sir when iam type sudo apt-get install wiringpi it shows unable to locate package wiringpi….what iam do?it is header or total wiring pi..How to install wiring pi?Please give me some idea…

    thanks&Regards,
    K.Arungopal

  3. Xena says:

    Very helpful.

    Missing a $ on filedate on your encoder statement

  4. Andrew says:

    I would like to do this same project but with a couple of different options.

    -Can I save it to a mounted NFS?
    -Can I avoid the button and just ssh in and start the script?
    -I’d like to do a time lapse from now until early summer (to show the snow melting, etc) Can I change the 750 limit?

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *