Skip to content

Instantly share code, notes, and snippets.

@neckro
Created February 9, 2024 15:46
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 neckro/95a3050b8782c600ec390037c3ae7301 to your computer and use it in GitHub Desktop.
Save neckro/95a3050b8782c600ec390037c3ae7301 to your computer and use it in GitHub Desktop.
convert faster-whisper subs to an actually usable format
#!/usr/bin/env python3
import fileinput
import re
from math import floor
def main():
r = re.compile("(\\d+\\.\\d+) --> (\\d+\\.\\d+)")
for line in fileinput.input():
m = r.match(line)
if m is None:
print (line.rstrip())
else:
# print (line.rstrip())
print ("%s --> %s" % (convtime(m.group(1)), convtime(m.group(2))))
def convtime(i):
i = float (i)
h = floor (i / 3600)
i = i - (3600 * h)
m = floor (i / 60)
i = i - (60 * m)
s = floor (i)
frag = str(round(i - s, 3)).lstrip("0").lstrip(".")
# print("h:", h)
# print("m:", m)
# print("s:", s)
# print("frag:", frag)
return "%s:%s:%s,%s" % (pad(h), pad(m), pad(s), frag)
def pad(n):
if (n < 10):
return "0%s" % n
return n
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment