Pi Camera – Boot Up Timelaspe

Posted: May 27, 2013 in Guides, Other Products, Raspberry Pi

While I have not had time to properly play around with the new Raspberry Pi camera, I did create a quick time lapse video with it.

Raspberry Pi Camera installed in my case (with yet another modification for easy mounting)

Raspberry Pi Camera installed in my case (with yet another modification for easy mounting)

A week or so later during some twitter based Camera discussions with @RasPiTV @RPiSpy, we had @_smstext join in.  His question was how to get a time lapse to start automatically on power up.

What a great idea, I already had to use my “porta-pi” set-up [a portable DVD Player with some internal modifications to connect to the Raspberry Pi] to perform the time-lapse, where it would be MUCH easier to simply place the Raspberry Pi in location, plug-in a standard mobile phone charger power pack and off it goes! Challenge accepted!

The following script is the end result. I’m sorry it appears like a mass of jumbled code if you aren’t used to bash scripts.

The bulk of the actual work happens inside f_runtimecapture and the rest is setting up options and filenames to use in those commands.  The action is triggered by f_runtimecapture, so that it can easily be set to run in the background (with the & command). The script will capture the images, and then automatically generate a video from the frames when it is finished.

Also, I’ve left a small preview window as there is a bug for some which throws an error if preview is switched off (-n), this has already been fixed by the foundation so just a matter of time before it is updated officially. However, it is useful to see what the camera is capturing. If you have shared your home directory on the RPi with SMB (see Guide to…Remote Connections) then you can also view the images as they get captured.

Finally, due to how my camera is mounted inside my case, it is rotated clockwise by 90 degrees, so I apply a 90 degrees rotation (rotation=”-rot 90″ # rotation 0 90 180 270).

UPDATE 31 May 13: I’ve added a little extra to the script, to allow you to select if you want to run the timelapse (with a 5 min/300 second delayed start) or skip it.

Also, there is code to automatically shutdown 5 mins after it has completed!  That way, if the unit is running from batteries it won’t drain them.

To make it run on power up, add the script to .bash_profile (where “period” is the time between shots, and “shots” the number of shots to take – i.e. 30 seconds, 60 shots)
sudo nano ~/.bash_profile

Add the call to the script:

echo "Perform Time-laspe? Y/n"
read -t $TMOUT answer
case $answer in
  n) echo "Skipped";;
  *) sleep 300;. ~/bin/timelaspe.sh 30 60;ps;;


To save and exit, (ctrl+x,y and enter).

Create the following script:
nano timelaspe.sh
To save and exit, (ctrl+x,y and enter).

# Usage:
# timelaspe

  #Start capture
  raspistill -o $filename -t $timetotal -tl $timeperiod $rotation $preview $size
  #Once finished build image list
  ls "$fname"*.jpg > $dir/image_list.txt
  #Send to be encoded
  #mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o $fnameout -mf type=jpeg:fps=24 mf://@image_list.txt
  CMDmencoder="mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 $extra -vf scale=1920:1080 -o $fnameout -mf type=jpeg:fps=24 mf://@$dir/image_list.txt"
  echo $CMDmencoder
  #Play the video
  #omxplayer $fnameout
  #Display command to remove image files
  echo "#### To remove old images type: rm "$fname"*.jpg ####"
  echo "#### System will shutdown in 5 mins              ####"
  echo "#### To cancel shutdown type: sudo shutdown -c   ####"
  sudo shutdown -h +5
#time period is in ms
#Set defaults:
#let timeperiod=$timemin*$timems #1min
let timeperiod=10*$timems
#Set time total by overall time
#let timetotal=3*$timemin*$timesec*$timems #3 hours
#Or by number of shots
let shottotal=30

#Check for commandline input
if [ "$1" != "" ]; then
  let timeperiod=$1*$timems
if [ "$2" != "" ]; then
let timetotal=$shottotal*$timeperiod

#Work out how long this will take (just for display message)
let timeperioddisplay=($timeperiod/$timems)
let timetotaldisplay=($timetotal/$timems)/$timemin

timestart=$(date +"%Y%m%d-%H%M")
mkdir $dir > /dev/null 2>&1
echo "#### Image Time Period $timeperioddisplay seconds x$shottotal Shots = $timetotaldisplay Mins ####"
echo "#### Saving files as "$fname"-XXXX.jpg ... Capturing now! ####"
#Extra settings
preview="-p 100,100,100,100" #-p x,y,w,h -f fullscreen -n nopreview
rotation="-rot 90" # rotation 0 90 180 270
extra="" #Any extra video settings can be put here
size="-w 1920 -h 1080" #i.e 1920x1080

#Start capture.
# to make it run in the background add & to the end of the line

It is a little rough and ready, but will continue to tweak it as I use it.
Let me know how you get on, and also please share any good time-laspe videos you manage to create!

Here’s one I made earlier! 😀


Also I thought I would share some links which @recantha on twitter posted, as they are astounding images created using the Raspberry Pi Camera!

RaspberryPi Camera for Astrophotographyhttp://zeusbox.net/blog/2013/05/raspberrypi-camera-board/
Dave Akerman’s Raspberry Eye In The Skyhttp://www.daveakerman.com/?p=1154


  1. Excellent, I was just looking exactly for this. I’ll give it a try!

  2. […] Read more over at  Meltwater’s Raspberry Pi Hardware. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.