Skip to content

Instantly share code, notes, and snippets.

@jacob414

jacob414/trans.py

Last active Jun 18, 2020
Embed
What would you like to do?
Quick and dirty .srt file translation using cheeseshop packages translate, funcy
import sys
import srt
from translate import Translator
import funcy as fy
codec = 'ISO-8859-1'
tr = Translator('sv', email='your.adress@provider.x')
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