翻译 SRT 字幕文件

832 阅读1分钟

依赖

pip install srt
pip install pygtrans
"""字幕翻译"""

import srt
from pygtrans import Translate

if __name__ == '__main__':
    titles = list(srt.parse(open('test-subtitle.srt', encoding='utf-8').read()))
    contents = [title.content for title in titles]

    client = Translate(proxies={'https': 'socks5://localhost:10808'})
    zh_words = client.translate(contents)

    for i, zh_word in enumerate(zh_words):
        titles[i].content = zh_word.translatedText

    with open('test-subtitle-zh.srt', 'w', encoding='utf-8') as f:
        f.write(srt.compose(titles))