Guest User

YouTube subtitle converter (download format -> .sub)

a guest
Feb 3rd, 2019
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import print_function
  4.  
  5. import re
  6. import sys
  7. import datetime
  8.  
  9. timechars_re = re.compile(r'^[:0-9]+$')
  10.  
  11.  
  12. def time_parts(timecode):
  13.     parts = timecode.split(':')
  14.     parts.reverse()
  15.     s = int(parts[0])
  16.     m = int(parts[1])
  17.     h = 0
  18.     if len(parts) >= 3:
  19.         h = parts[2]
  20.     return h, m, s
  21.  
  22.  
  23. def time_from(timecode):
  24.     h, m, s = time_parts(timecode)
  25.     return datetime.time(hour=h, minute=m, second=s)
  26.  
  27.  
  28. def time_plus(t, hrs_to_add=0, mins_to_add=0, secs_to_add=0, ms_to_add=0):
  29.     assert secs_to_add <= 59
  30.     assert mins_to_add <= 59
  31.  
  32.     delta = datetime.timedelta(minutes=mins_to_add,
  33.                                seconds=secs_to_add,
  34.                                milliseconds=ms_to_add)
  35.  
  36.     end = datetime.time(hour=t.hour,
  37.                         minute=t.minute + mins_to_add,
  38.                         second=t.second + secs_to_add,
  39.                         microsecond=t.microsecond + ms_to_add * 1000)
  40.     return end
  41.  
  42. def start_time(time_text):
  43.     h, m, s = time_parts(time_text)
  44.     t = datetime.time(hour=h, minute=m, second=s)
  45.     t2 = time_plus(t, ms_to_add=100)
  46.     return t2
  47.  
  48. def end_time(time_text):
  49.     h, m, s = time_parts(time_text)
  50.     t = datetime.time(hour=h, minute=m, second=s)
  51.     return t
  52.  
  53. def last_time(time_text):
  54.     h, m, s = time_parts(time_text)
  55.     t = datetime.time(hour=h, minute=m, second=s)
  56.     t2 = time_plus(t, secs_to_add=5)
  57.     return t2
  58.  
  59. def time_line(t0, t1):
  60.     return str(start_time(t0)) + ',' + str(end_time(t1))
  61.  
  62.  
  63. def main(fname):
  64.     with open('cc.sub', 'w') as outf:
  65.         print('[SUBTITLE]', file=outf)
  66.  
  67.         next_time = None
  68.         prev_time = None
  69.         prev_line = None
  70.  
  71.         with open(fname) as inf:
  72.             for fline in inf:
  73.                 fline = fline.strip()
  74.                 if timechars_re.match(fline):
  75.                     this_time = fline
  76.                     if prev_time is not None:
  77.                         time_text = time_line(prev_time, this_time)
  78.                         print(time_text, file=outf)
  79.                     if prev_line:
  80.                         print(prev_line, file=outf)
  81.                         print(file=outf)
  82.                     prev_time = this_time
  83.                 else:
  84.                     prev_line = fline
  85.  
  86.             print('%s,%s' % (str(start_time(prev_time)),
  87.                              str(last_time(this_time))), file=outf)
  88.             print(prev_line, file=outf)
  89.  
  90.  
  91. if __name__ == '__main__':
  92.     main(sys.argv[1])
Add Comment
Please, Sign In to add comment