Created
October 28, 2020 23:47
-
-
Save kamui-fin/6ec9bfcf7f6dcf3bf882b60752953048 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 os, sys, re | |
from glob import glob | |
import pathlib | |
class substation: | |
def __init__(self, filepath): | |
self.filepath = filepath | |
self.raw_text = self.get_text() | |
self.full_info = {} | |
def get_text(self): | |
return pathlib.Path(self.filepath).read_text(encoding="utf8") | |
def analyze(self): | |
toremove = re.compile(r"\{\\fad\(\d+,\d+\)\\.*\}") | |
dialogRe = re.compile("Dialogue: \d,(\d:\d{2}:\d{2}.\d{2}),(\d:\d{2}:\d{2}.\d{2}),.*,.*.*,.*,.*,.*,(.*)") | |
zerotime = re.compile("0:00:00\.00") | |
dialogs = re.findall(dialogRe, re.sub(toremove,"",self.raw_text)) | |
dialogs = sorted(list(filter(lambda x: x[2], | |
dialogs))) | |
self.format_subs(dialogs) | |
def format_subs(self,dialogs): | |
self.full_info = {start.replace(".", ",") + " --> " + | |
end.replace(".", ","): text for start, end, text in dialogs} | |
def translate(self, outputDir): | |
self.analyze() | |
with open(os.path.join(outputDir,self.filepath[:-4] + ".srt"),encoding="utf8",mode="w") as writer: | |
entry = 1 | |
for timestamp, text in self.full_info.items(): | |
sub = f"{entry}\n{timestamp}\n{text}\n\n" | |
writer.write(sub) | |
entry += 1 | |
if __name__ == "__main__": | |
for subsalpha in glob(sys.argv[1] + "/*.ass"): | |
subs = substation(subsalpha) | |
subs.translate(sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment