Guest User

combineSRT

a guest
Aug 8th, 2026
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. import glob
  2. import json
  3. import os
  4. import re
  5.  
  6.  
  7. def load_translated_texts(txt_folder_path, txt_prefix="translate_part"):
  8. """Reads all translated .txt chunk files and extracts {number: translated_text}."""
  9. translations = {}
  10.  
  11. # Find all text files matching the prefix (e.g., translate_part_1.txt, translate_part_2.txt)
  12. pattern = os.path.join(txt_folder_path, f"{txt_prefix}_*.txt")
  13. file_list = sorted(
  14. glob.glob(pattern),
  15. key=lambda x: [
  16. int(c) if c.isdigit() else c for c in re.split(r"(\d+)", x)
  17. ],
  18. )
  19.  
  20. if not file_list:
  21. print(
  22. f"Warning: No files found matching '{pattern}'. Checking current directory instead."
  23. )
  24.  
  25. # Regex to match "123. Translated text goes here..."
  26. line_pattern = re.compile(r"^(\d+)\.\s*(.*)$")
  27.  
  28. for file_path in file_list:
  29. with open(file_path, "r", encoding="utf-8") as f:
  30. for line in f:
  31. line = line.strip()
  32. if not line:
  33. continue
  34.  
  35. match = line_pattern.match(line)
  36. if match:
  37. sub_number = int(match.group(1))
  38. translated_text = match.group(2).strip()
  39. translations[sub_number] = translated_text
  40.  
  41. return translations
  42.  
  43.  
  44. def recombine_translated_srt(
  45. json_path="full_subtitles.json",
  46. txt_folder_path=".",
  47. txt_prefix="translate_part",
  48. output_srt_path="translated_output.srt",
  49. ):
  50. """Combines translated text chunks back with the JSON metadata to export a final SRT file."""
  51. # 1. Load original JSON data (with capped timestamps)
  52. with open(json_path, "r", encoding="utf-8") as f:
  53. json_data = json.load(f)
  54.  
  55. # 2. Parse all translated text chunks into a map
  56. translations = load_translated_texts(txt_folder_path, txt_prefix)
  57. print(
  58. f" Loaded {len(translations)} translated lines from text files."
  59. )
  60.  
  61. srt_blocks = []
  62. missing_count = 0
  63.  
  64. # 3. Match translations with JSON metadata
  65. for entry in json_data:
  66. sub_number = entry["number"]
  67. start_time = entry["start_time"]
  68. end_time = entry["end_time"]
  69.  
  70. # Use translated text if available; fall back to original text if missing
  71. if sub_number in translations:
  72. final_text = translations[sub_number]
  73. else:
  74. final_text = entry["text"]
  75. missing_count += 1
  76.  
  77. # Format as standard SRT block
  78. srt_block = f"{sub_number}\n{start_time} --> {end_time}\n{final_text}\n"
  79. srt_blocks.append(srt_block)
  80.  
  81. # 4. Write to finalized .srt file
  82. with open(output_srt_path, "w", encoding="utf-8") as f:
  83. f.write("\n".join(srt_blocks))
  84.  
  85. if missing_count > 0:
  86. print(
  87. f"⚠️ Notice: {missing_count} subtitles were missing from translated text files and used original text."
  88. )
  89.  
  90. print(f" Successfully generated final SRT file -> {output_srt_path}")
  91.  
  92.  
  93. # --- Example Usage ---
  94. if __name__ == "__main__":
  95. recombine_translated_srt(
  96. json_path="full_subtitles.json",
  97. txt_folder_path=".", # Path where your translated .txt files live
  98. txt_prefix="translate_part", # Matches "translate_part_1.txt", etc.
  99. output_srt_path="translated_movie.srt",
  100. )
Advertisement
Add Comment
Please, Sign In to add comment