Skip to content

Instantly share code, notes, and snippets.

@jacob414
Last active June 18, 2020 10:12
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 jacob414/a6432878c80e4b3adb2b4a80ecb162ef to your computer and use it in GitHub Desktop.
Save jacob414/a6432878c80e4b3adb2b4a80ecb162ef to your computer and use it in GitHub Desktop.
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