Last active
June 14, 2019 21:49
-
-
Save m4p/b8f449f2eec6283b7ad77f58c29d3ed8 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
import pysubs2 | |
import re | |
import sys | |
from xml.sax.saxutils import escape | |
print (sys.argv[1]) | |
subs = pysubs2.load(sys.argv[1], encoding="utf-8") | |
xml = '' | |
colors = [] | |
for line in subs: | |
newline = escape(line.text) | |
matches = re.findall(r"\{\\c&H(......)&\}", newline) | |
for match in matches: | |
if match not in colors: | |
colors.append(match) | |
newline = re.sub(r"\{\\c&H%s&\}" % matches[0], "<s p=\"%i\">" % colors.index(matches[0]), newline) | |
newline = re.sub(r"\{\\c&H%s&\}" % matches[1], "</s><s p=\"%i\">" % colors.index(matches[1]), newline) | |
newline = re.sub(r"$", "\n</s>", newline) | |
line.text = newline | |
head = """<?xml version="1.0" encoding="utf-8" standalone="yes"?> | |
<timedtext format="3"> | |
<head> | |
<wp id="0" ap="6" ah="0" av="100"/> | |
<ws id="0" ju="0" pd="0" sd="0" /> | |
""" | |
xml = xml + head | |
for color in colors: | |
coldecl = " <pen id=\"%i\" sz=\"75\" fc=\"#%s\" fo=\"255\" bo=\"0\" et=\"3\" ec=\"#000000\"/>\n" % (colors.index(color), color) | |
xml = xml + coldecl | |
middle = """ </head> | |
<body> | |
""" | |
xml = xml + middle | |
n = 2 | |
lines = [] | |
events = [] | |
for line in subs: | |
lines.append(line) | |
lines = lines[-n:] | |
events.append(lines) | |
for i in range(0, len(events)-1): | |
duration = 3000 | |
if (i < len(events)-1): | |
duration = events[i+1][-1].start - events[i][-1].start | |
eventheader = " <p t=\"%i\" d=\"%i\" wp=\"0\" ws=\"0\">\n" % (events[i][-1].start,duration) | |
xml = xml + eventheader | |
for j in range(0, len(events[i])): | |
print(j) | |
line = events[i][j] | |
if (j<len(events[i])-1): | |
eventstring = line.text | |
xml = xml + (eventstring) | |
else: | |
eventstring = line.text | |
eventstring = re.sub(r"\n</s>", "</s>", eventstring) | |
xml = xml + (eventstring) | |
eventfooter = " </p>\n" | |
xml = xml + (eventfooter) | |
footer = """</body></timedtext>""" | |
xml = xml + footer | |
text_file = open(sys.argv[1]+".xml", "w") | |
text_file.write(xml) | |
text_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Converts twitch chat logs downloaded with tcd to a colored subtitle format ready for youtube archival.