Skip to content

Instantly share code, notes, and snippets.

@corenominal
Created December 12, 2015 09:20
Show Gist options
  • Save corenominal/db4336303a324aeab3cc to your computer and use it in GitHub Desktop.
Save corenominal/db4336303a324aeab3cc to your computer and use it in GitHub Desktop.
A time-lapse shell script for the Raspberry Pi
#!/bin/bash
# To mount drives, add to fstab, e.g.
# ===================================
# UUID=5b4b881f-8d88-4faf-9e76-6bbed8e92002 /home/user/data-one ext2 auto 0 0
# UUID=46910aac-a24e-422a-b871-74e0cbf4850c /home/user/data-two ext2 auto 0 0
# Crontab entries
# ===============
# * * * * * /home/user/bin/camd.sh
# * * * * * ( sleep 30 ; /home/user/bin/camd.sh )
# To build the video
# ==================
# cd <dir containing photos>
# ls *.jpg > list.txt
# mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o timelapse.avi -mf type=jpeg:fps=24 mf://@list.txt
# Set some variables
USER=$(whoami) # Required as the script will be called by cron.
COUNT_FILE=/home/$USER/.photo_count # File used to store incrementing number
USB1='data-one' # Directory name of first USB drive
USB2='data-two' # Directory name of second USB drive
# Test for existance of the file that is used to store
# the incrementing number for use in naming output files.
# If it doesn't exist, create it and supply it with a
# starting number.
if [ ! -f $COUNT_FILE ]; then
echo 10000000 > $COUNT_FILE
fi
# Read the incrementing number, store as variable,
# increment it.
COUNT_IN=`cat $COUNT_FILE`
COUNT_OUT=$(($COUNT_IN + 1))
# In order to figure out which USB drive to store the
# photo to, get a percentage how much space has been
# used on each drive.
DRIVE_1=`df -h | grep $USB1 | awk '{ print $5}'`
DRIVE_1=${DRIVE_1%\%}
DRIVE_2=`df -h | grep $USB2 | awk '{ print $5}'`
DRIVE_2=${DRIVE_2%\%}
# Sanity check: make sure both drives are not too full,
# if so, exit the script now.
if [ $DRIVE_1 -gt 98 ] && [ $DRIVE_2 -gt 98 ]; then
exit
fi
# Take the photo and output it to a temporary location.
# Run 'raspistill --help' to see full list of options.
raspistill -q 100 -w 1920 -h 1080 -n -o /home/$USER/${COUNT_OUT}.jpg
# Sleep for a while.
sleep 5s
# Select USB drive to move photo to. Try drive one first,
# switch to drive two if drive one is full.
if [ $DRIVE_1 -lt 98 ]
then
DRIVE=/home/$USER/$USB1
else
DRIVE=/home/$USER/$USB2
fi
# Move the photo.
mv /home/$USER/${COUNT_OUT}.jpg ${DRIVE}/${COUNT_OUT}.jpg
# Write incremental number to file and exit.
echo $COUNT_OUT > $COUNT_FILE
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment