vid_utils.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. import re
  2. import os
  3. from glob import glob
  4. from subprocess import Popen, PIPE
  5. from time import strftime, strptime
  6. # many of these imports serve the commented code...
  7. # this is the hard-split version (files need to be concatenated...)
  8. def check_dimension(f):
  9. """ If f is larger than 50MB it divides it into files up to 45MB """
  10. if os.path.getsize(f) > 50 * 1024 * 1023:
  11. os.system("split -b 45MB {0} {1}".format(f, f))
  12. os.remove(f)
  13. # this is the soft-split version, require avconv, but the audio isn't synchronized, avconv's problems :(
  14. '''
  15. def get_duration(filepath): # get duration in seconds
  16. cmd = "avconv -i %s" % filepath
  17. p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
  18. di = p.communicate()
  19. for line in di:
  20. if line.rfind(b"Duration") > 0:
  21. duration = str(re.findall(b"Duration: (\d+:\d+:[\d.]+)", line)[0])
  22. return 3600 * int(duration[2: 4]) + 60 * int(duration[5: 7]) + int(duration[8: 10])
  23. def check_dimension(f): # if f is bigger than 50MB split it in subvideos
  24. if os.path.getsize(f) > 50 * 1024 * 1023:
  25. duration = get_duration(f)
  26. for i in range(0, duration, 180):
  27. start = strftime("%H:%M:%S", strptime('{0} {1} {2}'.format(i // 3600, (i // 60) % 60, i % 60), "%H %M %S")) # TODO this is not pythonic code!
  28. os.system("""avconv -i '{0}' -vcodec copy -acodec copy -ss {1} -t {2} 'part_{3}.mp4'""".format(f, start, 180, (i // 180) % 180))
  29. os.remove(f) # delete original file
  30. '''