英文字幕确实让人头疼,因此,花了一天专门研究,用python实现了比较简单的翻译代码。仅有几十行
首先加载模块
1
2
3
|
pip uninstall googletrans
git clone https: / / github.com / BoseCorp / py - googletrans.git
cd . / py - googletrans && python setup.py install
|
使用测试
1
2
3
4
5
6
7
8
|
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' ))
|
输出没问题就行
思路:
按行读取文件内容,翻译特定行的字幕即可,非常简单,但是要注意编码问题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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)
|
使用示范:
1
|
python tran.py - - fs "D:\xiaxiaxia\pycharmtext\a.vtt"
|
注意事项
读行,按行处理,然后写入和打开都要注意编码,写入的时候还得再最后加个换行符\n 这样才不会有错误。
代码还是比较简单,感兴趣的可以在io处理和并发上还可以再优化。
总结
到此这篇关于python利用google翻译(翻译字幕文件)的文章就介绍到这了,更多相关python用google翻译字幕文件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/xia_xu_liang/article/details/108694184