I'm new to Raspberry Pi tinkering and I'm trying to make some slight changes to the Adafruit 'Raspberry Pi Wearable Time Lapse Camera' project.
Instead of the camera taking pictures at a given interval until the slim button is pushed down, I'd like it to record a video for a given duration, then shut itself down. Here is the code I currently have:
# Configurable stuff...
INTERVAL=3 # Time between captures, in seconds
WIDTH=1920 # Image width in pixels
HEIGHT=1080 # Image height in pixels
QUALITY=51 # JPEG image quality (0-100)
DEST=/boot/timelapse # Destination directory (MUST NOT CONTAIN NUMBERS)
PREFIX=img # Image prefix (MUST NOT CONTAIN NUMBERS)
HALT=21 # Halt button GPIO pin (other end to GND)
FPS=30 # FPS of video
DURATION=10 # Duration of video in seconds
LED=5 # Status LED pin (v2 Pi cam lacks built-in LED)
starttime=0 # Time of last capture (0 = do 1st image immediately)
gpio -g mode $HALT up # Initialize GPIO states
gpio -g mode $LED out
mkdir -p $DEST # Create destination directory (if not present)
# Record video
FRAME=$(($(find $DEST -name "*.h264" -printf %f\\n | sed 's/^[^1-9]*//g' | sort -rn | head -1 | sed 's/[^0-9]//g') + 1))
OUTFILE2=`printf "$DEST/$PREFIX%05d.h264" $FRAME`
raspivid -n -t $DURATION*1000 -w $WIDTH -h $HEIGHT -fps $FPS -o $OUTFILE2
starttime=$(date +%s)
while : # Lasts as long as duration + 10 seconds
do
currenttime=$(date +%s)
if [ $(($currenttime-$starttime)) -ge $DURATION+10 ]; then
gpio -g write $LED 1
shutdown -h now
fi
done
However the Raspberry Pi never shuts itself down, and when I have to manually turn it off I'm left with a 1 KB h264 file that causes VLC to spaz out.