Skip to content

Instantly share code, notes, and snippets.

@ethicnology
Last active December 22, 2022 16:15
Show Gist options
  • Save ethicnology/204aded2924aa056cc3819c46534b6b3 to your computer and use it in GitHub Desktop.
Save ethicnology/204aded2924aa056cc3819c46534b6b3 to your computer and use it in GitHub Desktop.
Converts .vtt to .srt subtitles with python3
#!/usr/bin/env python
# put this file in /usr/local/bin
# vtt2srt my_subtitle.vtt
import os, re, argparse
parser = argparse.ArgumentParser()
parser.add_argument("filename", help=".vtt file")
args = parser.parse_args()
input_path_filename = args.filename
assert input_path_filename.endswith(".vtt"), "input file should be .vtt"
output_path_filename = input_path_filename[:-4] + ".srt"
time_pattern = r"\b\d{2}:\d{2}:\d{2}\.\d{3}\b --> \b\d{2}:\d{2}:\d{2}\.\d{3}\b"
tag_pattern = r"<.+?>"
count_time = 0
subtitles = False
print("processing… " + input_path_filename)
with open(output_path_filename, "w") as output:
with open(input_path_filename, "r") as input:
for line in input:
pattern = re.search(time_pattern, line)
if subtitles and not pattern:
subtitle_line = line.strip()
subtitle_line = re.sub(tag_pattern, "", subtitle_line)
output.write(subtitle_line + os.linesep)
if pattern:
count_time += 1
time_string = pattern.group()
output.write(str(count_time) + os.linesep)
output.write(time_string.replace(".", ",") + os.linesep)
subtitles = True
print("done " + output_path_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment