Created
May 21, 2021 07:54
-
-
Save rebane2001/48ad3327761b7183dc61178b6f5a3947 to your computer and use it in GitHub Desktop.
This file contains 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
# Simple script to convert trs files to srt. May not be perfect, so edit this script as necessary. | |
# Warning, this script is unsafe to use against untrusted data. | |
# See for more info: https://docs.python.org/3/library/xml.html#xml-vulnerabilities | |
from datetime import timedelta | |
from xml.dom import minidom | |
import srt | |
subs = [] | |
filename = '20200701 - Mänguri telefon (400+ mängu) - ltJACYtJ3IU.mp4.trs' | |
# Import trs file | |
xmldoc = minidom.parse(filename) | |
itemlist = xmldoc.getElementsByTagName('Turn') | |
for s in itemlist: | |
# Start and end times in timedelta seconds | |
startTime = timedelta(seconds=float(s.getAttribute("startTime"))) | |
endTime = timedelta(seconds=float(s.getAttribute("endTime"))) | |
line = "" | |
for e in s.childNodes: | |
# Load text from only textnodes | |
try: | |
line += e.data | |
except AttributeError: | |
pass | |
line = line.strip() | |
subs.append(srt.Subtitle(index=-1, start=startTime, end=endTime, content=line)) | |
# Generate srt | |
srtout = srt.compose(subs, reindex=True, strict=True) | |
# Save to file | |
with open(filename[:-3] + ".srt", "w", encoding="UTF-8") as f: | |
f.write(srtout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment