bpmircea

subtitles time shift

Feb 15th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import re
  2.  
  3. # Shift amount in seconds
  4. shift = 33.640
  5.  
  6. # Load the .srt file
  7. with open('After.Yang.BluRay.x264.AAC-RARBG.srt', 'r') as f:
  8.     lines = f.readlines()
  9.  
  10. # Regex pattern to match timestamps
  11. pattern = r'(\d{2}):(\d{2}):(\d{2}),(\d{3})'
  12.  
  13. # Shift the timestamps
  14. for i, line in enumerate(lines):
  15.     # Find all the timestamps in the line
  16.     matches = list(re.finditer(pattern, line))
  17.  
  18.     # Shift each timestamp
  19.     for match in matches:
  20.         # Parse the timestamp
  21.         hours = int(match.group(1))
  22.         minutes = int(match.group(2))
  23.         seconds = int(match.group(3))
  24.         milliseconds = int(match.group(4))
  25.  
  26.         # Convert the timestamp to seconds
  27.         total_seconds = hours * 3600 + minutes * 60 + seconds + milliseconds / 1000
  28.  
  29.         # Add the shift amount
  30.         total_seconds += shift
  31.  
  32.         # Convert the shifted timestamp back to hh:mm:ss,ms format
  33.         hours = int(total_seconds // 3600)
  34.         minutes = int((total_seconds % 3600) // 60)
  35.         seconds = int(total_seconds % 60)
  36.         milliseconds = int((total_seconds % 1) * 1000)
  37.  
  38.         # Replace the timestamp in the line with the shifted timestamp
  39.         line = line[:match.start()] + f"{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}" + line[match.end():]
  40.     lines[i] = line
  41.  
  42. # Write the modified subtitles to a new .srt file
  43. with open('output.srt', 'w') as f:
  44.     f.writelines(lines)
  45.  
Add Comment
Please, Sign In to add comment