Skip to content

Instantly share code, notes, and snippets.

@palaniraja
Created July 26, 2018 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palaniraja/a0ee100dec5a7dbd6a704c51adb96ba2 to your computer and use it in GitHub Desktop.
Save palaniraja/a0ee100dec5a7dbd6a704c51adb96ba2 to your computer and use it in GitHub Desktop.
PiLapse - Python script to take timelapse
#!/usr/bin/python
import os
import time
import subprocess
from time import sleep
import random
# Configuration
sleepDurationInSeconds = 15
checkDiskSpaceEveryImage = 20
rand = random.randint(0,10000)
# "echo \"SEQ-$RANDOM-$(ls | wc -l)-\""
numFiles = subprocess.check_output("echo $(ls | wc -l)", shell=True)
randPrefix = "SEQ-"+str(rand)+"-"+str(numFiles).strip()
imgSeq = 0
shouldAbort = False
print "Timelapse started. Capture image every "+str(sleepDurationInSeconds)+" sec. Check Free Space every "+str(checkDiskSpaceEveryImage)+" images."
while (shouldAbort != True):
# Every x seconds
sleep(sleepDurationInSeconds)
ts = time.strftime("%Y%m%d%H%M%S")
imgName = randPrefix+"-"+str(imgSeq)+"-"+ts
print imgName
os.system("raspistill -t 2000 -n -q 100 -vf -hf -w 1440 -h 1080 -o "+imgName+".jpg")
imgSeq += 1
# Quit after x images
# if (imgSeq > 10):
# shouldAbort = True
# Check freespace every x seconds
if (imgSeq % checkDiskSpaceEveryImage == 0):
dfOut = subprocess.check_output("df | grep root| awk '{print $4}'", shell=True)
freespace = int(dfOut.strip())
# less than 1G
if (freespace < 1000000):
print "Low on space, exiting timelapse"
shouldAbort = True
print "Timelapse completed."
@palaniraja
Copy link
Author

palaniraja commented Jul 26, 2018

I'm aware of built-in raspistill -t 30000 -tl 2000 -o image%04d.jpg

I wrote this script, because I need to manage the quality -q and W x H which -tl flag does not support.

@palaniraja
Copy link
Author

palaniraja commented Jul 26, 2018

Files were later copied to mac, batch renamed later as

  • image0.jpg
  • image1.jpg
  • ...
  • image100.jpg

And used the following command to convert to video

avconv -r 10 -i image%d.jpg -r 10 -vcodec libx264 -vf scale=1440:1080 timelapse.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment