From 425a97f98026d154651feac9cb0884a1f8af7cc5 Mon Sep 17 00:00:00 2001 From: Mikhail Osipov Date: Wed, 6 Nov 2013 02:56:01 +0400 Subject: show progress of encoding --- TODO | 2 +- cutter.py | 2 ++ cutter/formats/command.py | 18 ++++++------- cutter/formats/decoder.py | 3 +++ cutter/formats/encoder.py | 9 +++++-- cutter/splitter.py | 69 ++++++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 88 insertions(+), 15 deletions(-) diff --git a/TODO b/TODO index 4c17216..19a4a48 100644 --- a/TODO +++ b/TODO @@ -11,4 +11,4 @@ OK 10. create default config on startup OK 11. make cchardet optional OK 12. manually convert files OK 13. use python wave module -14. show progress of encoding +OK 14. show progress of encoding diff --git a/cutter.py b/cutter.py index 0cc5b64..cd86c50 100755 --- a/cutter.py +++ b/cutter.py @@ -248,6 +248,8 @@ def process_options(opt): opt.tracks = tracks + opt.show_progress = os.isatty(sys.stdout.fileno()) + return True def find_cuefile(path): diff --git a/cutter/formats/command.py b/cutter/formats/command.py index 3641eae..6be748d 100644 --- a/cutter/formats/command.py +++ b/cutter/formats/command.py @@ -30,15 +30,6 @@ class Command: self.status = "not started" self.status_msg = err.strerror - def __getattr__(self, attr): - if self.proc is None: - raise CommandError("command not started") - - if attr not in ("stdin", "stdout", "stderr"): - raise CommandError("unknown attribute '%s'", attr) - - return getattr(self.proc, attr) - def ready(self): return self.proc is not None @@ -60,3 +51,12 @@ class Command: self.status = strsignal(-self.status) self.proc = None + + def __getattr__(self, attr): + if self.proc is None: + raise CommandError("command not started") + + if attr not in ("stdin", "stdout", "stderr"): + raise CommandError("unknown attribute '%s'" % attr) + + return getattr(self.proc, attr) diff --git a/cutter/formats/decoder.py b/cutter/formats/decoder.py index b57591d..fb2ad3c 100644 --- a/cutter/formats/decoder.py +++ b/cutter/formats/decoder.py @@ -28,6 +28,9 @@ class Decoder: params[3] = self.nframes return params + def size(self): + return self.nframes * self.bytes_per_frame + def read(self, maxframes=None): if self.nread >= self.nframes: return [] diff --git a/cutter/formats/encoder.py b/cutter/formats/encoder.py index 8689de8..43670b5 100644 --- a/cutter/formats/encoder.py +++ b/cutter/formats/encoder.py @@ -74,16 +74,21 @@ class Encoder: def ready(self): return self.proc.ready() - def process(self): + def process(self, progress): if not self.proc.ready(): return + progress.init(self.reader.size()) + while True: data = self.reader.read(self.FRAME_BUFFER_SIZE) if not len(data): break self.writer.writeframesraw(data) + progress.update(len(data)) + + progress.finish() def get_command(self): return self.command @@ -92,7 +97,7 @@ class Encoder: return self.proc.get_status() def close(self): - if self.proc and self.proc.ready(): + if self.proc is not None and self.proc.ready(): self.writer.close() self.stream.close() self.proc.close() diff --git a/cutter/splitter.py b/cutter/splitter.py index 332ce50..4446bcc 100644 --- a/cutter/splitter.py +++ b/cutter/splitter.py @@ -74,6 +74,65 @@ class StreamInfo: return stream.info() +class Progress: + def __init__(self, message): + self.progress_shown = False + self.message = message + + def erase(self, n): + if not self.progress_shown: + self.last_length = n + self.progress_shown = True + return 0 + + prev, self.last_length = self.last_length, n + return prev + + def show(self, msg): + printf("%s", "\b" * self.erase(len(msg)) + msg) + + def do_print(self): + if self.percent > self.last_percent: + self.show("%3d%% " % self.percent) + self.last_percent = self.percent + + def clear(self): + if self.last_length: + self.show(" " * self.last_length) + + def init(self, total): + self.total = total + self.current = 0 + self.percent = 0 + self.last_percent = -1 + self.last_length = 0 + + self.do_print() + + def update(self, value): + self.current += value + percent = 100 * self.current // self.total + self.percent = min(max(percent, 0), 100) + + self.do_print() + + def finish(self): + n = max(self.last_length - len(self.message), 0) + self.show(self.message + " " * n + "\n") + +class DummyProgress: + def __init__(self, message): + self.message = message + + def init(self, total): + pass + + def update(self, value): + pass + + def finish(self): + printf("%s\n", self.message) + class Splitter: EXT = ["ape", "flac", "wv"] @@ -327,6 +386,12 @@ class Splitter: return track.end - track.begin + def progress(self, message): + if self.opt.show_progress: + return Progress(message) + + return DummyProgress(message) + def split_file(self, file): stream = self.open_decode(file.path) if not stream: @@ -364,11 +429,9 @@ class Splitter: printf("FAILED\n") sys.exit(1) - out.process() + out.process(self.progress("OK")) out.close() - printf("OK\n") - self.tag(track, path) stream.close() -- cgit v1.2.3-70-g09d2