Advertisement
ProfCTurner

Untitled

Oct 9th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #
  4. # Script to create multiple timelapse videos
  5. #
  6.  
  7. # Some important configuration
  8.  
  9. # Directory that contains raw images
  10. image_dir = '/home/ftpsecure/timelapse'
  11. # Directory that contains output videos
  12. video_dir = '/home/ftpsecure/timelapse-videos'
  13.  
  14. # Here is the command to create a timelapse given jpgs in the CWD, the output filename needs to be appended
  15. #timelapse_cmd = 'mencoder "mf://*.jpg" -mf fps=12:type=jpg -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell:vbitrate=7000 -o'
  16. timelapse_cmd = 'avconv -y -r 10 -i *.jpg -r 10 -vcodec libx264 -q:v 20 -vf crop=4256:2832,scale=iw/4:ih/4 '
  17. # Here is the command to find a list of directories that require timelapse building
  18. find_cmd = '/usr/bin/find ' + image_dir + ' -type f -name make_timelapse'
  19.  
  20. # Configuration Ends
  21.  
  22. import os
  23. import subprocess
  24. import errno
  25. import shutil
  26.  
  27. def individual_timelapse(pathname):
  28. 'Given one path, build the timelapse for it'
  29. (YYYY, MM, DD) = extract_date_from_pathname(pathname)
  30. relative_filename = "%s-%s-%s.avi" % (YYYY, MM, DD)
  31. individual_cmd = timelapse_cmd + " " + relative_filename
  32.  
  33. print "Building the timelapse for %s..." % pathname
  34. # Run the command
  35. p = subprocess.Popen(individual_cmd, shell=True, cwd=pathname, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  36. for line in p.stdout.readlines():
  37. print line
  38. return_value = p.wait()
  39.  
  40. # Make sure paths exist to allow the move
  41. mkdir_p(video_dir + os.sep + YYYY)
  42. mkdir_p(video_dir + os.sep + YYYY + os.sep + MM)
  43.  
  44. # Move the file
  45. print "Moving the file..."
  46. shutil.move(pathname + os.sep + relative_filename, video_dir + os.sep + YYYY + os.sep + MM + os.sep + relative_filename)
  47.  
  48. def extract_date_from_pathname(pathname):
  49. 'The timelapse directory must end in YYYY-MM-DD, extract these aspects'
  50. (stub, date) = os.path.split(pathname)
  51. date_components = date.split('-')
  52. YYYY = date_components[0]
  53. MM = date_components[1]
  54. DD = date_components[2]
  55.  
  56. print "Extracted date %s-%s-%s" % (YYYY,MM,DD)
  57. return (YYYY, MM, DD)
  58.  
  59.  
  60. def build_directory_list():
  61. 'Fetch a list of all directories that need built and return them in a list'
  62.  
  63. print "Building list of paths..."
  64. dir_list = list()
  65. # Run the find command
  66. p = subprocess.Popen(find_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  67. for line in p.stdout.readlines():
  68. (find_path, find_name) = os.path.split(os.path.abspath(line))
  69. dir_list.append(find_path)
  70. return_value = p.wait()
  71. return dir_list
  72.  
  73. import os, errno
  74.  
  75.  
  76. # Straight from stackoverflow.com
  77. def mkdir_p(path):
  78. try:
  79. os.makedirs(path)
  80. except OSError as exc: # Python >2.5
  81. if exc.errno == errno.EEXIST and os.path.isdir(path):
  82. pass
  83. else: raise
  84.  
  85.  
  86. def main():
  87. 'Kick it all off'
  88. dir_list = build_directory_list()
  89.  
  90. for item in dir_list:
  91. print item
  92. individual_timelapse(item)
  93.  
  94. if __name__ == '__main__':
  95. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement