Skip to content

Video editing with Python

Fixed stderr bug when writing logiles. Started to rewrite the docs.

Zulkodec0add018d4
diff --git a/docs/crash_course/crash_course.rst b/docs/crash_course/crash_course.rst
index 572691bb..232145b4 100644
--- a/docs/crash_course/crash_course.rst
+++ b/docs/crash_course/crash_course.rst
@@ -1,14 +1,16 @@
A MoviePy crash-course
-----------------------
-In this section we will give you the basics to start editing your videos.
+
+This section presents the basics of video editing with MoviePy.
+
Example code
~~~~~~~~~~~~~~
-In a typical script, you load or generate some clips, modify them, combine them, and write the result to a video file.
-So let us load a movie of my last holidays, lower the volume, add a title in the center of the video for the ten first seconds, and write the result in a file: ::
+In a typical MoviePy script, you load some clips from video files, then modify them, put them together, and write the result to a video file. As an example, let us load a movie of my last holidays, lower the volume, add a title in the center of the video for the first ten seconds, and write the result in a file: ::
+ # Import everything needed to edit video clips
from moviepy.editor import *
# Load myHolidays.mp4 and select the subclip 00:00:50 - 00:00:60
@@ -29,39 +31,35 @@ So let us load a movie of my last holidays, lower the volume, add a title in the
# Write the result to a file
video.to_videofile("myHolidays_edited.avi",fps=24, codec='mpeg4')
-How MoviePy works, in a nutshell
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-MoviePy uses mainly ``ffmpeg`` for reading/writing multimedia files, and Numpy/Scipy for image and sound manipulations.
+What powers MoviePy
+~~~~~~~~~~~~~~~~~~~~~
+
+MoviePy uses the software ``ffmpeg`` to read frames from video and audio files. The code to edit and put these frames together relies mainly on the Python library Numpy. The edited frames are assembled in a video file using once again ``ffmpeg``. The software ImageMagick can also be used to generate texts or export an animation in GIF format.
.. image:: explanations.jpeg
:width: 570px
:align: center
-You can do pretty much any effect you want with MoviePy, but it is just a framework, and in many cases you will need to code a little (or find someone who will !) to come to your goal.
-
-
The clip objects
~~~~~~~~~~~~~~~~~~~
-In MoviePy you manipulate mostly two kind of objects: audio clips, and
-video clips. These clips have different attributes (indicating their
-duration, their size, their starting time...) which can change as the clips are cut and
-mixed with other clips.
+The most common objects in MoviePy are called clips. A clip (let's call it ``myclip``) can be either an audio clip or a video clip. It has different attributes, like ``myclip.duration`` (indicating its duration in seconds), ``clip.start`` (indicating the time in seconds where the clip should start playing when mixed with other videos), ``myclip.w`` (the width in pixel of the clip), etc.
-A video clip can carry around a soundtrack (which is an
-audioclip) and, for more complex effects, a mask (which is a special
-video clip).
+A video clip ``myclip`` can have two special attributes which are themselves clips: ``myclip.audio`` is an audio clip representing the audio track of the vidceo clip. ``myclip.mask`` is a special kind of video clip which defines the transparency of ``myclip``.
+
+A clip also has methods which enable to create a derivative of this clip. For instance ``myclip.subclip(2,7)`` creates a new clip which is like ``myclip`` cut between seconds 2 and 7.
Classes of video clips
~~~~~~~~~~~~~~~~~~~~~~
Video clips are the building blocks of longer videos. They can be created with one of the following:
-- ``VideoFileClip('myHolidays.mp4')`` will generate a clip from a movie file. The input file can have any format or extension : mp4, flv, mov, ... even gif !
-- ``ImageClip('myPicture.png')`` will make a clip displaying a picture, for an *a priori* infinite duration.
-- ``ColorClip((height,width),color=(R,G,B))`` makes a special ImageClip of a single color.
+- ``VideoFileClip('myHolidays.mp4')`` will generate a clip from a video file. This video file can have any format or extension : mp4, flv, mov, ... even gif !
+- ``ImageClip('myPicture.png')`` will generate a clip displaying a picture, for an *a priori* infinite duration.
+- ``ColorClip((height,width),color=(R,G,B))`` generates a special ImageClip of a single color.
- ``TextClip('Hello folks !',font='Impact-Regular')`` generates a special ImageClip representing a text. The generation of the text image is made by the software ImageMagick (see the Installation section).
+
To that list we should add the ``CompositeVideoClips`` which are obtained by putting several clips together.
.. _CompositeVideoClips:
diff --git a/docs/gallery.rst b/docs/gallery.rst
new file mode 100644
index 00000000..80cfd30b
--- /dev/null
+++ b/docs/gallery.rst
@@ -0,0 +1,53 @@
+Gallery
+========
+
+The gallery will certainly fill up as more people start using MoviePy (which is currently less than one year old).
+
+
+Videos edited with Moviepy
+---------------------------
+
+The Cup Song Covers Mix
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+This video (link) is a mix of 60 covers of the Cup Song which demonstrates non-linear video editing capabilities of MoviePy. The code that generated the script (not commented) can be found here.
+
+
+Gifs edited with MoviePy
+--------------------------
+
+An introductory tutorial
+~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This tutorial (link)
+
+Programs and Scripts using MoviePy
+-----------------------------------
+
+Videogrep
+~~~~~~~~~~
+
+If you liked it there is also [this blog post] which attempts to cut a video precisely at the beginning and end of sentences or words, and this blog post in which MoviePy automatically cuts together all the highlights of a soccer game, based on the fact that the crowd cheers louder when something interesting happens.
+
+
+
+MoviePy online
+----------------
+
+Rinconcam
+~~~~~~~~~~~
+
+Rincomcam (link) is a camera which films surfers on the Californian beach of Point Rincon. At the end of each day it cuts together a video and puts it online. Everything is entirely automatized with Python. MoviePy is used to add transitions, titles and music to the videos.
+
+
+
+
+
+
+MoviePy in sciences and technology
+------------------------------------
+
+Piano rolls transcription to sheet music
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This blog post explains how to transform a video of a piano roll performance into playable sheet music. MoviePy is used for the frame-by-frame analysis of the piano roll video. The last video is also edited with MoviePy:
\ No newline at end of file
diff --git a/docs/index.rst b/docs/index.rst
index 247b18bf..9812e0b7 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -1,18 +1,18 @@
MoviePy
=======
-MoviePy is a Python module for script-based movie editing. It enables basic operations (cuts, concatenations, title insertions) to be done in a few lines, and can be used for advanced compositing and special effects.
+MoviePy is a Python module for script-based video editing. It enables basic operations (cuts, concatenations, title insertions) to be done in a few lines, and can be used for advanced compositing and video processing. It can read and write all the most common video formats.
-It can read and write to many formats, `including animated GIFs <http://zulko.github.io/blog/2014/01/23/making-animated-gifs-from-video-files-with-python>`_.
+It can be used to edit your personal videos, or to automatize the creation of videos, for instance on a webserver. See the :ref:`gallery` for examples of projects using MoviePy.
Let us put together a few demonstration clips (you will find the code for most of these in the :ref:`examples`): ::
import os
- from moviepy.editor import *
+ from moviepy.editor import VideoFileClip, concatenate
files = sorted( os.listdir("clips/") )
clips = [ VideoFileClip('clips/%s'%f) for f in files]
video = concatenate(clips, transition = VideoFileClip("logo.avi"))
- video.to_videofile("demos.avi",fps=25, codec="mpeg4")
+ video.to_videofile("demos.mp4",fps=25) # many options available !
.. raw:: html
@@ -27,9 +27,7 @@ Let us put together a few demonstration clips (you will find the code for most o
</center>
MoviePy is an open source software originally written by Zulko_ and released under the MIT licence.
-It is hosted on Github_, where you can push improvements, report bugs and ask for help.
-
-**Very New:** there is now a MoviePy forum on Reddit_ and a mailing list on librelist_ .
+It is hosted on Github_, where you can push improvements, report bugs and ask for help. There is also a MoviePy forum on Reddit_ and a mailing list on librelist_ .
.. raw:: html
diff --git a/moviepy/Clip.py b/moviepy/Clip.py
index ed3269d2..9b99333e 100644
--- a/moviepy/Clip.py
+++ b/moviepy/Clip.py
@@ -10,6 +10,7 @@ import numpy as np
from moviepy.decorators import ( apply_to_mask,
apply_to_audio,
time_can_be_tuple,
+ requires_duration,
outplace)
@@ -373,3 +374,34 @@ class Clip:
return newclip.set_duration(self.duration - (tb - ta))
else:
return newclip
+
+ @requires_duration
+ def iter_frames(self, fps=None):
+ """ Iterates over all the frames of the clip.
+
+ Returns each frame of the clip as a HxWxN np.array,
+ where N=1 for mask clips and N=3 for RGB clips.
+
+ This function is not really meant for video editing.
+ It provides an easy way to do frame-by-frame treatment of
+ a video, for fields like science, computer vision...
+
+ The ``fps`` (frames per second) parameter is optional if the
+ clip already has a ``fps`` attribute.
+
+ Examples
+ ---------
+
+ >>> # prints the maximum of red that is contained
+ >>> # on the first line of each frame of the clip.
+ >>> from moviepy.editor import VideoFileClip
+ >>> myclip = VideoFileClip('myvideo.mp4')
+ >>> print ( [frame[0,:,0].max()
+ for frame in myclip.iter_frames()])
+ """
+
+ if fps is None:
+ fps = self.fps
+
+ for t in np.arange(0, self.duration, 1.0/fps):
+ yield self.get_frame(t)
diff --git a/moviepy/audio/AudioClip.py b/moviepy/audio/AudioClip.py
index 8f8f61b7..c3875627 100644
--- a/moviepy/audio/AudioClip.py
+++ b/moviepy/audio/AudioClip.py
@@ -14,7 +14,7 @@ if sys.version_info < (3,):
class AudioClip(Clip):
- """Base class for audio clips.
+ """ Base class for audio clips.
See ``SoundClip`` and ``CompositeSoundClip`` for usable classes.
diff --git a/moviepy/audio/io/ffmpeg_audiowriter.py b/moviepy/audio/io/ffmpeg_audiowriter.py
index 7bb72d5c..3f834d6e 100644
--- a/moviepy/audio/io/ffmpeg_audiowriter.py
+++ b/moviepy/audio/io/ffmpeg_audiowriter.py
@@ -120,7 +120,8 @@ class FFMPEG_AudioWriter:
def close(self):
self.proc.stdin.close()
- self.proc.stderr.close()
+ if self.proc.stderr is not None:
+ self.proc.stderr.close()
self.proc.wait()
del self.proc
diff --git a/moviepy/version.py b/moviepy/version.py
index e8329ec7..1b051f89 100644
--- a/moviepy/version.py
+++ b/moviepy/version.py
@@ -1 +1 @@
-__version__ = "0.2.1.8.02"
+__version__ = "0.2.1.8.03"
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index da77cc21..5140c85d 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -790,39 +790,6 @@ class VideoClip(Clip):
else:
return self
-
- @requires_duration
- def iter_frames(self, fps=None):
- """ Iterates over all the frames of the clip.
-
- Returns each frame of the clip as a HxWxN np.array,
- where N=1 for mask clips and N=3 for RGB clips.
-
- This function is not really meant for video editing.
- It provides an easy way to do frame-by-frame treatment of
- a video, for fields like science, computer vision...
-
- The ``fps`` (frames per second) parameter is optional if the
- clip already has a ``fps`` attribute.
-
- Examples
- ---------
-
- >>> # prints the maximum of red that is contained
- >>> # on the first line of each frame of the clip.
- >>> from moviepy.editor import VideoFileClip
- >>> myclip = VideoFileClip('myvideo.mp4')
- >>> for frame in myclip.iter_frames():
- >>> print ( frame[0,:,0].max() )
-
- """
-
- if fps is None:
- fps = self.fps
-
- for t in np.arange(0, self.duration, 1.0/fps):
- yield self.get_frame(t)
-
#----------------------------------------------------------------
# Audio
diff --git a/moviepy/video/io/VideoFileClip.py b/moviepy/video/io/VideoFileClip.py
index abc188ce..ad9967ef 100644
--- a/moviepy/video/io/VideoFileClip.py
+++ b/moviepy/video/io/VideoFileClip.py
@@ -12,7 +12,7 @@ class VideoFileClip(VideoClip):
A video clip originating from a movie file. For instance: ::
>>> clip = VideofileClip("myHolidays.mp4")
- >>> clip2 = VideofileClip("myMaskVideo.avi",ismask = True)
+ >>> clip2 = VideofileClip("myMaskVideo.avi")
Parameters
@@ -21,9 +21,6 @@ class VideoFileClip(VideoClip):
filename:
The name of the video file. It can have any extension supported
by ffmpeg: .ogv, .mp4, .mpeg, .avi, .mov etc.
-
- ismask:
- Set this to `True` if the clip is going to be used as a mask.
has_mask:
Set this to 'True' if there is a mask included in the videofile.
@@ -47,11 +44,11 @@ class VideoFileClip(VideoClip):
"""
- def __init__(self, filename, ismask=False, has_mask=False,
+ def __init__(self, filename, has_mask=False,
audio=True, audio_buffersize = 200000,
audio_fps=44100, audio_nbytes=2, verbose=False):
- VideoClip.__init__(self, ismask)
+ VideoClip.__init__(self)
# Make a reader
pix_fmt= "rgba" if has_mask else "rgb24"
diff --git a/moviepy/video/io/ffmpeg_writer.py b/moviepy/video/io/ffmpeg_writer.py
index fc0f25b3..b9b08b3f 100644
--- a/moviepy/video/io/ffmpeg_writer.py
+++ b/moviepy/video/io/ffmpeg_writer.py
@@ -144,6 +144,8 @@ class FFMPEG_VideoWriter:
def close(self):
self.proc.stdin.close()
+ if self.proc.stderr is not None:
+ self.proc.stderr.close()
self.proc.wait()
del self.proc