Skip to content

Commit

Permalink
Use rich.progress for all the progress bars instead of tqdm (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
fireattack authored Sep 3, 2024
1 parent 61836cb commit 0881411
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
58 changes: 26 additions & 32 deletions nndownload/ffmpeg_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import re
import subprocess
import warnings
from datetime import timedelta, datetime
from typing import AnyStr, List

import ffmpeg
from tqdm import TqdmExperimentalWarning
from tqdm.rich import tqdm_rich

warnings.filterwarnings("ignore", category=TqdmExperimentalWarning)
from rich.progress import Progress


class FfmpegDLException(Exception):
Expand Down Expand Up @@ -63,30 +59,28 @@ def load_subprocess(self):
)

def convert(self, name: AnyStr, duration: float):
"""Perform an ffmpeg conversion while printing progress using tqdm."""

progress = tqdm_rich(desc=name, unit="sec", colour="green", total=duration)

self.load_subprocess()

stdout_line = None
prev_line = None
while True:
if self.proc.stdout is None:
continue
if stdout_line:
prev_line = stdout_line
stdout_line = self.proc.stdout.readline().decode("utf-8", errors="replace").strip()
out_time_data = self.REGEX_OUT_TIME.search(stdout_line)
if out_time_data is not None:
out_time = self.get_timedelta(out_time_data.group(1))
progress.update(out_time.total_seconds() - progress.n)
continue
if not stdout_line and self.proc.poll() is not None:
progress.refresh()
progress.close()
exit_code = self.proc.poll()
if exit_code:
raise FfmpegDLException(prev_line)
else:
break
"""Perform an ffmpeg conversion while printing progress using rich.progress."""

with Progress() as progress:
task = progress.add_task(name, total=duration)
self.load_subprocess()

stdout_line = None
prev_line = None
while True:
if self.proc.stdout is None:
continue
if stdout_line:
prev_line = stdout_line
stdout_line = self.proc.stdout.readline().decode("utf-8", errors="replace").strip()
out_time_data = self.REGEX_OUT_TIME.search(stdout_line)
if out_time_data is not None:
out_time = self.get_timedelta(out_time_data.group(1))
progress.update(task, completed=out_time.total_seconds())
continue
if not stdout_line and self.proc.poll() is not None:
exit_code = self.proc.poll()
if exit_code:
raise FfmpegDLException(prev_line)
else:
break
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ pycryptodome
requests
rich
setuptools
tqdm
urllib3

0 comments on commit 0881411

Please sign in to comment.