Skip to content

Instantly share code, notes, and snippets.

@aguilarpgc
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save aguilarpgc/edabba315ab257fff4c5 to your computer and use it in GitHub Desktop.

Select an option

Save aguilarpgc/edabba315ab257fff4c5 to your computer and use it in GitHub Desktop.
Move srt files
import sys
import re
def add_time(pattern,to_add):
first_pattern, d = pattern.split(',')
a, b, c = [int(i) for i in first_pattern.split(':')]
d = int(d)
d -= to_add
# if d < 0:
c += d / 1000
d %= 1000
# if c < 0:
b += c / 60
c %= 60
# if b < 0:
a += b / 60
b %= 60
return "%02d:%02d:%02d,%03d" % (a,b,c,d)
def main():
try:
filename = sys.argv[1]
time_to_add = int(sys.argv[2])
except:
print "Missing arguments: filename and time in miliseconds"
sys.exit()
print "Adding " + str(time_to_add) + " miliseconds..."
f = open(filename, 'r')
# temporal file
new_file = open('testing.srt','w')
for line in f:
aaa = re.match(r'(\d\d:\d\d:\d\d,\d\d\d) --> (\d\d:\d\d:\d\d,\d\d\d)', line)
if aaa:
pat1, pat2 = aaa.groups(0)
new_file.write(add_time(pat1,time_to_add) + ' --> '+ add_time(pat2,time_to_add) + '\n')
else:
new_file.write(line)
new_file.close()
f.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment