| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- """
- VTT chunk reassembly logic.
- """
- import os
- from typing import List
- from pathlib import Path
- from vtt_utils import VTTFile, Subtitle
- class VTTReassembler:
- """Reassembles translated VTT chunks into a single file."""
- @staticmethod
- def reassemble(chunks: List[VTTFile], original_filename: str,
- output_dir: str) -> str:
- """
- Reassemble translated chunks into a single VTT file.
- Args:
- chunks: List of translated VTTFile chunks
- original_filename: Original filename (without path)
- output_dir: Directory to save the reassembled file
- Returns:
- Path to the output file
- """
- # Combine all subtitles from all chunks
- all_subtitles: List[Subtitle] = []
- for chunk in chunks:
- all_subtitles.extend(chunk.subtitles)
- # Create new VTT file
- final_vtt = VTTFile.__new__(VTTFile)
- final_vtt.filepath = ""
- final_vtt.subtitles = all_subtitles
- # Generate output filename
- name_without_ext = os.path.splitext(original_filename)[0]
- output_filename = f"{name_without_ext}-EN.vtt"
- output_path = os.path.join(output_dir, output_filename)
- # Save to disk
- final_vtt.save(output_path)
- return output_path
- @staticmethod
- def get_output_filename(original_filename: str) -> str:
- """
- Generate output filename from original filename.
- Args:
- original_filename: Original VTT filename
- Returns:
- Output filename with '-EN' suffix
- """
- name_without_ext = os.path.splitext(original_filename)[0]
- return f"{name_without_ext}-EN.vtt"
|