Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import argparse
- from datetime import datetime, timedelta
- parser = argparse.ArgumentParser(description='Tool to adjust srt-file subtitle timing values')
- parser.add_argument('source', metavar='source', type=str, nargs=1,
- help='source srt-file')
- parser.add_argument('-a', '--appear', dest='appear', action='store',
- default=0, type=int, nargs=1,
- help='Integer value, milliseconds to add to time when subtitle appears (default: 0)')
- parser.add_argument('-d', '--disappear', dest='disappear', action='store',
- default=0, type=int, nargs=1,
- help='Integer value, milliseconds to add to time when subtitle disappears (default: 0)')
- args = parser.parse_args()
- try:
- with open(args.source[0]) as fp:
- count = 0
- while True:
- count += 1
- line = fp.readline()
- if not line:
- break
- times = line.strip().split('-->')
- if (len(times) == 2) and (args.appear != 0 or args.disappear != 0):
- if args.appear != 0:
- appears = datetime.strptime(times[0].strip(),'%H:%M:%S,%f')
- appears += timedelta(milliseconds=args.appear[0])
- appears = datetime.strftime(appears, '%H:%M:%S,%f')[:-3]
- else:
- appears = times[0].strip()
- if args.disappear != 0:
- disappears = datetime.strptime(times[1].strip(),'%H:%M:%S,%f')
- disappears += timedelta(milliseconds=args.disappear[0])
- disappears = datetime.strftime(disappears, '%H:%M:%S,%f')[:-3]
- else:
- disappears = times[1].strip()
- print("{} --> {}".format(appears, disappears))
- else:
- print(line.strip())
- except FileNotFoundError:
- print('File "{}" not found'.format(args.source[0]))
- except Exception as e:
- print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement