Advertisement
Guest User

Untitled

a guest
Jul 30th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import argparse
  4. from datetime import datetime, timedelta
  5.  
  6. parser = argparse.ArgumentParser(description='Tool to adjust srt-file subtitle timing values')
  7. parser.add_argument('source', metavar='source', type=str, nargs=1,
  8.                     help='source srt-file')
  9. parser.add_argument('-a', '--appear', dest='appear', action='store',
  10.                     default=0, type=int, nargs=1,
  11.                     help='Integer value, milliseconds to add to time when subtitle appears (default: 0)')
  12. parser.add_argument('-d', '--disappear', dest='disappear', action='store',
  13.                     default=0, type=int, nargs=1,
  14.                     help='Integer value, milliseconds to add to time when subtitle disappears (default: 0)')
  15.  
  16. args = parser.parse_args()
  17.  
  18. try:
  19.   with open(args.source[0]) as fp:
  20.     count = 0
  21.     while True:
  22.         count += 1
  23.         line = fp.readline()
  24.         if not line:
  25.           break
  26.         times = line.strip().split('-->')
  27.         if (len(times) == 2) and (args.appear != 0 or args.disappear != 0):
  28.           if args.appear != 0:
  29.             appears = datetime.strptime(times[0].strip(),'%H:%M:%S,%f')
  30.             appears += timedelta(milliseconds=args.appear[0])
  31.             appears = datetime.strftime(appears, '%H:%M:%S,%f')[:-3]
  32.           else:
  33.             appears = times[0].strip()
  34.           if args.disappear != 0:
  35.             disappears = datetime.strptime(times[1].strip(),'%H:%M:%S,%f')
  36.             disappears += timedelta(milliseconds=args.disappear[0])
  37.             disappears = datetime.strftime(disappears, '%H:%M:%S,%f')[:-3]
  38.           else:
  39.             disappears = times[1].strip()
  40.           print("{} --> {}".format(appears, disappears))
  41.         else:
  42.           print(line.strip())
  43.  
  44. except FileNotFoundError:
  45.   print('File "{}" not found'.format(args.source[0]))
  46.  
  47. except Exception as e:
  48.   print(e)
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement