Timelapse Video Recording Using a Rapberry Pi

RaspberryPi

Getting Ready…

For this challenge you will need:

  • A Raspberry Pi with Python 2 installed,
  • A PiCamera.

Follow the steps described on the following pages to make sure you have plugged the camera in your raspberry pi and that you have enabled it. Also make sure you have installed the picamera library.

Getting Started


timelapse

First we need to write a Python script to take and store a picture every “x” seconds. We will do this using an infinite loop.

To make sure we can exit the loop we will catch a keyboard interrupt (when the user presses CTRL + “C”. This will terminate the program.

The main variable used in this script is the timeInterval variable. Per default we set it to 5 seconds. You may want to change this depending on what you are recording and how long you intend to record it for.

import picamera, time
#Initailise the camera
camera=picamera.PiCamera()

#This will be used to name the pictures once stored.
filename = "frame_"
timeInterval = 5 # The time interval between two pictures, in seconds

#We have left all the settings in case you need to make some adjustments to improve the picture quality
camera.hflip = True
camera.vflip = True
#camera.sharpness = 0
#camera.contrast = 0
#camera.brightness = 50
#camera.saturation = 0
#camera.ISO = 0
#camera.video_stabilization = False
#camera.exposure_compensation = 0
#camera.exposure_mode = 'auto'
#camera.meter_mode = 'average'
#camera.awb_mode = 'auto'
#camera.image_effect = 'none'
#camera.color_effects = None
#camera.rotation = 0
#camera.crop = (0.0, 0.0, 1.0, 1.0)

i=1
try:
    while True: #Infinite Loop
        camera.capture(filename + str(i) + '.jpg')
        print(filename + str(i) + '.jpg')
        time.sleep(timeInterval)
        i+=1
except KeyboardInterrupt: #Press Ctrl+C to interrupt
    pass

print('All done...')

Final Step


Now that we have all the pictures stored. We need to combine them into a video clip (.avi).

To do so we will use the following script, from Claude Pageau: Source: https://github.com/pageauc/pi-motion-orig

print "initializing ...." 
import StringIO 
import subprocess 
import os 
import time 
import csv 
from datetime import datetime 
import cgi, cgitb 

imageWidth = 1296 
imageHeight = 972 
# Aspect ratio of video eg 4/3 16/9 Etc. 
# Note put value in quotes. 
aspectRatio = "4/3" 
# Video fps (frames per second) vulue usually  between 2 to 30.  I recommend 5 fps to start 
framesPerSec = 5 
# Video output filename. 
# Can also include folder path, othewise file saved to current folder.  
movieName = "./makemovie.avi" 
 
print "makemovie.py" 
print "============" 
print "Creating makemovie.txt file of *jpg files in google_drive folder."  
ls_params = " -t -r ./*jpg > makemovie.txt" 
exit_status = subprocess.call("ls %s " % ls_params, shell=True) 

print "Creating movie file %s using makemovie.txt" % ( movieName ) 
print "Settings = Image W=%s H=%s aspect=%s fps=%s filename=%s" % ( imageWidth, imageHeight, aspectRatio, framesPerSec, movieName ) 

mencoder_params = "-nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=%s:vbitrate=8000000 -vf scale=%s:%s -o %s -mf type=jpeg:fps=%s  mf://@makemovie.txt" % ( aspectRatio, imageWidth, imageHeight, movieName, framesPerSec ) 
print "memcoder_params = %s" % ( mencoder_params ) 
print "Creating Movie. This will take a while ......." 
print "----------------------------------------------" 

exit_status = subprocess.call("mencoder %s" % mencoder_params, shell=True) 
print "makemovie.py" 
print "============" 
print "Finished timelapse movie filename= %s" % ( movieName ) 

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 5

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: ,