Skip to content

Instantly share code, notes, and snippets.

@ioggstream
Created April 25, 2015 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ioggstream/5fefa4073cd468ab85b1 to your computer and use it in GitHub Desktop.
Save ioggstream/5fefa4073cd468ab85b1 to your computer and use it in GitHub Desktop.
Align subtitles to video file
from __future__ import print_function, division
from time import gmtime,localtime,strptime,strftime,mktime
from sys import argv
def ts_to_s(x):
x = x[:x.find(",")]
return (mktime(strptime(x, "%H:%M:%S"))) # + 10 * 3600*365)
def get_t(l, linear):
if not '-->' in l: return l
X = (ts_to_s(x) for x in l.split(" --> "))
X = (linear(x) for x in X)
try:
X = (localtime(x) for x in X)
X = list(X)
print("X:", list(X))
X = (strftime("%T,000", x) for x in X)
except Exception as e:
print("X:", list(X))
return "%s --> %s\n" % tuple(X)
if __name__ == '__main__':
filename = argv[1]
s0, e0, s1, e1 = (ts_to_s(x) for x in argv[2:6])
m = (e1 - s1) / (e0 - s0)
q = s1 - s0
linear = lambda x: m*x+q
print (m,q)
with open(argv[1]) as fh:
for l in fh:
try:
print(get_t(l, linear), end="")
except:
print("err", l)
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment