Skip to content

Instantly share code, notes, and snippets.

@hma02
Last active April 4, 2017 15:42
Show Gist options
  • Save hma02/debd468f5ca8ab78a5df6444ba4258ad to your computer and use it in GitHub Desktop.
Save hma02/debd468f5ca8ab78a5df6444ba4258ad to your computer and use it in GitHub Desktop.
Adjust movie SRT based on two timestamps
import pysrt
def srt_shift(shift, scale, input_file, output_file):
## the input srt file should be in utf-8 encoding
subs = pysrt.open(input_file)
print len(subs)
subs.shift(seconds=shift)
subs.shift(ratio=scale)
subs.save(output_file, encoding='utf-8')
print 'adjusted'
def strtime_to_sec(strtime='00:01:00,000'):
import datetime
import time
x = time.strptime(strtime.split(',')[0],'%H:%M:%S')
sec = datetime.timedelta(hours=x.tm_hour,
minutes=x.tm_min,
seconds=x.tm_sec).total_seconds()
return sec
if __name__ == '__main__':
input_file ='movie.srt'
output_file='movie_adjusted.srt'
point1_now=strtime_to_sec('00:05:35,682')
point1_true=strtime_to_sec('00:03:06,682')
point2_now =strtime_to_sec('01:26:26,810')
point2_true=strtime_to_sec('01:24:01,810')
import numpy as np
A=np.array([[point2_now,1],[point1_now,1]])
B=np.array([point2_true,point1_true])
scale, shift = np.linalg.solve(A,B).tolist()
srt_shift(shift, scale, input_file, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment