Skip to content

Instantly share code, notes, and snippets.

@rebane2001
Created May 21, 2021 07:54
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 rebane2001/48ad3327761b7183dc61178b6f5a3947 to your computer and use it in GitHub Desktop.
Save rebane2001/48ad3327761b7183dc61178b6f5a3947 to your computer and use it in GitHub Desktop.
# 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