Skip to content

Video editing with Python

changes in subtitles, added mask_to_rgb to write_gif, corrected doc

Zulko35954aaa9917
diff --git a/README.rst b/README.rst
index dbd7650c..9adbb175 100644
--- a/README.rst
+++ b/README.rst
@@ -21,33 +21,28 @@ A typical MoviePy script looks like this: ::
# write the result to a file in any format
final_clip.to_videofile("myHolidays_edited.webm",fps=25)
-MoviePy can read and write all the most common audio and video formats, including GIF, and runs on Windows/Mac/Linux, with Python 2 and 3.
+MoviePy can read and write all the most common audio and video formats, including GIF, and runs on Windows/Mac/Linux, with Python 2.7+ and 3.
Contribute !
-------------
-MoviePy is an open-source software originally written by Zulko_ and released under the MIT licence. The project is hosted on Github_ , where everyone is welcome to contribute, ask for help or simply give feedback.
+MoviePy is an open-source software originally written by Zulko_ and released under the MIT licence. The project is hosted on Github_ , where everyone is welcome to contribute, ask for help or simply give feedback.
+
You can also discuss about the project on Reddit_ or on the mailing list [email protected] .
Installation
--------------
-**Method with pip:** if you have ``pip`` installed, just type this in a terminal (sudo is optional on some systems) ::
-
- (sudo) pip install moviepy
-
-This method will fail if ``ez_setup`` is not installed on your computer. In this case install ``ez_setup`` first, with ::
+**Method with pip:** if you have ``pip`` installed, just type this in a terminal (it will install ez_setup if you don't already have it) ::
- (sudo) pip install ez_setup
+ (sudo) pip install ez_setup moviepy
**Method by hand:** download the sources, either on PyPI_ or (if you want the development version) on Github_, unzip everything in one folder, open a terminal and type ::
(sudo) python setup.py install
-MoviePy depends on the Python modules Numpy_, Decorator_, and tqdm_, which will be automatically installed during MoviePy's installation.
-
-This should work on Windows/Mac/Linux, with Python 2 and 3 ; if you have trouble installing MoviePy or one of its dependencies, please provide feedback !
+MoviePy depends on the Python modules Numpy_, Decorator_, and tqdm_, which will be automatically installed during MoviePy's installation. It should work on Windows/Mac/Linux, with Python 2.7+ and 3 ; if you have trouble installing MoviePy or one of its dependencies, please provide feedback !
Linking to ffmpeg
~~~~~~~~~~~~~~~~~~
diff --git a/docs/examples/quick_recipes.rst b/docs/examples/quick_recipes.rst
index 7d612e0c..ec92577d 100644
--- a/docs/examples/quick_recipes.rst
+++ b/docs/examples/quick_recipes.rst
@@ -2,21 +2,64 @@ Quick recipes
===============
+
+Effects and filters
+---------------------
-Add a title before a video
----------------------------
+Blurring all frames of a video
+"""""""""""""""""""""""""""""""
+
+::
+
+ from skimage.filter import gaussian_filter
+ from moviepy.editor import VideoFileClip
+
+ def blur(image):
+ """ Returns a blurred (radius=2 pixels) version of the image """
+ return gaussian_filter(image.astype(float), sigma=2)
+
+ clip = VideoFileClip("my_video.mp4")
+ clip_blurred = clip.fl_image( blur )
+ clip_blurred.write_videofile("blurred_video.mp4")
-Videos
+Cutting videos
+---------------
+Scene detection
+----------------
-Make gifs that loop well
---------------------------
+Compositing videos
+-------------------
+
+Add a title before a video
+"""""""""""""""""""""""""""
+
+
+Art of Gif-making
+-------------------
clip.fx( vfx.time_symmetrize)
# find a subclip
T = clip
+
+Useless but fun
+----------------
+
+
+Getting the average frame of a video
+"""""""""""""""""""""""""""""""""""""
+::
+
+ from moviepy.editor import VideoFileClip, ImageClip
+ clip = VideoFileClip("video.mp4")
+ fps= 1.0 # take one frame per second
+ nframes = clip.duration*fps # total number of frames used
+ total_image = sum(clip.iter_frames(fps,dtype=float,progress_bar=True))
+ average_image = ImageClip(total_image/ nframes)
+ average_image.save_frame("average_test.png")
+
diff --git a/docs/getting_started/clips.rst b/docs/getting_started/clips.rst
index e7110d0e..de72e8b0 100644
--- a/docs/getting_started/clips.rst
+++ b/docs/getting_started/clips.rst
@@ -10,6 +10,7 @@ The following code summarizes the base clips that you can create with moviepy: :
# VIDEO CLIPS
clip = VideoClip(make_frame, duration=4) # for custom animations (see below)
clip = VideoFileClip("my_video_file.mp4") # or .avi, .webm, .gif ...
+ clip = ImageSequenceClip(['image_file1.jpeg', ...], fps=24)
clip = ImageClip("my_picture.png") # or .jpeg, .tiff, ...
clip = TextClip("Hello !", font="Amiri-Bold", fontsize=70, color="black")
clip = ColorClip(size=(460,380), color=[R,G,B])
@@ -75,6 +76,18 @@ Note that these clips will have an ``fps`` (frame per second) attribute, which w
For more, see :py:class:`~moviepy.video.io.VideoFileClip.VideoFileClip`.
+ImageSequenceClip
+""""""""""""""""""
+
+This is a clip made from a series of images, you call it with ::
+
+ clip = ImageSequenceClip(images_list, fps=25)
+
+where ``images_list`` can be either a list of image names (that will be *played*) in that order, a folder name (at which case all the image files in the folder will be played in alphanumerical order), or a list of frames (Numpy arrays), obtained for instance from other clips.
+
+When you provide a folder name or list of file names, you can choose ``load_images=True`` to specify that all images should be loaded into the RAM. This is only interesting if you have a small number of images that will be each used more than once (e.g. if the images form a looping animation).
+
+
ImageClip
""""""""""
@@ -89,6 +102,7 @@ For more, see :py:class:`~moviepy.video.VideoClip.ImageClip`.
Two examples of ImageClip shown below are the TextClip and ColorClip
+
TextClip
"""""""""""""""
diff --git a/docs/getting_started/compositing.rst b/docs/getting_started/compositing.rst
index 09e7bf81..3d7fae55 100644
--- a/docs/getting_started/compositing.rst
+++ b/docs/getting_started/compositing.rst
@@ -20,13 +20,13 @@ Stacking and concatenating clips
Two simple ways of putting clips together is to concatenate them (to play them one after the other in a single long clip) or to stack them (to them side by side in a single larger clip).
-Concatenation is done with the function ``concatenate``: ::
+Concatenation is done with the function ``concatenate_videoclips``: ::
- from moviepy.editor import VideoFileClip, concatenate
+ from moviepy.editor import VideoFileClip, concatenate_videoclips
clip1 = VideoFileClip("myvideo.mp4")
clip2 = VideoFileClip("myvideo2.mp4").subclip(50,60)
clip3 = VideoFileClip("myvideo3.mp4")
- final_clip = concatenate([clip1,clip2,clip3])
+ final_clip = concatenate_videoclips([clip1,clip2,clip3])
final_clip.write_videofile("my_concatenation.mp4")
diff --git a/moviepy/Clip.py b/moviepy/Clip.py
index 1190437b..3cba0c89 100644
--- a/moviepy/Clip.py
+++ b/moviepy/Clip.py
@@ -379,20 +379,30 @@ class Clip:
"""
if (self.duration is not None) and (t_start>self.duration):
+
raise ValueError("t_start (%.02f) "%t_start +
"should be smaller than the clip's "+
"duration (%.02f)."%self.duration)
newclip = self.fl_time(lambda t: t + t_start, apply_to=[])
+
if (t_end is None) and (self.duration is not None):
+
t_end = self.duration
+
elif t_end<0:
+
if self.duration is None:
+
print ("Error: subclip with negative times can only be"+
"extracted from clips with a ``duration``")
+
else:
+
t_end = self.duration + t_end
+
if (t_end is not None):
+
newclip.duration = t_end - t_start
newclip.end = newclip.start + newclip.duration
@@ -418,9 +428,13 @@ class Clip:
fl = lambda t: t + (t >= ta)*(tb - ta)
newclip = self.fl_time(fl)
+
if self.duration is not None:
+
return newclip.set_duration(self.duration - (tb - ta))
+
else:
+
return newclip
@requires_duration
@@ -453,17 +467,25 @@ class Clip:
"""
def generator():
+
for t in np.arange(0, self.duration, 1.0/fps):
+
frame = self.get_frame(t)
+
if (dtype is not None) and (frame.dtype != dtype):
+
frame = frame.astype(dtype)
if with_times:
+
yield t, frame
+
else:
+
yield frame
if progress_bar:
+
nframes = int(self.duration*fps)+1
return tqdm(generator(), total=nframes)
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 9112ee5c..4bc07058 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -402,6 +402,7 @@ class VideoClip(Clip):
@requires_duration
+ @convert_masks_to_RGB
def write_gif(self, filename, fps=None, program='ImageMagick',
opt="OptimizeTransparency", fuzz=1, verbose=True,
loop=0, dispose=False, colors=None, tempfiles=False):
diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py
index 964bde2c..0c885bb8 100644
--- a/moviepy/video/io/ffmpeg_reader.py
+++ b/moviepy/video/io/ffmpeg_reader.py
@@ -123,8 +123,8 @@ class FFMPEG_VideoReader:
else:
- result = np.fromstring(s, dtype='uint8').\
- reshape((h, w, len(s)//(w*h)))
+ result = np.fromstring(s, dtype='uint8')
+ result.shape =(h, w, len(s)//(w*h)) # reshape((h, w, len(s)//(w*h)))
self.lastread = result
return result
diff --git a/moviepy/video/tools/subtitles.py b/moviepy/video/tools/subtitles.py
index ad7c035c..81851356 100644
--- a/moviepy/video/tools/subtitles.py
+++ b/moviepy/video/tools/subtitles.py
@@ -4,6 +4,7 @@ import re
import numpy as np
from moviepy.video.VideoClip import VideoClip, TextClip
from moviepy.tools import cvsecs
+from moviepy.decorators import convert_to_seconds
class SubtitlesClip(VideoClip):
@@ -80,6 +81,24 @@ class SubtitlesClip(VideoClip):
self.make_frame = make_frame
self.mask = VideoClip(make_mask_frame, ismask=True)
+
+ def in_subclip(self, t_start= None, t_end= None):
+ """ Returns a sequence of [(t1,t2), txt] covering all the given subclip
+ from t_start to t_end. The first and last times will be cropped so as
+ to be exactly t_start and t_end if possible. """
+
+ def is_in_subclip(t1,t2):
+ try:
+ return (t_start<=t1<t_end) or (t_start< t2 <=t_end)
+ except:
+ return False
+ def try_cropping(t1,t2):
+ try:
+ return (max(t1, t_start), min(t2, t_end))
+ except:
+ return (t1, t2)
+ return [(try_cropping(t1,t2), txt) for ((t1,t2), txt) in self.subtitles
+ if is_in_subclip(t1,t2)]
@@ -107,7 +126,7 @@ class SubtitlesClip(VideoClip):
def match_expr(self, expr):
return SubtitlesClip([e for e in self.subtitles
- if re.find(expr, e) != []])
+ if re.findall(expr, e[1]) != []])
def write_srt(self, filename):