Python uses Google Translate API to translate subtitle files
English subtitles are really a headache, so I spent a day studying it and implemented a relatively simple translation code with python. Only a few dozen lines
Load the module first
pip uninstall googletrans
git clone https://github.com/BoseCorp/py-googletrans.git
cd ./py-googletrans && python setup.py install
Use test
from googletrans import Translator
# 实例化
translator = Translator(service_urls=['translate.google.cn'])
content = 'Today is a gooday'
print(translator.translate(content, dest='zh-CN').text.encode('utf-8').decode('utf-8'))
The output is fine
Idea:
Read the content of the file by line and translate the subtitles of a specific line. It is very simple, but you must pay attention to the encoding problem
from googletrans import Translator
# encoding=utf-8
import argparse
def main(args):
# 实例化
translator = Translator(service_urls=['translate.google.cn'])
f = open(args.fs, 'r+', encoding="utf-8")
flist = f.readlines()
f = open(args.fs, 'w+', encoding="utf-8")
f.writelines(flist)
for x in range(4, len(flist)):
if x % 4 == 0:
print(x)
flist[x] = translator.translate(flist[x], dest='zh-CN').text.encode('utf-8').decode('utf-8') + "\n"
print(translator.translate(flist[x], dest='zh-CN').text.encode('utf-8').decode('utf-8'))
f = open(args.fs, 'w+', encoding="utf-8")
f.writelines(flist)
if __name__ == '__main__':
parser = argparse.ArgumentParser(usage="it's usage tip.", description="help info.")
parser.add_argument("--fs", type=str, required=True, help="the fileloacation")
args = parser.parse_args()
main(args)
Use demonstration:
python tran.py --fs "D:\xiaxiaxia\pycharmtext\a.vtt"
Matters needing attention
Read lines, process them by line, and then pay attention to the encoding when writing and opening. When writing, you must add a newline character \n at the end so that there will be no errors.
The code is still relatively simple, and interested parties can be optimized in io processing and concurrency.