import os
from subprocess import call

resolutions = {
    '4k': {
        'x': 3840,
        'y': 2160,
        'bitrate': '50000k',
    },
    '1080': {
        'x': 1920,
        'y': 1080,
        'bitrate': '10000k',
    },
}
res = resolutions['4k']
frame_rate = 24 #frames per second

# select the source directory(ies)
#dirs = [".",]
dirs = ["/media/aaronm/NIKON D5100/DCIM/100D5100/",]

print call("mkdir scaled", shell=True)
print "starting"

i = 0
for d in dirs:
    for file in sorted(os.listdir(d)):
        i += 1
        if file.endswith('JPG'):
            print file
            command = "convert '%(file)s' -scale %(x)sx%(x)s -gravity Center -crop %(x)sx%(y)s+0+0 scaled/image%(num)03d.jpg" % {
                'file': d + file,
                'num': i,
                'x': res['x'],
                'y': res['y'],
                }
            print "command is", command
            call(command, shell=True)

#make the video
command = "avconv -r %(frame_rate)s -i scaled/image%%03d.jpg -b:v %(bitrate)s timelapse.mp4" % {
    'bitrate': res['bitrate'],
    'frame_rate': frame_rate,
    }
print 'command is', command
call(command, shell=True)

#cleanup
call("rm -rf scaled", shell=True)


