From f2a7b0ae688ea75178141e40a47e75e402f188ba Mon Sep 17 00:00:00 2001 From: EAR Date: Tue, 16 Jul 2024 09:18:46 -0500 Subject: [PATCH] Show translation status as progress bar --- srtranslator/ass_file.py | 11 ++++++----- srtranslator/srt_file.py | 7 ++++--- srtranslator/util.py | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 srtranslator/util.py diff --git a/srtranslator/ass_file.py b/srtranslator/ass_file.py index c32dece..d01b8b0 100644 --- a/srtranslator/ass_file.py +++ b/srtranslator/ass_file.py @@ -5,6 +5,7 @@ from typing import List, Generator from .translators.base import Translator +from .util import show_progress class AssFile: @@ -47,7 +48,7 @@ def _load_backup(self): ] def load_from_file(self, input_file): - ass_file = pyass.load(input_file) + ass_file = pyass.load(input_file) ass_file.events = sorted(ass_file.events, key=lambda e: (e.start)) return self._clean_subs_content(ass_file) @@ -108,7 +109,7 @@ def _clean_subs_content(self, subtitles): for sub in subtitles.events: sub.text = cleanr.sub("", sub.text) # No real equivalent in ASS - #sub.text = srt.make_legal_content(sub.content) + # sub.text = srt.make_legal_content(sub.content) sub.text = sub.text.strip() if sub.text == "": @@ -152,12 +153,10 @@ def translate( destination_language (str): Destination language (must be coherent with your translator) source_language (str): Source language (must be coherent with your translator) """ + print("Starting translation") # For each chunk of the file (based on the translator capabilities) for subs_slice in self._get_next_chunk(translator.max_char): - progress = int(100 * self.current_subtitle / len(self.subtitles.events)) - print(f"... Translating {progress} %") - # Put chunk in a single text with break lines text = [sub.text for sub in subs_slice] text = "\n".join(text) @@ -184,6 +183,8 @@ def translate( subs_slice[i].text = translation[i] self.current_subtitle += 1 + show_progress(len(self.subtitles.events), progress=self.current_subtitle) + print(f"... Translation done") def save_backup(self): diff --git a/srtranslator/srt_file.py b/srtranslator/srt_file.py index f4fabcf..b295c18 100644 --- a/srtranslator/srt_file.py +++ b/srtranslator/srt_file.py @@ -6,6 +6,7 @@ from typing import List, Generator from .translators.base import Translator +from .util import show_progress class SrtFile: @@ -167,12 +168,10 @@ def translate( destination_language (str): Destination language (must be coherent with your translator) source_language (str): Source language (must be coherent with your translator) """ + print("Starting translation") # For each chunk of the file (based on the translator capabilities) for subs_slice in self._get_next_chunk(translator.max_char): - progress = int(100 * self.current_subtitle / len(self.subtitles)) - print(f"... Translating {progress} %") - # Put chunk in a single text with break lines text = [sub.content for sub in subs_slice] text = "\n".join(text) @@ -188,6 +187,8 @@ def translate( subs_slice[i].content = translation[i] self.current_subtitle += 1 + show_progress(len(self.subtitles), progress=self.current_subtitle) + print(f"... Translation done") def save_backup(self): diff --git a/srtranslator/util.py b/srtranslator/util.py new file mode 100644 index 0000000..0440a20 --- /dev/null +++ b/srtranslator/util.py @@ -0,0 +1,16 @@ +import sys + + +def show_progress(total: int, progress: int): + """Displays or updates a console progress bar""" + + barLength, status = 20, "" + progress = float(progress) / float(total) + if progress >= 1.0: + progress, status = 1, "\r\n" + block = int(round(barLength * progress)) + text = "\r[{}] {:.0f}% {}".format( + "#" * block + "-" * (barLength - block), round(progress * 100, 0), status + ) + sys.stdout.write(text) + sys.stdout.flush()