openlrc icon indicating copy to clipboard operation
openlrc copied to clipboard

能否直接翻译 `srt` 字幕文件?

Open piwawa opened this issue 1 year ago • 1 comments

已经有 srt 字幕,需要翻译成中文或者双语,能否添加一个 api 接收3个参数,源语言、目标语言、是否双语,实现该功能?

piwawa avatar May 15 '24 09:05 piwawa

It can be implemented by directly calling the components of openlrc, and there isn't much code:

from copy import deepcopy
from pathlib import Path

from openlrc.opt import SubtitleOptimizer
from openlrc.prompter import BaseTranslatePrompter
from openlrc.subtitle import Subtitle, BilingualSubtitle
from openlrc.translate import LLMTranslator

if __name__ == '__main__':
    # Parameters
    src_lang = 'en'
    target_lang = 'zh'
    chatbot_model = 'gpt-3.5-turbo'
    proxy = 'http:// 127.0.0.1:7890'
    src_subtitle_path = Path('C:/Users/10578/Desktop/openlrc_test/test_video_nontrans.srt')
    intermediate_path = src_subtitle_path.parent / Path('intermediate.json')
    bilingual = True

    # Translate
    src_subtitle = Subtitle.from_file(src_subtitle_path)
    prompter = BaseTranslatePrompter(src_lang, target_lang)
    translator = LLMTranslator(chatbot_model, proxy=proxy)
    target_texts = translator.translate(src_subtitle.texts, src_lang=src_lang, target_lang=target_lang,
                                        title=src_subtitle_path.stem, compare_path=intermediate_path)
    target_subtitle = deepcopy(src_subtitle)
    target_subtitle.set_texts(target_texts, lang=target_lang)

    # Optimize the final subtitle
    optimized_subtitle = deepcopy(target_subtitle)
    optimizer = SubtitleOptimizer(optimized_subtitle)
    optimizer.perform_all(extend_time=True)

    # Save into xxx-zh.srt
    optimized_subtitle.filename = src_subtitle_path.parent / Path(
        f'{src_subtitle_path.stem}-{target_lang}{src_subtitle_path.suffix}')
    optimized_subtitle.to_srt()

    if bilingual:
        # xxx-bilingual.srt
        bilingual_filename = src_subtitle_path.parent / Path(
            f'{src_subtitle_path.stem}-bilingual{src_subtitle_path.suffix}')
        bilingual_subtitle = BilingualSubtitle(src_subtitle, target_subtitle, filename=bilingual_filename)
        bilingual_subtitle.to_srt()

zh-plus avatar May 16 '24 03:05 zh-plus