Created
June 22, 2021 03:47
-
-
Save yudenzel/76a41282a9f66f24f7917676e27fd1d8 to your computer and use it in GitHub Desktop.
Convert subtitles from srv3 to vtt format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from os import write | |
import xml.etree.ElementTree as ET | |
import sys | |
class paragraph(): | |
def __init__(self, t, d, text): | |
self.t = int(t if t else 0) | |
self.d = int(d if d else 0) | |
self.text = text.strip() if text else "" | |
def vtt_file_from(input): | |
return input.replace(".srv3", ".vtt") | |
def format_timestamp(ms): | |
hh = ms / 3600000 | |
ms = ms % 3600000 | |
mm = ms / 60000 | |
ms = ms % 60000 | |
ss = ms / 1000 | |
ms = ms % 1000 | |
return "%02d:%02d:%02d.%03d" % (hh, mm, ss, ms) | |
def generate_vtt_file(name, list_of_p): | |
with open(name, "w") as of: | |
of.write("WEBVTT\n\n") | |
for p in list_of_p: | |
if len(p.text) > 0: | |
time_line = ("%s --> %s\n" % (format_timestamp(p.t), format_timestamp(p.t + p.d))) | |
of.write(time_line) | |
of.write(p.text + "\n\n") | |
pass | |
def convert_srv3_to_vtt(input): | |
doc = ET.parse(input) | |
root = doc.getroot() | |
list_of_p = [] | |
if root.tag == "timedtext" and root.get("format") == "3": | |
for root_child in root: | |
if root_child.tag == 'body': | |
body = root_child | |
for body_child in body: | |
if body_child.tag == "p": | |
p = body_child | |
t = p.get("t") | |
d = p.get("d") | |
text = "".join(p.itertext()) | |
list_of_p.append(paragraph(t, d, text)) | |
pass | |
last_t = 0xFFFFFFFF | |
for p in reversed(list_of_p): | |
if len(p.text) > 0: | |
if p.t + p.d > last_t: | |
p.d = last_t - p.t | |
last_t = p.t | |
generate_vtt_file(vtt_file_from(input), list_of_p) | |
pass | |
args = sys.argv | |
for input in args[1:]: | |
print(("input: %s" % input)) | |
convert_srv3_to_vtt(input) | |
input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment