(编辑:jimmy 日期: 2024/11/13 浏览:2)
英文字幕确实让人头疼,因此,花了一天专门研究,用python实现了比较简单的翻译代码。仅有几十行
首先加载模块
pip uninstall googletrans git clone https://github.com/BoseCorp/py-googletrans.git cd ./py-googletrans && python setup.py install
使用测试
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'))
输出没问题就行
思路:
按行读取文件内容,翻译特定行的字幕即可,非常简单,但是要注意编码问题
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)
使用示范:
python tran.py --fs "D:\xiaxiaxia\pycharmtext\a.vtt"
注意事项
读行,按行处理,然后写入和打开都要注意编码,写入的时候还得再最后加个换行符\n 这样才不会有错误。
代码还是比较简单,感兴趣的可以在io处理和并发上还可以再优化。
总结