Skip to content

Video editing with Python

added class ImageSequenceClip to replace DirectoryClip

Zulko9cc3547e9f7d
diff --git a/moviepy/editor.py b/moviepy/editor.py
index a874eae5..5d09c9f1 100644
--- a/moviepy/editor.py
+++ b/moviepy/editor.py
@@ -1,6 +1,6 @@
"""
-This file is meant to make it easy to load the main features of MoviePy
-by simply typing:
+This file is meant to make it easy to load the main features of
+MoviePy by simply typing:
>>> from moviepy.editor import *
@@ -20,6 +20,7 @@ clip.preview().
# Clips
from .video.io.VideoFileClip import VideoFileClip
+from .video.io.ImageSequenceClip import ImageSequenceClip
from .video.VideoClip import VideoClip, ImageClip, ColorClip, TextClip
from .video.compositing.CompositeVideoClip import CompositeVideoClip
from .video.compositing.concatenate import concatenate
@@ -62,7 +63,8 @@ for method in ["vfx.crop",
for method in ["afx.audio_fadein",
- "afx.audio_fadeout"]:
+ "afx.audio_fadeout",
+ "afx.volumex"]:
exec("AudioClip.%s = %s"%( method.split('.')[1], method))
@@ -75,10 +77,10 @@ try:
from moviepy.video.io.preview import show, preview
except ImportError:
def preview(self, *args, **kwargs):
- """ NOT AVAILABLE : clip.preview requires Pygame installed """
+ """NOT AVAILABLE : clip.preview requires Pygame installed."""
raise ImportError("clip.preview requires Pygame installed")
def show(self, *args, **kwargs):
- """ NOT AVAILABLE : clip.show requires Pygame installed """
+ """NOT AVAILABLE : clip.show requires Pygame installed."""
raise ImportError("clip.show requires Pygame installed")
@@ -89,7 +91,7 @@ try:
from moviepy.audio.io.preview import preview
except ImportError:
def preview(self, *args, **kwargs):
- """ NOT AVAILABLE : clip.preview requires Pygame installed """
+ """ NOT AVAILABLE : clip.preview requires Pygame installed."""
raise ImportError("clip.preview requires Pygame installed")
AudioClip.preview = preview
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index a8e90986..86b7607c 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -340,6 +340,67 @@ class VideoClip(Clip):
return blit(img, picture, pos, mask=mask, ismask=self.ismask)
+ def to_images_sequence(self, nameformat, fps=None, verbose=True):
+ """ Writes the videoclip to a sequence of image files.
+
+
+ Parameters
+ -----------
+
+ nameformat
+ A filename specifying the numerotation format and extension
+ of the pictures. For instance "frame%03d.png" for filenames
+ indexed with 3 digits and PNG format. Also possible:
+ "some_folder/frame%04d.jpeg", etc.
+
+ fps
+ Number of frames per second to consider when writing the clip.
+ If not specified, the clip's ``fps`` attribute will be used if
+ it has one.
+
+ verbose
+ Verbose output ?
+
+
+ Returns
+ --------
+
+ names_list
+ A list of all the files generated.
+
+ Notes
+ ------
+
+ The resulting image sequence can be read using e.g. the class
+ ``DirectoryClip``.
+
+ """
+
+ if verbose:
+ print( "MoviePy: Writing frames %s."%(nameformat))
+
+ if fps is None:
+ fps = self.fps
+
+ tt = np.arange(0, self.duration, 1.0/fps)
+
+ filenames = []
+ total = int(self.duration/fps)+1
+ for i, t in tqdm(enumerate(tt), total=total):
+ name = nameformat%(i+1)
+ filenames.append(name)
+ self.save_frame(name, t, savemask=True)
+
+ if verbose:
+ print( "MoviePy: Done writing frames %s."%(nameformat))
+
+ return filenames
+
+
+
+
+
+
def to_gif(self, filename, fps=None, program= 'ImageMagick',
opt="OptimizeTransparency", fuzz=1, verbose=True,
diff --git a/moviepy/video/fx/freeze_at_start.py b/moviepy/video/fx/freeze_at_start.py
index 4fc34d45..37e8b048 100644
--- a/moviepy/video/fx/freeze_at_start.py
+++ b/moviepy/video/fx/freeze_at_start.py
@@ -5,12 +5,13 @@ from moviepy.video.compositing.concatenate import concatenate
@requires_duration
def freeze_at_start(clip, freeze_duration=None, total_duration=None):
- """
- Makes the clip freeze on its last frame. With ``duration`` you can
- specify the duration of the freeze. With ``total_duration`` you can
- specify the total duration of the clip and the freeze (i.e. the
- duration of the freeze is automatically calculated). If neither
- is provided, the freeze will have an infinite length.
+ """ Momentarily freeze the clip on its first frame.
+
+ With ``duration``you can specify the duration of the freeze.
+ With ``total_duration`` you can specify the total duration of
+ the clip and the freeze (i.e. the duration of the freeze is
+ automatically calculated). If neither is provided, the freeze
+ will have an infinite length.
"""
freezed_clip = ImageClip(clip.get_frame(0))
diff --git a/moviepy/video/io/DirectoryClip.py b/moviepy/video/io/DirectoryClip.py
deleted file mode 100644
index 4290451e..00000000
--- a/moviepy/video/io/DirectoryClip.py
+++ /dev/null
@@ -1,38 +0,0 @@
-from moviepy.video.VideoClip import VideoClip
-from .ffmpeg_reader import ffmpeg_read_image
-
-class DirectoryClip(VideoClip):
- """
-
- A VideoClip read from a directory containing pictures.
-
- """
-
- def __init__(self, foldername, fps, withmask=True, ismask=False):
-
- VideoClip.__init__(self, ismask=ismask)
-
- self.directory = foldername
- self.fps = fps
- self.imagefiles = sorted(os.listdir(foldername))
-
- self.duration = 1.0* len(self.imagefiles) / self.fps
- self.end = self.duration
-
- self.lastpos = None
- self.lastimage = None
-
-
-
- def get_frame(t):
-
- pos = int(self.fps*t)
- if pos != self.lastpos:
- self.lastimage = ffmpeg_read_image(self.imagefiles[ind],
- withmask=withmask)
- self.lastpos = pos
-
- return self.lastimage
-
- self.get_frame = get_frame
- self.size = get_frame(0).shape[:2][::-1]
diff --git a/moviepy/video/io/ImageSequenceClip.py b/moviepy/video/io/ImageSequenceClip.py
new file mode 100644
index 00000000..ee6101cd
--- /dev/null
+++ b/moviepy/video/io/ImageSequenceClip.py
@@ -0,0 +1,104 @@
+from ..VideoClip import VideoClip
+from .ffmpeg_reader import ffmpeg_read_image
+import os
+
+class ImageSequenceClip(VideoClip):
+ """
+
+ A VideoClip made from a series of images.
+
+
+ Parameters
+ -----------
+
+ sequence
+ Can be one of these:
+ - The name of a folder (containing only pictures). The pictures
+ will be considered in alphanumerical order.
+ - A list of names of image files.
+ - A list of Numpy arrays representing images. In this last case,
+ masks are not supported currently.
+
+
+ fps
+ Number of picture frames to read per second.
+
+ with_mask
+ Should the alpha layer of PNG images be considered as a mask ?
+
+ ismask
+ Will this sequence of pictures be used as an animated mask.
+
+
+ """
+
+
+ def __init__(self, sequence, fps, with_mask=True, ismask=False):
+
+ # CODE WRITTEN AS IT CAME, MAY BE IMPROVED IN THE FUTURE
+
+ VideoClip.__init__(self, ismask=ismask)
+
+ # Parse the data
+
+ fromfiles = True
+
+ if isinstance(sequence, list):
+ if not isinstance(sequence[0], str):
+ # sequence is a list of numpy arrays
+ fromfiles = False
+ else:
+ # sequence is a folder name
+ sequence = sorted([os.path.join(sequence, f)
+ for f in os.listdir(sequence)])
+
+ self.fps = fps
+ self.duration = 1.0* len(sequence) / self.fps
+ self.end = self.duration
+ self.sequence = sequence
+
+ if fromfiles:
+
+ self.lastpos = None
+ self.lastimage = None
+
+ def get_frame(t):
+
+ pos = int(self.fps*t)
+ if pos != self.lastpos:
+ self.lastimage = ffmpeg_read_image(
+ self.sequence[pos],
+ with_mask=False)
+ self.lastpos = pos
+
+ return self.lastimage
+
+ if with_mask and (get_frame(0).shape[2]==4):
+
+ self.mask = VideoClip(ismask=True)
+
+ def mask_get_frame(t):
+
+ pos = int(self.fps*t)
+ if pos != self.lastpos:
+ self.mask.lastimage = ffmpeg_read_image(
+ self.sequence[pos],
+ with_mask=True)[:,:,3]
+ self.mask.lastpos = pos
+
+ return self.mask.lastimage
+
+ self.mask.get_frame = mask_get_frame
+ self.mask.size = mask_get_frame(0).shape[:2][::-1]
+
+
+ else:
+
+ def get_frame(t):
+
+ pos = int(self.fps*t)
+ return self.sequence[pos]
+
+
+ self.get_frame = get_frame
+ self.size = get_frame(0).shape[:2][::-1]