Skip to content

Commit e1615c9

Browse files
FEAT: Add per-file line translation progress bar and srt_file logging in the Subtitles Translation with DeepL Python Project
1 parent 2259a39 commit e1615c9

File tree

1 file changed

+27
-22
lines changed
  • Subtitles Translation with DeepL

1 file changed

+27
-22
lines changed

Subtitles Translation with DeepL/main.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -260,40 +260,45 @@ def translate_text_block(text_block, translator):
260260
print(f"{BackgroundColors.RED}Translation failed: {e}. Returning original lines.{Style.RESET_ALL}")
261261
return text_block.split("\n") # Return original lines on failure
262262

263-
def translate_srt_lines(lines):
263+
def translate_srt_lines(srt_file, lines):
264264
"""
265265
Translates lines from an SRT file using DeepL API.
266266
Timing and index lines remain unchanged.
267267
268+
:param srt_file: Path to the SRT file (for logging purposes)
268269
:param lines: List of SRT lines
269270
:return: List of translated lines
270271
"""
271272

272-
verbose_output(f"{BackgroundColors.GREEN}Translating SRT lines...{Style.RESET_ALL}") # Output the verbose message
273+
verbose_output(f"{BackgroundColors.GREEN}Translating SRT lines from file: {BackgroundColors.CYAN}{srt_file}{Style.RESET_ALL}") # Output the verbose message
273274

274275
translator = deepl.DeepLClient(auth_key=DEEPL_API_KEY) # Create a DeepL client
275276
translated_lines = [] # List to store translated lines
276277
buffer = [] # Buffer to store text lines for translation
277278

278-
for line in lines: # Iterate through each line in the SRT file
279-
stripped = line.strip() # Remove leading and trailing whitespace
280-
281-
if stripped == "" or stripped.replace(":", "").replace(",", "").isdigit() or "-->" in line: # If line is empty, timing, or index
282-
if buffer: # If buffer contains text to translate
283-
translated = translate_text_block("\n".join(buffer), translator) # Translate buffer
284-
if translated is None: # Defensive check, should never happen
285-
translated = buffer
286-
translated_lines.extend(translated) # Add translated lines
287-
buffer = [] # Clear buffer
288-
translated_lines.append(line.rstrip("\n")) # Keep timing/index/empty line as is
289-
else: # Line is text to be translated
290-
buffer.append(stripped) # Add line to buffer
291-
292-
if buffer: # Translate any remaining text in buffer
293-
translated = translate_text_block("\n".join(buffer), translator)
294-
if translated is None:
295-
translated = buffer
296-
translated_lines.extend(translated)
279+
with tqdm(total=len(lines), desc=f"{BackgroundColors.GREEN}Lines translated in file {BackgroundColors.CYAN}{srt_file}{BackgroundColors.GREEN}", unit="line", ncols=100, leave=False, bar_format=f"{BackgroundColors.CYAN}{{l_bar}}{{bar}}{BackgroundColors.GREEN}{{r_bar}}{Style.RESET_ALL}") as pbar:
280+
for line in lines: # Iterate through each line in the SRT file
281+
stripped = line.strip() # Remove leading and trailing whitespace
282+
283+
if stripped == "" or stripped.replace(":", "").replace(",", "").isdigit() or "-->" in line: # If line is empty, timing, or index
284+
if buffer: # If buffer contains text to translate
285+
translated = translate_text_block("\n".join(buffer), translator) # Translate buffer
286+
if translated is None: # Defensive check, should never happen
287+
translated = buffer # Fallback to original lines
288+
translated_lines.extend(translated) # Add translated lines
289+
buffer = [] # Clear buffer
290+
translated_lines.append(line.rstrip("\n")) # Keep timing/index/empty line as is
291+
else: # Line is text to be translated
292+
buffer.append(stripped) # Add line to buffer
293+
294+
pbar.update(1) # Update progress bar for each line processed
295+
296+
if buffer: # Translate any remaining text in buffer
297+
translated = translate_text_block("\n".join(buffer), translator) # Translate remaining buffer
298+
if translated is None: # Defensive check, should never happen
299+
translated = buffer # Fallback to original lines
300+
translated_lines.extend(translated) # Add translated lines
301+
pbar.update(len(buffer)) # Update progress bar for remaining lines
297302

298303
return translated_lines # Return all translated lines
299304

@@ -391,7 +396,7 @@ def main():
391396
if DESCRIPTIVE_SUBTITLES_REMOVAL: # Remove descriptive subtitles if enabled
392397
srt_lines = remove_descriptive_subtitles(srt_file) # Clean SRT lines
393398

394-
translated_lines = translate_srt_lines(srt_lines) # Translate
399+
translated_lines = translate_srt_lines(srt_file, srt_lines) # Translate
395400

396401
relative_path = srt_file.relative_to(INPUT_DIR).parent # Get relative path
397402
output_subdir = OUTPUT_DIR / relative_path # Create output subdirectory path

0 commit comments

Comments
 (0)