reassembler.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. VTT chunk reassembly logic.
  3. """
  4. import os
  5. from typing import List
  6. from pathlib import Path
  7. from vtt_utils import VTTFile, Subtitle
  8. class VTTReassembler:
  9. """Reassembles translated VTT chunks into a single file."""
  10. @staticmethod
  11. def reassemble(chunks: List[VTTFile], original_filename: str,
  12. output_dir: str) -> str:
  13. """
  14. Reassemble translated chunks into a single VTT file.
  15. Args:
  16. chunks: List of translated VTTFile chunks
  17. original_filename: Original filename (without path)
  18. output_dir: Directory to save the reassembled file
  19. Returns:
  20. Path to the output file
  21. """
  22. # Combine all subtitles from all chunks
  23. all_subtitles: List[Subtitle] = []
  24. for chunk in chunks:
  25. all_subtitles.extend(chunk.subtitles)
  26. # Create new VTT file
  27. final_vtt = VTTFile.__new__(VTTFile)
  28. final_vtt.filepath = ""
  29. final_vtt.subtitles = all_subtitles
  30. # Generate output filename
  31. name_without_ext = os.path.splitext(original_filename)[0]
  32. output_filename = f"{name_without_ext}-EN.vtt"
  33. output_path = os.path.join(output_dir, output_filename)
  34. # Save to disk
  35. final_vtt.save(output_path)
  36. return output_path
  37. @staticmethod
  38. def get_output_filename(original_filename: str) -> str:
  39. """
  40. Generate output filename from original filename.
  41. Args:
  42. original_filename: Original VTT filename
  43. Returns:
  44. Output filename with '-EN' suffix
  45. """
  46. name_without_ext = os.path.splitext(original_filename)[0]
  47. return f"{name_without_ext}-EN.vtt"