Advertisement
wolfsinner

srt_resync

Jun 10th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #Python 2 compatible (tested with 2.6.6)
  3. #This script assumes a lot, and doesn't follow many DRY guidelines. There's a lot of room for improvement.
  4. #It also only works with positive offsets, as it was what I needed. Might update it to work with negative offsets.
  5. #Created to resync some Curb Your Enthusiasm subtitles. Great show by the way. :)
  6.  
  7. """
  8. srt_resync.py: Give it 2 parameters: first the path to the file, and second an offset in positive seconds (can be fractional).
  9. The script produces a file with the same name with "-resync" appended to the end of the filename (before the extension)
  10. """
  11.  
  12. __author__ = "Nuno Melo";
  13. __copyright__ = "Copyleft 2012";
  14. __credits__ = ["Nuno Melo"];
  15. __license__ = "WTFPL";
  16. __version__ = "1.0b";
  17.  
  18. import re, sys, math, os;
  19.  
  20. HOURS_IN_SECONDS = 59*59;
  21. MINUTES_IN_SECONDS = 59;
  22.  
  23. LINE_BREAK = "\n";
  24.  
  25. args = sys.argv;
  26.  
  27. def resync(time, hours, minutes, seconds, ms):
  28.     tokens = time.split(':');
  29.     new_hours = int(tokens[0]) + hours;
  30.     new_minutes = int(tokens[1]) + minutes;
  31.     tokens = tokens[2].split(',');
  32.     new_seconds = int(tokens[0]) + seconds;
  33.     new_ms = int(tokens[1]) + ms;
  34.     while new_ms-999 >= 0:
  35.         new_ms -= 999;
  36.         new_seconds += 1;
  37.     while new_seconds-59 >= 0:
  38.         new_seconds -= 59;
  39.         new_minutes += 1;
  40.     while new_minutes-59 >= 0:
  41.         new_minutes -= 59;
  42.         new_hours += 1;
  43.     return "%s:%s:%s,%s" % (str(new_hours).zfill(2), str(new_minutes).zfill(2), str(new_seconds).zfill(2), str(new_ms).zfill(3));
  44.    
  45.    
  46. if len(args) == 3:
  47.     srtf = args[1];
  48.     try:
  49.         diff = float(args[2]);
  50.     except ValueError:
  51.         print "Usage: python %s file_path number_seconds" % args[0];
  52.         sys.exit(1);
  53.  
  54.     #get actual numbers
  55.     hours = 0;
  56.     minutes = 0;
  57.     seconds = 0;
  58.     ms = 0;
  59.     while diff-HOURS_IN_SECONDS >= 0:
  60.         hours += 1;
  61.         diff -= HOURS_IN_SECONDS;
  62.     while diff-MINUTES_IN_SECONDS >= 0:
  63.         minutes += 1;
  64.         diff -= MINUTES_IN_SECONDS;
  65.     seconds = int(diff);
  66.     ms = int((diff-seconds)*999);
  67.  
  68.     try:
  69.         f = open(args[1], 'r');
  70.     except IOError:
  71.         print "File does not exist. Exiting...";
  72.         sys.exit(1);
  73.  
  74.     data = f.read();
  75.  
  76.     lines = re.findall(r"([0-9]{1,3})(?:\r)?\n?([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}) --> ([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})(?:\r)?\n(.*?(?:\r)?\n(?:\r)?\n)", data, re.DOTALL);
  77.    
  78.     file_ext = os.path.splitext(args[1])[1];
  79.     new_file = args[1].replace(file_ext, "-resync" + file_ext);
  80.     out = open(new_file, 'w');
  81.    
  82.     for nuple in lines:
  83.         new_start = resync(nuple[1], hours, minutes, seconds, ms);
  84.         new_end = resync(nuple[2], hours, minutes, seconds, ms);
  85.         out.write(nuple[0] + LINE_BREAK);
  86.         out.write(new_start + " --> " + new_end + LINE_BREAK);
  87.         out.write(re.sub('(?:\r)\n', LINE_BREAK, nuple[3]));
  88.        
  89.  
  90.     f.close();
  91.     out.close();
  92.    
  93.     print "Resynced %s." % args[1];
  94. else:
  95.     print "Usage: python %s file_path number_seconds" % args[0];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement