Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from __future__ import print_function
- import re
- import sys
- import datetime
- timechars_re = re.compile(r'^[:0-9]+$')
- def time_parts(timecode):
- parts = timecode.split(':')
- parts.reverse()
- s = int(parts[0])
- m = int(parts[1])
- h = 0
- if len(parts) >= 3:
- h = parts[2]
- return h, m, s
- def time_from(timecode):
- h, m, s = time_parts(timecode)
- return datetime.time(hour=h, minute=m, second=s)
- def time_plus(t, hrs_to_add=0, mins_to_add=0, secs_to_add=0, ms_to_add=0):
- assert secs_to_add <= 59
- assert mins_to_add <= 59
- delta = datetime.timedelta(minutes=mins_to_add,
- seconds=secs_to_add,
- milliseconds=ms_to_add)
- end = datetime.time(hour=t.hour,
- minute=t.minute + mins_to_add,
- second=t.second + secs_to_add,
- microsecond=t.microsecond + ms_to_add * 1000)
- return end
- def start_time(time_text):
- h, m, s = time_parts(time_text)
- t = datetime.time(hour=h, minute=m, second=s)
- t2 = time_plus(t, ms_to_add=100)
- return t2
- def end_time(time_text):
- h, m, s = time_parts(time_text)
- t = datetime.time(hour=h, minute=m, second=s)
- return t
- def last_time(time_text):
- h, m, s = time_parts(time_text)
- t = datetime.time(hour=h, minute=m, second=s)
- t2 = time_plus(t, secs_to_add=5)
- return t2
- def time_line(t0, t1):
- return str(start_time(t0)) + ',' + str(end_time(t1))
- def main(fname):
- with open('cc.sub', 'w') as outf:
- print('[SUBTITLE]', file=outf)
- next_time = None
- prev_time = None
- prev_line = None
- with open(fname) as inf:
- for fline in inf:
- fline = fline.strip()
- if timechars_re.match(fline):
- this_time = fline
- if prev_time is not None:
- time_text = time_line(prev_time, this_time)
- print(time_text, file=outf)
- if prev_line:
- print(prev_line, file=outf)
- print(file=outf)
- prev_time = this_time
- else:
- prev_line = fline
- print('%s,%s' % (str(start_time(prev_time)),
- str(last_time(this_time))), file=outf)
- print(prev_line, file=outf)
- if __name__ == '__main__':
- main(sys.argv[1])
Add Comment
Please, Sign In to add comment