Skip to content

Commit 8488993

Browse files
REFACTOR: Improving console outputs from the Subtitles Translations with DeepL Python Project
1 parent 5383d3c commit 8488993

File tree

1 file changed

+21
-18
lines changed
  • Subtitles Translations with DeepL

1 file changed

+21
-18
lines changed

Subtitles Translations with DeepL/main.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,11 @@ def translate_text_block(text_block, translator):
245245

246246
remaining_chars = get_remaining_characters(translator) # Check remaining characters
247247

248-
if remaining_chars is not None and len(text_block) > remaining_chars: # Exceeding limit
249-
print(f"{BackgroundColors.YELLOW}Warning: Translation limit would be exceeded. Current block size: {BackgroundColors.CYAN}{len(text_block)}{BackgroundColors.YELLOW}. Current remaining characters: {BackgroundColors.CYAN}{remaining_chars}{BackgroundColors.YELLOW}. Skipping translation for this block.{Style.RESET_ALL}")
250-
return text_block.split("\n") # Return original lines
248+
if remaining_chars is not None: # If there is a limit on remaining characters
249+
print(f"{BackgroundColors.GREEN}Current remaining characters in DeepL API: {BackgroundColors.CYAN}{remaining_chars}{BackgroundColors.GREEN} characters{Style.RESET_ALL}") # Output remaining characters
250+
if len(text_block) > remaining_chars: # Exceeding limit
251+
print(f"{BackgroundColors.YELLOW}Warning: Translation limit would be exceeded. Current block size: {BackgroundColors.CYAN}{len(text_block)}{BackgroundColors.YELLOW}. Exceeding limit by: {BackgroundColors.CYAN}{len(text_block) - remaining_chars}{BackgroundColors.YELLOW} characters. Skipping translation for this block.{Style.RESET_ALL}") # Output warning message
252+
return text_block.split("\n") # Return original lines
251253
else: # Perform translation
252254
result = translator.translate_text(text_block, source_lang="EN", target_lang="PT-BR") # Translate text block
253255
return result.text.split("\n") # Return translated lines
@@ -368,22 +370,23 @@ def main():
368370
print(f"No .srt files found in directory: {INPUT_DIR}") # Output message
369371
return # Exit the program
370372

371-
for srt_file in tqdm(srt_files, desc=f"{BackgroundColors.GREEN}Translating SRT files{Style.RESET_ALL}", unit="file"): # Iterate through each SRT file with progress bar
372-
tqdm.write(f"{BackgroundColors.GREEN}Processing file: {BackgroundColors.CYAN}{srt_file}{Style.RESET_ALL}") # Show current file
373+
with tqdm(srt_files, desc=f"{BackgroundColors.GREEN}Translating SRT files", unit="file") as progress_bar: # Progress bar for SRT files
374+
for srt_file in progress_bar: # Iterate through each SRT file
375+
progress_bar.set_description(f"{BackgroundColors.GREEN}Processing: {BackgroundColors.CYAN}{srt_file.name}{BackgroundColors.GREEN}")
373376

374-
srt_lines = read_srt(srt_file) # Read SRT
375-
376-
if DESCRIPTIVE_SUBTITLES_REMOVAL: # Remove descriptive subtitles if enabled
377-
srt_lines = remove_descriptive_subtitles(srt_file) # Clean SRT lines
378-
379-
translated_lines = translate_srt_lines(srt_lines) # Translate
380-
381-
relative_path = srt_file.relative_to(INPUT_DIR).parent # Get relative path
382-
output_subdir = OUTPUT_DIR / relative_path # Create output subdirectory path
383-
output_subdir.mkdir(parents=True, exist_ok=True) # Ensure output subdir exists
384-
385-
output_file = output_subdir / f"{srt_file.stem}_ptBR.srt" # Build output file path
386-
save_srt(translated_lines, output_file) # Save translated SRT
377+
srt_lines = read_srt(srt_file) # Read SRT
378+
379+
if DESCRIPTIVE_SUBTITLES_REMOVAL: # Remove descriptive subtitles if enabled
380+
srt_lines = remove_descriptive_subtitles(srt_file) # Clean SRT lines
381+
382+
translated_lines = translate_srt_lines(srt_lines) # Translate
383+
384+
relative_path = srt_file.relative_to(INPUT_DIR).parent # Get relative path
385+
output_subdir = OUTPUT_DIR / relative_path # Create output subdirectory path
386+
output_subdir.mkdir(parents=True, exist_ok=True) # Ensure output subdir exists
387+
388+
output_file = output_subdir / f"{srt_file.stem}_ptBR.srt" # Build output file path
389+
save_srt(translated_lines, output_file) # Save translated SRT
387390

388391
finish_time = datetime.datetime.now() # Get the finish time of the program
389392
print(f"{BackgroundColors.GREEN}Start time: {BackgroundColors.CYAN}{start_time.strftime('%d/%m/%Y - %H:%M:%S')}\n{BackgroundColors.GREEN}Finish time: {BackgroundColors.CYAN}{finish_time.strftime('%d/%m/%Y - %H:%M:%S')}\n{BackgroundColors.GREEN}Execution time: {BackgroundColors.CYAN}{calculate_execution_time(start_time, finish_time)}{Style.RESET_ALL}") # Output start, finish, and execution times

0 commit comments

Comments
 (0)