Skip to content

Video editing with Python

Enhanced docs & gallery. added docstring to TextClip.search

Zulko9ec82b6994de
diff --git a/docs/gallery.rst b/docs/gallery.rst
index a118ef27..39a6f8bc 100644
--- a/docs/gallery.rst
+++ b/docs/gallery.rst
@@ -39,12 +39,12 @@ in the :ref:`examples`.
</div>
-Gifs edited with MoviePy
---------------------------
+Animations edited with MoviePy
+------------------------------
-GIFs from video files
-~~~~~~~~~~~~~~~~~~~~~~
+GIFs made from video files
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This `tutorial
<http://zulko.github.io/blog/2014/01/23/making-animated-gifs-from-video-files-with-python/>`_ gives you the basics to make gifs from video files (cutting, croping, adding text...). The last example shows how to remove a (still) background to keep only the animated part of a video.
@@ -55,11 +55,10 @@ This `tutorial
<a href="http://imgur.com/Fo2BxBK"><img src="http://i.imgur.com/Fo2BxBK.gif" title="Hosted by imgur.com"
style="max-width:50%; height:'auto'; display:block; margin-left: auto;margin-right: auto; margin-bottom:30px;" /></a>
-Animations
-~~~~~~~~~~~~~
+Vector Animations
+~~~~~~~~~~~~~~~~~~~
-This `tutorial
-<http://zulko.github.io/blog/2014/09/20/vector-animations-with-python/>`_ shows how to combine MoviePy with Gizeh to create animations:
+This `tutorial <http://zulko.github.io/blog/2014/09/20/vector-animations-with-python/>`_ shows how to combine MoviePy with Gizeh to create animations:
.. raw:: html
@@ -70,11 +69,47 @@ This `tutorial
It is also possible to combine MoviePy with other graphic librairies like matplotlib, etc.
-.. Rinconcam
+3D animations
+~~~~~~~~~~~~~~~~~~~
-.. aaa~~~~~~~~~~~
+This `tutorial <http://zulko.github.io/blog/2014/11/13/things-you-can-do-with-python-and-pov-ray/>`_ shows how to combine MoviePy with Vapory, a library to render 3D scenes using the free ray-tracer POV-Ray
-.. 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.
+.. raw:: html
+
+ <a href="http://imgur.com/2YdW9yf"><img src="http://i.imgur.com/XN7e2IP.gif"
+ style="max-width:70%; height:'auto'; display:block; margin-left: auto;margin-right: auto; margin-bottom:30px;" /></a>
+
+
+With Vapory and MoviePy you can for instance embed a movie in a 3D scene:
+
+
+.. raw:: html
+
+ <div style="position: relative; padding-bottom: 56.25%; padding-top: 30px; height: 0; margin-bottom:30px; overflow: hidden; margin-left: 5%;">
+ <iframe src="http://youtube.com/v/M9R21SquDSk?rel=0" frameborder="0" allowfullscreen
+ style="position: absolute; top: 0; bottom: 10; left: 0; width: 90%; height: 100%;">
+ </iframe>
+ </div>
+
+
+Or render the result of this physics simulation made with PyODE (`script <https://gist.github.com/Zulko/f828b38421dfbee59daf>`_):
+
+.. raw:: html
+
+ <a href="http://imgur.com/2YdW9yf"><img src="http://i.imgur.com/TdhxwGz.gif"
+ style="max-width:70%; height:'auto'; display:block; margin-left: auto;margin-right: auto; margin-bottom:30px;" /></a>
+
+
+Or use `this script <https://gist.github.com/Zulko/b910c8b22e8e1c01fae6>`_ to make piano animations from MIDI files (which are some sort of electronic sheet music):
+
+
+.. raw:: html
+
+ <div style="position: relative; padding-bottom: 56.25%; padding-top: 30px; height: 0; margin-bottom:30px; overflow: hidden; margin-left: 5%;">
+ <iframe src="http://youtube.com/v/tCqQhmuwgMg?rel=0" frameborder="0" allowfullscreen
+ style="position: absolute; top: 0; bottom: 10; left: 0; width: 90%; height: 100%;">
+ </iframe>
+ </div>
Scientific or technological projects
@@ -86,11 +121,8 @@ Piano rolls transcription to sheet music
This `blog post <http://zulko.github.io/blog/2014/02/12/transcribing-piano-rolls/>`_ 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:
-
.. raw:: html
-
-
<div style="position: relative; padding-bottom: 56.25%; padding-top: 30px; height: 0; margin-bottom:30px; overflow: hidden; margin-left: 5%;">
<iframe src="http://youtube.com/v/V2XCJNZjm4w&hl?rel=0" frameborder="0" allowfullscreen
style="position: absolute; top: 0; bottom: 10; left: 0; width: 90%; height: 100%;">
@@ -102,6 +134,19 @@ This `blog post <http://zulko.github.io/blog/2014/02/12/transcribing-piano-rolls
Misc. Programs and Scripts using MoviePy
------------------------------------------
+Rinconcam
+----------
+
+`Rincomcam <http://www.rinconcam.com/month/2014-03>`_ is a camera which films surfers on the Californian beach of Point Rincon. At the end of each day it cuts together a video, puts it online, and tweets it. Everything is entirely automatized with Python.
+MoviePy is used to add transitions, titles and music to the videos.
+
+
+.. raw:: html
+
+ <a href="http://imgur.com/2YdW9yf"><img src="https://pbs.twimg.com/media/B2_NlnwCMAAingW.jpg"
+ style="max-width:70%; height:'auto'; display:block; margin-left: auto;margin-right: auto; margin-bottom:30px;" /></a>
+
+
Videogrep
~~~~~~~~~~
@@ -115,10 +160,36 @@ Videogrep is a python script written by Sam Lavigne, that goes through the subti
</iframe>
</div>
-Here are Videogrep's introductory `blog post
-<http://lav.io/2014/06/videogrep-automatic-supercuts-with-python/>`_ and the Github project `page <https://github.com/antiboredom/videogrep/>`_.
+Here are `Videogrep's introductory blog post
+<http://lav.io/2014/06/videogrep-automatic-supercuts-with-python/>`_ and the Github `Videogrep page <https://github.com/antiboredom/videogrep/>`_.
+
+If you liked it, also have a look at these Videogrep-inspired projects:
-If you liked it, also have a look at these two Videogrep-inspired projects:
+This `blog post <http://zulko.github.io/blog/2014/06/21/some-more-videogreping-with-python/>`_ attempts to cut a video precisely at the beginning and end of sentences or words: ::
+
+ words = ["Americans", "must", "develop", "open ", "source",
+ " software", "for the", " rest ", "of the world",
+ "instead of", " soldiers"]
+ numbers = [3,0,4,3,4,0,1,2,0,1,0] # take clip number 'n'
+
+ cuts = [find_word(word)[n] for (word,n) in zip(words, numbers)]
+ assemble_cuts(cuts, "fake_speech.mp4")
+
+.. raw:: html
-- This `blog post <http://zulko.github.io/blog/2014/06/21/some-more-videogreping-with-python/>`_ attempts to cut a video precisely at the beginning and end of sentences or words.
-- This `other post <http://zulko.github.io/blog/2014/07/04/automatic-soccer-highlights-compilations-with-python/>`_ uses MoviePy to automatically cut together all the highlights of a soccer game, based on the fact that the crowd cheers louder when something interesting happens.
+ <div style="position: relative; padding-bottom: 56.25%; padding-top: 30px; margin-bottom:30px; height: 0; overflow: hidden; margin-left: 5%;">
+ <iframe src="http://youtube.com/v/iWRYGULFd_c?rel=0" frameborder="0" allowfullscreen
+ style="position: absolute; top: 0; bottom: 10; left: 0; width: 90%; height: 100%;">
+ </iframe>
+ </div>
+
+
+This `other post <http://zulko.github.io/blog/2014/07/04/automatic-soccer-highlights-compilations-with-python/>`_ uses MoviePy to automatically cut together all the highlights of a soccer game, based on the fact that the crowd cheers louder when something interesting happens. All in under 30 lines of Python:
+
+.. raw:: html
+
+ <div style="position: relative; padding-bottom: 56.25%; padding-top: 30px; margin-bottom:30px; height: 0; overflow: hidden; margin-left: 5%;">
+ <iframe src="http://youtube.com/v/zJtWPFX2bA0?rel=0" frameborder="0" allowfullscreen
+ style="position: absolute; top: 0; bottom: 10; left: 0; width: 90%; height: 100%;">
+ </iframe>
+ </div>
diff --git a/docs/getting_started/clips.rst b/docs/getting_started/clips.rst
index c178542a..e7110d0e 100644
--- a/docs/getting_started/clips.rst
+++ b/docs/getting_started/clips.rst
@@ -1,7 +1,7 @@
.. _clips:
-Creating and exporting clips
-==============================
+Creating and exporting video clips
+===================================
Video au audio clips are the central objects of MoviePy. In this section we present the different sorts of clips, how to create them, and how to write them to a file. For informations on modifying a clip (cuts, effects, etc.), see :ref:`effects`. For how to put clips together see :ref:`CompositeVideoClips` and to see how to preview clips before writing a file, refer to :ref:`efficient`.
@@ -53,38 +53,88 @@ VideoClip
:width: 128 px
:align: center
-Note: three next sections are in construction
+Note that clips make with a `make_frame` do not have an explicit frame rate, so you must provide a frame rate (``fps``, frames er second) for ``write_gif`` and ``write_videofile``, and more generally for any methods that requires iterating through the frames.
+
VideoFileClip
"""""""""""""""
-See :py:class:`~moviepy.video.io.VideoFileClip.VideoFileClip`.
+
+A VideoFileClip is a clip read from a video file (most formats are supported) or a GIF file. You load the video as follows: ::
+
+ myclip = VideoFileClip("some_video.avi")
+ myclip = VideoFileClip("some_animation.gif")
+
+Note that these clips will have an ``fps`` (frame per second) attribute, which will be transmitted if you do small modifications of the clip, and will be used by default in ``write_videofile``, ``write_gif``, etc. For instance: ::
+
+ myclip = VideoFileClip("some_video.avi")
+ print (myclip.fps) # prints for instance '30'
+ # Now cut the clip between t=10 and 25 secs. This conserves the fps.
+ myclip2 = myclip.subclip(10, 25)
+ myclip2.write_gif("test.gif") # the gif will have 30 fps
+
+
+For more, see :py:class:`~moviepy.video.io.VideoFileClip.VideoFileClip`.
ImageClip
""""""""""
-See :py:class:`~moviepy.video.VideoClip.ImageClip`.
+
+An ImageClip is a video clip that always displays the same image. You can create one as follows: ::
+
+ myclip = ImageClip("some_picture.jpeg")
+ myclip = ImageClip(somme_array) # a (height x width x 3) RGB numpy array
+ myclip = some_video_clip.to_ImageClip(t='01:00:00') # frame at t=1 hour.
+
+For more, see :py:class:`~moviepy.video.VideoClip.ImageClip`.
+
+Two examples of ImageClip shown below are the TextClip and ColorClip
+
TextClip
"""""""""""""""
-See :py:class:`~moviepy.video.VideoClip.TextClip`.
+
+Generating a TextClip requires to have ImageMagick installed and (for windows users) linked to MoviePy, see the installation instructions.
+
+Here is how you make a textclip (you won't need all these options all the time): ::
+
+ myclip = TextClip("Hello", font='Amiri-Bold')
+
+
+The font can be any font installed on your computer, but ImageMagick will have specific names for it. For instance the *normal* Amiri font will be called ``Amiri-Regular`` while the Impact font will be called ``Impact-Normal``. To get a list of the possible fonts, type ``TextClip.list('fonts')``. To find all the font names related to a given font, use for instance ::
+
+ TextClip.search('Amiri', 'fonts') # Returns all font names containing Amiri
+
+Note also that the use of a stroke (or contour) will not work well on small letters, so if you need a small text with a contour, it is better to generate a big text, then downsize it: ::
+
+ myclip = TextClip("Hello", fontsize=70, stroke_width=5).resize(height=15)
+
+
+TextClips have many, many options: alignment, kerning (distance between the letters), stroke size, background, word wrapping, etc. see :py:class:`~moviepy.video.VideoClip.TextClip` for more.
Mask clips
~~~~~~~~~~~~~~
-A mask is a special video clip which indicates which pixels will be visible when a video clip carrying this mask will be composed with other video clips (see :ref:`CompositeVideoClips`).
+A mask is a special video clip which indicates which pixels will be visible when a video clip carrying this mask will be composed with other video clips (see :ref:`CompositeVideoClips`). Masks are also used to define transparency when you export the clip as GIF file or as a PNG.
The fundamental difference between masks and standard clips is that standard clips output frames with 3 components (R-G-B) per pixel, comprised between 0 and 255, while a mask has just one composant per pixel, between 0 and 1 (1 indicating a fully visible pixel and 0 a transparent pixel). Seen otherwise, a mask is always in greyscale.
-When you create or load a clip that you will use as a mask you need to declare it: ::
+When you create or load a clip that will be used as a mask you need to declare it: ::
+
+ maskclip = ImageClip("my_mask.jpeg", ismask=True)
+ maskclip = VideoFileClip("myvideo.mp4", ismask=True)
+ maskclip = VideoClip(makeframe_function, duration=4, ismask=True)
+
- mclip = VideoClip(makeframe, duration=4, ismask=True)
- mclip = ImageClip("my_mask.jpeg", ismask=True)
- mclip = VideoClip("myvideo.mp4", ismask=True)
-
In the case of video and image files, if these are not already black and white they will be converted automatically.
-Any video clip can be turned into a mask with ``clip.to_mask()``, and a mask can be turned to a standard RGB video clip with ``my_mask_clip.to_RGB()``.
+Then you attach this mask to a clip (which must have the same dimensions) with ``myclip.set_mask(maskclip)``.
+Some image formats like PNG support transparency with an *alpha layer*, which MoviePy will use as a mask: ::
+
+ myclip = ImageClip("image.png", transparent=True) # True is the default
+ myclip.mask # <- the alpha layer of the picture.
+
+Any video clip can be turned into a mask with ``clip.to_mask()``, and a mask can be turned to a standard RGB video clip with ``my_mask_clip.to_RGB()``.
Masks are treated differently by many methods (because their frames are different) but you can do with a mask pretty much everything you can do with a standard clip: you can cut it, edit it, preview it, write it to a video file, make snapshots, etc.
@@ -123,11 +173,23 @@ To write your video as an animated GIF, use ::
Note that this requires ImageMagick installed. Otherwise you can also create the GIF with ffmpeg by adding the option ``program='ffmpeg'``, it will be much faster but won't look as nice and won't be optimized.
-There are many options to optimize the quality and size of a gif. Please refer to :py:meth:`~moviepy.video.VideoClip.VideoClip.write_gif`.
+If the clip has a mask it will be used for transparency.
+
+There are many options to optimize the quality and size of a gif (see :py:meth:`~moviepy.video.VideoClip.VideoClip.write_gif`)
+
+Note that when editing gifs the best way to preview them is in the notebook as explained here: :ref:`ipython_display`
-Note that for editing gifs the best way is to preview them in the notebook as explained here: :ref:`ipython_display`
+For examples of use, see `this blog post <http://zulko.github.io/blog/2014/01/23/making-animated-gifs-from-video-files-with-python>`_ for informations on making GIFs from video files, and `this other post <http://zulko.github.io/blog/2014/09/20/vector-animations-with-python/>`_ for GIF animations with vector graphics.
-See `this blog post <http://zulko.github.io/blog/2014/01/23/making-animated-gifs-from-video-files-with-python>`_ for informations on making GIFs from video files, and `this other post <http://zulko.github.io/blog/2014/09/20/vector-animations-with-python/>`_ for GIF animations with vector graphics.
+Export images
+"""""""""""""""
+
+You can write a frame to an image file with ::
+
+ myclip.save_frame("frame.png") # by default the first frame is extracted
+ myclip.save_frame("frame.jpeg", t='01:00:00') # frame at time t=1h
+
+If the clip has a mask it will be exported as the alpha layer of the image unless you specify ``withmask=False``.
.. _CCaudioClips:
@@ -135,25 +197,31 @@ See `this blog post <http://zulko.github.io/blog/2014/01/23/making-animated-gifs
Audio clips
~~~~~~~~~~~~~
-When you make a video clip from a video containing a soundtrack, like this: ::
-
- clip = VideoFileClip('myHolidays.mp4')
+This section shows how to use MoviePy to create and edit audio clips.
-The resulting clip has an attribute ``clip.audio`` (its soundtrack) which is an audio clip. You can also set the soundtrack of a video clip with ::
-
- new_clip = clip.set_audio(some_audio_clip)
+Note that when you import, cut, mix or concatenate video clips in MoviePy, their audio tracks are automatically handled and you don't need to worry about it. This section is of interest if you just want to edit audio files, or you want custom audio clips for your videos.
-In this section we will see that audio clips can be created, imported, exported like video clips.
+What audioclips are made of
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Note: next sections are in construction
+AudioClips are very similar to video clips in moviepy: they have a length, can be cut and composed the same way, etc. A notable difference is that their method ``myclip.get_frame(t)``, instead of returning a RGB picture array like for video clips, returns just a couple of values between -1 and 1 (e.g.``[-0.4, 0.2]``) representing the left-right stereo sound at time ``t`` (for mono sounds, only one value is returned)
-AudioFileClip
-""""""""""""""
+Creating a new audio clip
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
-See :py:class:`~moviepy.audio.io.AudioFileClip.AudioFileClip`.
+Audio clips can be created from an audio file or from the soundtrack of a video file
+::
+ from moviepy.editor import *
+ audioclip = AudioFileClip("some_audiofile.mp3")
+ audioclip = AudioFileClip("some_video.avi")
+for more, see :py:class:`~moviepy.audio.io.AudioFileClip.AudioFileClip`.
-AudioClip
-""""""""""
+Alternatively you can get the audio track of an already created video clip:
+
+ videoclip = VideoFileClip("some_video.avi")
+ audioclip = videoclip.audio
+
+You can also create an audio clip from a numpy array or a custom function (to be written)
-See :py:class:`~moviepy.audio.AudioClip.AudioClip`
\ No newline at end of file
+For more, see :py:class:`~moviepy.audio.AudioClip.AudioClip`
\ No newline at end of file
diff --git a/docs/getting_started/effects.rst b/docs/getting_started/effects.rst
index ab23c173..a8bca366 100644
--- a/docs/getting_started/effects.rst
+++ b/docs/getting_started/effects.rst
@@ -1,5 +1,7 @@
.. _effects:
+Warning: page in construction.
+
Clips transformations and effects
===================================
diff --git a/docs/getting_started/getting_started.rst b/docs/getting_started/getting_started.rst
index 9904b9fe..456e5673 100644
--- a/docs/getting_started/getting_started.rst
+++ b/docs/getting_started/getting_started.rst
@@ -1,12 +1,13 @@
.. _getting_started:
-These pages explain everything you need to start editing with MoviePy. To go further, have a look at the :ref:`gallery` and the :ref:`examples`.
-
Getting started with MoviePy
------------------------------
+These pages explain everything you need to start editing with MoviePy. To go further, have a look at the :ref:`gallery` and the :ref:`examples`.
+
+
.. toctree::
:maxdepth: 1
@@ -14,5 +15,4 @@ Getting started with MoviePy
clips
compositing
effects
- advanced_tools
efficient_moviepy
\ No newline at end of file
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 08a7e037..fa76d951 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -1114,6 +1114,13 @@ class TextClip(ImageClip):
@staticmethod
def search(string, arg):
- string = string.lower()
- names_list = TextClip.list(arg)
- return [name for name in names_list if string in name.lower()]
+ """ Returns the of all valid entries which contain ``string`` for the
+ argument ``arg`` of ``TextClip``, for instance
+
+ >>> # Find all the available fonts which contain "Courier"
+ >>> print ( TextClip.search('Courier', 'font') )
+
+ """
+ string = string.lower()
+ names_list = TextClip.list(arg)
+ return [name for name in names_list if string in name.lower()]