Skip to content

Video editing with Python

Fix issue #2437 by major refactoring and avoiding circular reference

osaajani07f585f09625
diff --git a/moviepy/Clip.py b/moviepy/Clip.py
index b12fc65f..2b636177 100644
--- a/moviepy/Clip.py
+++ b/moviepy/Clip.py
@@ -323,6 +323,8 @@ class Clip:
frame_function : function
New frame creator function for the clip.
+ A frame_function is a function taking a time ``t`` as input and
+ returning a frame (a numpy array) as a result.
"""
self.frame_function = frame_function
diff --git a/moviepy/video/io/ImageSequenceClip.py b/moviepy/video/io/ImageSequenceClip.py
index f1e6588c..596befc3 100644
--- a/moviepy/video/io/ImageSequenceClip.py
+++ b/moviepy/video/io/ImageSequenceClip.py
@@ -23,8 +23,7 @@ class ImageSequenceClip(VideoClip):
will be considered in alphanumerical order.
- A list of names of image files. In this case you can choose to
load the pictures in memory pictures
- - A list of Numpy arrays representing images. In this last case,
- masks are not supported currently.
+ - A list of Numpy arrays representing images.
fps
Number of picture frames to read per second. Instead, you can provide
@@ -62,21 +61,18 @@ class ImageSequenceClip(VideoClip):
# Parse the data
- fromfiles = True
+ self.fromfiles = True
if isinstance(sequence, list):
if isinstance(sequence[0], str):
if load_images:
sequence = [imread(file) for file in sequence]
- fromfiles = False
- else:
- fromfiles = True
+ self.fromfiles = False
else:
# sequence is already a list of numpy arrays
- fromfiles = False
+ self.fromfiles = False
else:
# sequence is a folder name, make it a list of files:
- fromfiles = True
sequence = sorted(
[os.path.join(sequence, file) for file in os.listdir(sequence)]
)
@@ -112,56 +108,73 @@ class ImageSequenceClip(VideoClip):
if fps is None:
self.fps = len(sequence) / self.duration
- def find_image_index(t):
- return max(
- [i for i in range(len(self.sequence)) if self.images_starts[i] <= t]
- )
-
- if fromfiles:
+ if self.fromfiles:
self.last_index = None
self.last_image = None
- def frame_function(t):
- index = find_image_index(t)
+ if with_mask and (imread(self.sequence[0]).shape[2] == 4):
+ self.mask = ImageSequenceClip(
+ sequence=sequence,
+ fps=fps,
+ durations=durations,
+ with_mask=False,
+ is_mask=True,
+ load_images=load_images,
+ )
- if index != self.last_index:
- self.last_image = imread(self.sequence[index])[:, :, :3]
- self.last_index = index
+ else:
+ if with_mask and (self.sequence[0].shape[2] == 4):
+ self.mask = ImageSequenceClip(
+ sequence=sequence,
+ fps=fps,
+ durations=durations,
+ with_mask=False,
+ is_mask=True,
+ load_images=load_images,
+ )
- return self.last_image
+ self.size = self.frame_function(0).shape[:2][::-1]
- if with_mask and (imread(self.sequence[0]).shape[2] == 4):
- self.mask = VideoClip(is_mask=True)
- self.mask.last_index = None
- self.mask.last_image = None
+ def _find_image_index(self, t):
+ return max([i for i in range(len(self.sequence)) if self.images_starts[i] <= t])
- def mask_frame_function(t):
- index = find_image_index(t)
- if index != self.mask.last_index:
- frame = imread(self.sequence[index])[:, :, 3]
- self.mask.last_image = frame.astype(float) / 255
- self.mask.last_index = index
+ def frame_function(self, t):
+ """Retrieves the frame corresponding to the given time `t`.
- return self.mask.last_image
+ Depending on whether the frames are loaded from files or provided as
+ an in-memory sequence, this function either reads the frame from disk
+ or accesses it directly from the sequence.
- self.mask.frame_function = mask_frame_function
- self.mask.size = mask_frame_function(0).shape[:2][::-1]
+ Parameters
+ ----------
- else:
+ t (float): The time (in seconds) for which the corresponding frame
+ is to be retrieved.
- def frame_function(t):
- index = find_image_index(t)
- return self.sequence[index][:, :, :3]
+ Returns
+ -------
- if with_mask and (self.sequence[0].shape[2] == 4):
- self.mask = VideoClip(is_mask=True)
+ numpy.ndarray: The image frame at the specified time. If `is_mask`
+ is True, the alpha channel of the image is returned
+ as a float array normalized to the range [0, 1].
+ Otherwise, the RGB channels of the image are returned.
- def mask_frame_function(t):
- index = find_image_index(t)
- return 1.0 * self.sequence[index][:, :, 3] / 255
+ """
+ index = self._find_image_index(t)
- self.mask.frame_function = mask_frame_function
- self.mask.size = mask_frame_function(0).shape[:2][::-1]
+ if self.fromfiles:
+ self.last_index = index
+
+ if self.is_mask:
+ self.last_image = (
+ imread(self.sequence[index])[:, :, 3].astype(float) / 255.0
+ )
+ else:
+ self.last_image = imread(self.sequence[index])[:, :, :3]
- self.frame_function = frame_function
- self.size = frame_function(0).shape[:2][::-1]
+ return self.last_image
+ else:
+ if self.is_mask:
+ return self.sequence[index][:, :, 3].astype(float) / 255.0
+ else:
+ return self.sequence[index][:, :, :3]