Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # Script to create multiple timelapse videos
- #
- # Some important configuration
- # Directory that contains raw images
- image_dir = '/home/ftpsecure/timelapse'
- # Directory that contains output videos
- video_dir = '/home/ftpsecure/timelapse-videos'
- # Here is the command to create a timelapse given jpgs in the CWD, the output filename needs to be appended
- #timelapse_cmd = 'mencoder "mf://*.jpg" -mf fps=12:type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vbitrate=7000 -o'
- timelapse_cmd = 'avconv -y -r 10 -i *.jpg -r 10 -vcodec libx264 -q:v 20 -vf crop=4256:2832,scale=iw/4:ih/4 '
- # Here is the command to find a list of directories that require timelapse building
- find_cmd = '/usr/bin/find ' + image_dir + ' -type f -name make_timelapse'
- # Configuration Ends
- import os
- import subprocess
- import errno
- import shutil
- def individual_timelapse(pathname):
- 'Given one path, build the timelapse for it'
- (YYYY, MM, DD) = extract_date_from_pathname(pathname)
- relative_filename = "%s-%s-%s.avi" % (YYYY, MM, DD)
- individual_cmd = timelapse_cmd + " " + relative_filename
- print "Building the timelapse for %s..." % pathname
- # Run the command
- p = subprocess.Popen(individual_cmd, shell=True, cwd=pathname, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
- for line in p.stdout.readlines():
- print line
- return_value = p.wait()
- # Make sure paths exist to allow the move
- mkdir_p(video_dir + os.sep + YYYY)
- mkdir_p(video_dir + os.sep + YYYY + os.sep + MM)
- # Move the file
- print "Moving the file..."
- shutil.move(pathname + os.sep + relative_filename, video_dir + os.sep + YYYY + os.sep + MM + os.sep + relative_filename)
- def extract_date_from_pathname(pathname):
- 'The timelapse directory must end in YYYY-MM-DD, extract these aspects'
- (stub, date) = os.path.split(pathname)
- date_components = date.split('-')
- YYYY = date_components[0]
- MM = date_components[1]
- DD = date_components[2]
- print "Extracted date %s-%s-%s" % (YYYY,MM,DD)
- return (YYYY, MM, DD)
- def build_directory_list():
- 'Fetch a list of all directories that need built and return them in a list'
- print "Building list of paths..."
- dir_list = list()
- # Run the find command
- p = subprocess.Popen(find_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
- for line in p.stdout.readlines():
- (find_path, find_name) = os.path.split(os.path.abspath(line))
- dir_list.append(find_path)
- return_value = p.wait()
- return dir_list
- import os, errno
- # Straight from stackoverflow.com
- def mkdir_p(path):
- try:
- os.makedirs(path)
- except OSError as exc: # Python >2.5
- if exc.errno == errno.EEXIST and os.path.isdir(path):
- pass
- else: raise
- def main():
- 'Kick it all off'
- dir_list = build_directory_list()
- for item in dir_list:
- print item
- individual_timelapse(item)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement