-
-
Save hugoleodev/ce32c4394813566a5e1eb77c60fbb2d3 to your computer and use it in GitHub Desktop.
translate a srt file with google translate
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import sys,json,urllib | |
from pysrt import SubRipFile, SubRipItem | |
url_string = 'http://ajax.googleapis.com/ajax/services/language/' | |
def detect(text): | |
query = dict(v='1.0', q=text) | |
file = urllib.urlopen(url_string+'detect?%s' % urllib.urlencode(query)) | |
result = json.loads(file.read()) | |
return result['responseData']['language'] if result['responseData'] else None | |
def translate(text, input_language, output_language): | |
source = detect(text) | |
target = [input_language, output_language][source == input_language] | |
query = dict(v='1.0', q=text,langpair = '%s|%s' % (source, target)) | |
file = urllib.urlopen(url_string+'translate?%s' % urllib.urlencode(query)) | |
result = json.loads(file.read()) | |
return result['responseData']['translatedText'] if result['responseData'] else None | |
def translate_srt_file(input_file, output_file, input_language, output_language): | |
"""translate a srt file from a language to another""" | |
print 'processing file', input_file | |
subs = SubRipFile.open(input_file) | |
for sentence in subs: | |
sentence.text = translate(sentence.text, input_language, output_language) + '\n' | |
subs.save(output_file, 'utf-8') | |
def main(): | |
if len(sys.argv) < 4: | |
info = ''' | |
translate a subtitle file from a language to another. | |
Most of the code to use google translate API was taken there : | |
https://github.com/zhendi/google-translate-cli | |
usage: | |
translate-srt.py input_file output_file input_language output_language | |
example: | |
translate-srt.py ./titanic-en.srt ./titanic-fr.srt en fr | |
''' | |
print info | |
else: | |
translate_srt_file(*sys.argv[-4:]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment