Skip to content

Instantly share code, notes, and snippets.

@CodingFu
Created December 12, 2022 14:21
Show Gist options
  • Save CodingFu/ef81eac27c7120dfb874d4d87d302eb4 to your computer and use it in GitHub Desktop.
Save CodingFu/ef81eac27c7120dfb874d4d87d302eb4 to your computer and use it in GitHub Desktop.
import json
import math
import sys
from datetime import timedelta
def format_ms(ms):
remainder, milliseconds = divmod(ms, 1000)
remainder, seconds = divmod(remainder, 60)
hours, minutes = divmod(remainder, 60)
return '{:02}:{:02}:{:02}.{:03}'.format(int(hours), int(minutes), int(seconds), int(milliseconds))
def make_vtt(filename):
f = open(filename, "r")
data = json.load(f)
string = "WEBVTT\n\n"
start_ms = -1
sentence = ""
for word in data["words"]:
if start_ms == -1:
start_ms = word["start"]
last_char = word["text"][-1]
if last_char == "." or last_char == "!" or last_char == "?":
sentence += word["text"]
string += "\n"
string += f'{format_ms(start_ms)} --> {format_ms(word["end"])}'
string += "\n"
string += sentence
string += "\n"
start_ms = -1
sentence = ""
else:
sentence += word["text"] + " "
return string
print(make_vtt(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment