Skip to content

Instantly share code, notes, and snippets.

@agrimrules
Last active July 22, 2016 22:25
Show Gist options
  • Save agrimrules/e74ee836492249bc3f4ecfbe2913427c to your computer and use it in GitHub Desktop.
Save agrimrules/e74ee836492249bc3f4ecfbe2913427c to your computer and use it in GitHub Desktop.
__author__ = 'agrimasthana'
import boto
import boto.s3
from boto.s3.key import Key
from picamera import PiCamera
from time import sleep
from os import system
import urllib2
import json
import arrow
def s3upload(filename, bucket):
conn = boto.connect_s3('Did-you-really-think', 'I-would-check-this-in') #AWS credentials
bucket = conn.get_bucket(bucket)
k = Key(bucket, filename)
k.key = filename
with open(filename) as f:
k.set_contents_from_file(f)
print('Completed uploading {}'.format(filename))
def daylight():
res = json.load(urllib2.urlopen('http://ip-api.com/json/'))
lat = res['lat']
lon = res['lon']
tz = res['timezone']
time = json.load(urllib2.urlopen('http://api.sunrise-sunset.org/json?lat=%s&lng=%s&formatted=0' % (lat, lon)))
sunset = time['results']['sunset']
sunrise = time['results']['sunrise']
timespan = {'sunrise': arrow.get(sunrise).to(tz).timestamp,
'sunset': arrow.get(sunset).to(tz).timestamp,
'timezone': tz}
return timespan
if __name__ == '__main__':
imgs = raw_input('What should the captured series be called?: \n')
bkt = raw_input('What S3 bucket are you using?: \n')
camera = PiCamera()
camera.vflip = True #This is because my camera is attached upside down.
camera.resolution = (1920, 1080)
dl = daylight()
crnt = arrow.utcnow().to(dl['timezone']).timestamp
dif = dl['sunset'] - crnt
photostocapture = dif / 5 #It is suggested to take a photo every 3 seconds for sunset timelapse
for i in range(photostocapture):
camera.capture(imgs + '-{0:04d}.jpg'.format(i))
sleep(1)
s3upload(imgs + '-{0:04d}.jpg'.format(i), bkt)
sleep(2)
print('installing avconv library')
system('sudo apt-get install libav-tools -y')
print('Starting to process video')
system('avconv -y -r 30 -i '+imgs+'-%4d.jpg -r 30 -vcodec libx264 '+imgs+'.mp4')
print('uploading processed video to AWS: S3')
s3upload(imgs+'.mp4')
print('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment