增加srt翻译支持

This commit is contained in:
xunbu
2025-08-08 19:13:59 +08:00
parent a7d1a17166
commit 8e192e63e4
12 changed files with 363 additions and 9 deletions

View File

@@ -70,4 +70,12 @@ class DocxExportable(Protocol[T_ExporterConfig]):
...
def save_as_docx(self, name: str, output_dir: Path | str, config: T_ExporterConfig | None = None) -> Self:
...
@runtime_checkable
class SrtExportable(Protocol[T_ExporterConfig]):
def export_to_srt(self, config: T_ExporterConfig | None = None) -> str:
...
def save_as_srt(self, name: str, output_dir: Path | str, config: T_ExporterConfig | None = None) -> Self:
...

View File

@@ -0,0 +1,66 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Self
from docutranslate.exporter.base import ExporterConfig
from docutranslate.exporter.srt.srt2html_exporter import Srt2HTMLExporterConfig, Srt2HTMLExporter
from docutranslate.exporter.srt.srt2srt_exporter import Srt2SrtExporter
from docutranslate.ir.document import Document
from docutranslate.translator.ai_translator.srt_translator import SrtTranslatorConfig, SrtTranslator
from docutranslate.workflow.base import Workflow, WorkflowConfig
from docutranslate.workflow.interfaces import HTMLExportable, SrtExportable
@dataclass(kw_only=True)
class SrtWorkflowConfig(WorkflowConfig):
translator_config: SrtTranslatorConfig
html_exporter_config: Srt2HTMLExporterConfig
class SrtWorkflow(Workflow[SrtWorkflowConfig, Document, Document], HTMLExportable[Srt2HTMLExporterConfig],
SrtExportable[ExporterConfig]):
def __init__(self, config: SrtWorkflowConfig):
super().__init__(config=config)
if config.logger:
for sub_config in [self.config.translator_config]:
if sub_config:
sub_config.logger = config.logger
def _pre_translate(self,document_original:Document):
document = document_original.copy()
translate_config = self.config.translator_config
translator = SrtTranslator(translate_config)
return document,translator
def translate(self) -> Self:
document, translator=self._pre_translate(self.document_original)
translator.translate(document)
self.document_translated = document
return self
async def translate_async(self) -> Self:
document, translator = self._pre_translate(self.document_original)
await translator.translate_async(document)
self.document_translated = document
return self
def export_to_html(self, config: Srt2HTMLExporterConfig = None) -> str:
config = config or self.config.html_exporter_config
docu = self._export(Srt2HTMLExporter(config))
return docu.content.decode()
def export_to_srt(self, _: ExporterConfig | None = None) -> str:
docu = self._export(Srt2SrtExporter())
return docu.content.decode()
def save_as_html(self, name: str = None, output_dir: Path | str = "./output",
config: Srt2HTMLExporterConfig | None = None) -> Self:
config = config or self.config.html_exporter_config
self._save(exporter=Srt2HTMLExporter(config), name=name, output_dir=output_dir)
return self
def save_as_srt(self, name: str = None, output_dir: Path | str = "./output",
_: ExporterConfig | None = None) -> Self:
self._save(exporter=Srt2SrtExporter(), name=name, output_dir=output_dir)
return self