Last active
June 18, 2020 10:12
-
-
Save jacob414/a6432878c80e4b3adb2b4a80ecb162ef to your computer and use it in GitHub Desktop.
Quick and dirty .srt file translation using cheeseshop packages translate, funcy
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
import sys | |
import srt | |
from translate import Translator | |
import funcy as fy | |
codec = 'ISO-8859-1' | |
tr = Translator('sv', email='[email protected]') | |
inf = sys.argv[-1] | |
outf = f"{inf.split('.srt')[0]}-sv.srt" | |
translated = () | |
with open(sys.argv[-1], encoding=codec) as fp: | |
texts = srt.parse(fp.read()) | |
try: | |
out = open(outf, encoding=codec) | |
translated = srt.parse(out.read()) | |
resume = fy.last(translated).index | |
out.close() | |
out = open(outf, 'a') | |
except IOError: | |
out = open(outf, 'w', encoding=codec) | |
resume = 0 | |
for text in texts: | |
if text.index > resume: | |
sv = tr.translate(text.content) | |
print(f"TEXT {text.index}, {sv}") | |
out.write(text.to_srt().replace(text.content, sv)) | |
resume += 1 | |
print(f"{inf} translation done.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment