Skip to content

Commit de94b84

Browse files
committed
Implement initial version of CLI logs (using rich)
1 parent 8937b48 commit de94b84

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ install_requires =
3535
imageio-ffmpeg>=0.4.7
3636
imageio>=2.23.0
3737
pillow>=8.4.0
38+
rich>=13.0.0
3839
python_requires = >=3.7
3940
package_dir =
4041
=src

src/thumbnails/progress.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from rich.live import Live
2+
from rich.status import Status
3+
4+
5+
class Progress:
6+
def __init__(self, status, done="Process completed"):
7+
self.status = Status(status)
8+
self.live = Live(self.status)
9+
self.done = done
10+
11+
def update(self, status):
12+
self.status.update(status)
13+
self.live.update(self.status)
14+
15+
def __enter__(self):
16+
self.live.start()
17+
return self
18+
19+
def __exit__(self, *_):
20+
self.live.update("[white]%s ... [/white][green]done[/green]" % self.done)
21+
self.live.stop()

src/thumbnails/thumbnail.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .pathtools import ensure_tree
1313
from .pathtools import extract_name
1414
from .pathtools import metadata_path
15+
from .progress import Progress
1516

1617

1718
def register_thumbnail(typename):
@@ -45,7 +46,9 @@ def __init__(self, video, base, skip, output):
4546
self.thumbnail_dir = self.calc_thumbnail_dir()
4647
self.metadata_path = self._get_metadata_path()
4748
self._perform_skip()
48-
self.extract_frames()
49+
50+
with Progress("Extracting frames ...", "Frames have been extracted"):
51+
self.extract_frames()
4952

5053
def _get_metadata_path(self):
5154
"""Initiates the name of the thumbnail metadata file."""
@@ -100,12 +103,15 @@ def prepare_frames(self):
100103
master = Image.new(mode="RGBA", size=next(thumbnails))
101104
master_path = os.path.join(self.thumbnail_dir, extract_name(self.filepath) + ".png")
102105

103-
for frame, *_, x, y in thumbnails:
104-
with Image.open(frame) as image:
105-
image = image.resize((self.width, self.height), Image.ANTIALIAS)
106-
master.paste(image, (x, y))
106+
with Progress("Processing frames ...", "Frames have been merged into one") as progress:
107+
for frame, *_, x, y in thumbnails:
108+
progress.update("Processing '%s'" % frame)
109+
with Image.open(frame) as image:
110+
image = image.resize((self.width, self.height), Image.ANTIALIAS)
111+
master.paste(image, (x, y))
107112

108-
master.save(master_path)
113+
progress.update("Saving the result '%s'" % master_path)
114+
master.save(master_path)
109115

110116
def generate(self):
111117
def format_time(secs):
@@ -117,12 +123,14 @@ def format_time(secs):
117123
route = os.path.join(prefix, extract_name(self.filepath) + ".png")
118124
route = pathlib.Path(route).as_posix()
119125

120-
for _, start, end, x, y in self.thumbnails():
121-
thumbnail_data = "%s --> %s\n%s#xywh=%d,%d,%d,%d\n\n" % (
122-
format_time(start), format_time(end),
123-
route, x, y, self.width, self.height,
124-
)
125-
metadata.append(thumbnail_data)
126+
with Progress("Generating thumbnail metadata ...", "Thumbnail metadata has been generated") as progress:
127+
for _, start, end, x, y in self.thumbnails():
128+
progress.update("Generating metadata for '%s'" % route)
129+
thumbnail_data = "%s --> %s\n%s#xywh=%d,%d,%d,%d\n\n" % (
130+
format_time(start), format_time(end),
131+
route, x, y, self.width, self.height,
132+
)
133+
metadata.append(thumbnail_data)
126134

127135
with open(self.metadata_path, "w") as fp:
128136
fp.writelines(metadata)

src/thumbnails/video.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from .ffmpeg import _FFMpeg
1212
from .frame import _Frame
13+
from .progress import Progress
1314

1415
ffmpeg_bin = get_ffmpeg_exe()
1516

@@ -41,8 +42,9 @@ def __init__(self, filepath, compress, interval):
4142
self.__frames_count = None
4243
self.__columns = None
4344

44-
_FFMpeg.__init__(self, filepath)
45-
_Frame.__init__(self, self.size)
45+
with Progress("Parsing the video metadata ...", "Metadata has been parsed"):
46+
_FFMpeg.__init__(self, filepath)
47+
_Frame.__init__(self, self.size)
4648

4749
@property
4850
def filepath(self):

0 commit comments

Comments
 (0)