Skip to content

Instantly share code, notes, and snippets.

@jswetzen
Created March 21, 2023 14:42
Show Gist options
  • Save jswetzen/ed8578ed6fef70ccf7982a53fa2da4eb to your computer and use it in GitHub Desktop.
Save jswetzen/ed8578ed6fef70ccf7982a53fa2da4eb to your computer and use it in GitHub Desktop.
Subtitle (SRT) sync tool in Python
# Sync a subtitle file, both for offset and drift
#
# Find the time for the first and last spoken line, both in the audio and subtitles
# Fill in the times, in/out file names and encoding
# Run!
from datetime import timedelta
import srt
from srt import Subtitle
input_file = "input.srt"
output_file = "sync.srt"
encoding = "latin1"
audio_start = timedelta(seconds=24.8)
audio_end = timedelta(seconds=8873.5)
text_start = timedelta(seconds=28.59)
text_end = timedelta(seconds=7109.84)
audio_length = audio_end - audio_start
text_length = text_end - text_start
scaling = audio_length/text_length
def fix(t):
if t < text_start:
return audio_start + t - text_start
else:
return (t-text_start)*scaling + audio_start
with open(input_file, "r", encoding=encoding) as srtfile:
synced = [Subtitle(sub.index, fix(sub.start), fix(sub.end), sub.content) for sub in srt.parse(srtfile)]
with open(output_file, "w") as syncfile:
syncfile.write(srt.compose(synced))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment