Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show translation status as progress bar #91

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions srtranslator/ass_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import List, Generator

from .translators.base import Translator
from .util import show_progress


class AssFile:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 == "":
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions srtranslator/srt_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import List, Generator

from .translators.base import Translator
from .util import show_progress


class SrtFile:
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions srtranslator/util.py
Original file line number Diff line number Diff line change
@@ -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()