Skip to content

Commit 3dabc2a

Browse files
committed
GH-61: Transform the Video initializer into a generator
1 parent c017069 commit 3dabc2a

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/thumbnails/generator.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from .constants import DEFAULT_OUTPUT
1212
from .constants import DEFAULT_SKIP
1313
from .pathtools import listdir
14-
from .pathtools import metadata_path
1514
from .progress import use_progress
1615
from .thumbnail import ThumbnailExistsError
1716
from .thumbnail import ThumbnailFactory
@@ -36,6 +35,9 @@ def __init__(self, inputs):
3635
self.compress = DEFAULT_COMPRESS
3736
self.interval = DEFAULT_INTERVAL
3837

38+
# Remove non-video files in case of input directory already contains other generated files.
39+
self.inputs = [file for file in self.inputs if re.match(r"^.*\.(?:(?!png|vtt|json).)+$", file)]
40+
3941
@staticmethod
4042
def worker(video, fmt, base, skip, output):
4143
"""Executes the required workflows for generating a thumbnail."""
@@ -46,21 +48,18 @@ def worker(video, fmt, base, skip, output):
4648
thumbnail.prepare_frames()
4749
thumbnail.generate()
4850

49-
@use_progress
50-
def generate(self):
51-
self.inputs = [file for file in self.inputs if re.match(r"^.*\.(?:(?!png|vtt|json).)+$", file)]
52-
self.inputs = dict(zip(map(lambda i: metadata_path(i, self.output, self.format), self.inputs), self.inputs))
51+
def __iter__(self):
52+
return self
5353

54-
with concurrent.futures.ThreadPoolExecutor() as executor:
55-
videos = executor.map(
56-
functools.partial(
57-
Video,
58-
compress=self.compress,
59-
interval=self.interval,
60-
),
61-
self.inputs.values(),
62-
)
54+
def __next__(self):
55+
"""Returns the next video to be processed."""
56+
try:
57+
return Video(self.inputs.pop(), self.compress, self.interval)
58+
except IndexError:
59+
raise StopIteration
6360

61+
@use_progress
62+
def generate(self):
6463
with concurrent.futures.ThreadPoolExecutor() as executor:
6564
executor.map(
6665
functools.partial(
@@ -70,5 +69,5 @@ def generate(self):
7069
skip=self.skip,
7170
output=self.output,
7271
),
73-
videos,
72+
self,
7473
)

0 commit comments

Comments
 (0)