Skip to content

Instantly share code, notes, and snippets.

@itisrazza
Last active August 23, 2017 08:24
Show Gist options
  • Save itisrazza/4d44815bbca6fec539a836ee10758a51 to your computer and use it in GitHub Desktop.
Save itisrazza/4d44815bbca6fec539a836ee10758a51 to your computer and use it in GitHub Desktop.
A simple timelapse program written in Python and uses SimpleCV (for capturing photos) and ffmpeg (for making the video)

To use this you need to install SimpleCV and ffmpeg

For Ubuntu 16.04:

  • Download and run this script
  • Install ffmpeg using sudo apt install ffmpeg
  • Download and run timelapse.py

In terminal:

  • wget https://gist.github.com/thegreatrazz/bad1b2ccfcaaecc33d260febe933243e/raw/d3b1d09637532fd7fbd760d02d37562ec89e591a/install-python-simplecv.sh
  • sudo bash install-python-simplecv.sh
  • sudo apt install ffmpeg
  • wget https://gist.github.com/thegreatrazz/4d44815bbca6fec539a836ee10758a51/raw/f61b94a6f17fb1deeb4b47e55668c70603e54fc5/timelapse.py
  • sudo python2 timelapse.py
#!/usr/bin/python2
#
# Timelapse maker by Raresh Nistor (@thegreatrazz)
#
# A program that takes a photo every amount of time set
# in order to make a timelapse
#
#
# Keep in mind that I am rather crap at writing in Python
# and this video may make a bunch of more professional devs
# cringe. Viewer disretion advised.
#
# It also uses ffmpeg to stich the images into a video
#
# Import dem libz
import os, sys, random, time
# Check a few things
if os.path.isdir("captures"):
print "You need to delete the captures folder before you can do anything else"
sys.exit(1)
os.mkdir("captures")
if os.path.exists("timelapse.mp4"):
print "There already is a video. Please move it somewhere else first."
sys.exit(2)
# Ask for parameters
captureInterval = 0 # How many seconds between captures
captureNumber = 0 # How many captures before stopping
outputFps = 0 # How many frames per second in the video
try:
captureInterval = float(raw_input("How many seconds between captures? "))
captureNumber = int(raw_input("How many captures before stopping? "))
outputFps = float(raw_input("How many frames per second in the video? "))
except:
print "You need to input numbers, not text"
os.exit(3)
print ""
print "The recording will take more than {} seconds long".format(captureInterval * captureNumber)
print "Your video will be {} seconds long".format(captureNumber / outputFps)
print ""
print "" # A buffer for the ASCII escape lower down
# Import SimpleCV (cuz its stupid simple... cv)
import SimpleCV
# Create a camera object
cam = SimpleCV.Camera()
# Photo-taking loop
for i in range(0, captureNumber):
print "\033[F{} :: {}s left".format(i + 1, captureInterval * (captureNumber - i))
img = cam.getImage()
img.save("captures/img-{}.png".format(i))
time.sleep(captureInterval)
# FFMPEG bit
print "\033[FCapturing complete!"
print ""
print "Making video..."
print "Use 'ffmpeg -framerate {} -i captures/img-%d.png timelapse.mp4' if it fails".format(outputFps)
os.system("ffmpeg -framerate {} -i captures/img-%d.png timelapse.mp4".format(outputFps))
print "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment