Video editing with Python
added "interactive show". Add 'r' before paths in conf.py. Rewrote crash course
Zulko3a9a39da5bcc
diff --git a/docs/FAQ.rst b/docs/FAQ.rstindex d07ff0a7..21c16686 100644--- a/docs/FAQ.rst+++ b/docs/FAQ.rst@@ -22,10 +22,7 @@ readable only on some readers like VLC.have a deprecated version of FFMPEG, install a recent version from thewebsite, not from your OS's repositories ! (see :ref:`installation`).-- **I get an error when I use texts in my video**: If you are on-Windows, this is due to a name clash, see :ref:`installation`. Also,-make sure that you are using a recent version of ImageMagick.-+- **Previewing videos make them slower than they are**. It means that your computer is not good enough to render the clip in real time. Don't hesitate to play with the options of ``preview``: for instance, lower the fps of the sound (11000 Hz is still fine) and the video. Also, downsizing your video with ``resize`` can help... _Github: https://github.com/Zulko/moviepy.. _Reddit: http://www.reddit.com/r/moviepy/diff --git a/docs/crash_course/crash_course.rst b/docs/crash_course/crash_course.rstindex 1308b7b4..f0cc1314 100644--- a/docs/crash_course/crash_course.rst+++ b/docs/crash_course/crash_course.rst@@ -46,11 +46,11 @@ MoviePy uses the software ``ffmpeg`` to read frames from video and audio files.The clip objects~~~~~~~~~~~~~~~~~~~-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.+The most common objects in MoviePy are called clips. A clip (let's call it ``my_clip``) can be either an audio clip or a video clip. It has different attributes, like ``my_clip.duration`` (indicating its duration in seconds), ``clip.start`` (indicating the time in seconds where the clip should start playing when mixed with other videos), ``my_clip.w`` (the width in pixel of the clip), etc.-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 video clip ``my_clip`` can have two special attributes which are themselves clips: ``my_clip.audio`` is an audio clip representing the audio track of the vidceo clip. ``my_clip.mask`` is a special kind of video clip which defines the transparency of ``my_clip``.-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.+A clip also has methods which enable to create a derivative of this clip. For instance ``my_clip.subclip(2,7)`` creates a new clip which is like ``my_clip`` cut between seconds 2 and 7.Classes of video clips~~~~~~~~~~~~~~~~~~~~~~@@ -73,7 +73,7 @@ Composite video clips are video clips which will display the contents of severalvideo = concatenate([clip1,clip2,clip3])-Now ``video`` is a clip that plays the clips 1, 2, and 3 one after the other. You can also play a transition clip between the clips with the option ``transition=myClip``.+Now ``video`` is a clip that plays the clips 1, 2, and 3 one after the other. You can also play a transition clip between the clips with the option ``transition=my_clip``.Next, you have the `~moviepy.video.compositing.CompositeVideoClip.CompositeVideoClip`: ::@@ -93,13 +93,13 @@ So for instance your composition will look like ::clip2.set_start(5), # start at t=5sclip3.set_start(9)]) # start at t=9s-Because the three clips overlap, we can make them appear with a fading-in effect. Here we go for fadein effects that last 1.5 seconds: ::+In the example above, maybe ``clip2`` will start before ``clip1`` is over. In this case you can make ``clip2`` appear with a *fade-in* effect of one second: ::video = CompositeVideoClip([clip1, # starts at t=0- clip2.set_start(50).fadein(1.5),- clip3.set_start(90).fadein(1.5)])+ clip2.set_start(5).fadein(1),+ clip3.set_start(9)])-Finally, if ``clip2`` and ``clip3`` are smaller than ``clip1``, you can decide where they will appear in the composition: ::+Finally, if ``clip2`` and ``clip3`` are smaller than ``clip1``, you can decide where they will appear in the composition by setting their position. Here we indicate the coordinates of the top-left pixel: ::video = CompositeVideoClip([clip1,clip2.set_pos((45,150)),@@ -109,6 +109,8 @@ Note that there are many ways to specify the position: ::clip2.set_pos((45,150)) # x=45, y=150 , in pixels+ clip2.set_pos("center") # automatically centered+# clip2 is horizontally centered, and at the top of the pictureclip2.set_pos(("center","top"))@@ -127,45 +129,63 @@ When indicating the position keep in mind that the ``y`` coordinate has its zero.. _renderingAClip:-Rendering a video clip-~~~~~~~~~~~~~~~~~~~~~~~--A clip can be previewed (i.e. generated and displayed at the same time)-or be rendered directly to a file. Note that previewing requires to-have the Python package PyGame installed.+Wrting a video clip to a file+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-To preview just one frame of the clip, use one of these: ::+To write a clip as a video file, use ::- myClip.show() # shows the first frame of the clip- myClip.show(10.5) # shows the frame of the clip at t=10.5s--To preview the clip, type ::+ my_clip.write_videofile("movie.mp4") # default: 'libx264', 24 fps+ my_clip.write_videofile("movie.mp4",fps=15, codec='mpeg4')+ my_clip.write_videofile("movie.mp4",audio=False) # don't render audio.- myClip.preview() # preview with default fps=15- myClip.preview(fps=25)- myClip.preview(fps=15, audio=False) # Play the clip with no sound.+You can also use other codecs to write ``.webm`` or ``.ogv`` files.-Note that the ``fps`` can be any number, independently of the ``fps`` of your different sources.-To write a clip as a video file, use ::-- myClip.write_videofile("myEditedMovie.avi") # default: codec 'libx264' fps 24- myClip.write_videofile("myEditedMovie.avi",fps=15, codec='mpeg4')- myClip.write_videofile("myEditedMovie.avi",audio=False) # don't render the audio.--Alternatively you can also save a video clip as an animated GIF with ::+Sometimes it is impossible for MoviePy to guess the ``duration`` attribute of the clip (keep in mind that some clips, like ImageClips displaying a picture, have *a priori* an infinite duration). Then, the ``duration`` must be set manually with ``clip.set_duration``: ::++ # Make a video showing a flower for 5 seconds+ my_clip = Image("flower.jpeg") # has infinite duration+ my_clip.write_videofile("flower.mp4") # Will fail ! NO DURATION !+ my_clip.set_duration(5).write_videofile("flower.mp4") # works !+++Writing an animated GIF+~~~~~~~~~~~~~~~~~~~~~~~~~++To write your video as an animated GIF, use ::++ my_clip.write_gif('test.gif', fps=12)++If your computer has enough RAM (say, at least 2GB), you can use its faster version++ my_clip.write_gif('test.gif', fps=12)- myClip.to_gif('test.gif')See `this blog post <http://zulko.github.io/blog/2014/01/23/making-animated-gifs-from-video-files-with-python>`_ for more informations on making GIFs with MoviePy.+Previewing a video clip+~~~~~~~~~~~~~~~~~~~~~~~+A clip previewed, i.e. generated and displayed at the same time, which is faster than re-generating the file and opening it every time you change your script. Previewing requires to have the Python package PyGame installed, and will only work if PyGame is initialized, which will be the case+if you started with ``from moviepy import *``.-Sometimes it is impossible for MoviePy to guess the ``duration`` attribute of the clip (keep in mind that some clips, like ImageClips displaying a picture, have *a priori* an infinite duration). Then, the ``duration`` must be set manually with ``clip.set_duration``: ::+To preview just one frame of the clip, use one of these: ::++ my_clip.show() # shows the first frame of the clip+ my_clip.show(10.5) # shows the frame of the clip at t=10.5s+ my_clip.show(interactive = True)- myClip = Image("flower.jpeg") # has infinite duration- Image("flower.jpeg").preview() # Will fail ! NO DURATION !- myClip.set_duration(5).preview() # will show flowers for 5 seconds+The last line allows you to click anywhere on the clip and get the position and color of the pixel on which you clicked (press Escape to terminate).++To preview the clip, you must specify the frames-per-second ::++ my_clip.preview() # preview with default fps=15+ my_clip.preview(fps=25)+ my_clip.preview(fps=15, audio=False) # don't play sound.++The ``fps`` can be any number, independently of the ``fps`` of your different sources.++While the clip is getting prewieved, just can just click anywhere on the clip to get the time, position and color of the pixel you clicked... _CCaudioClips:@@ -186,11 +206,12 @@ Like video clips, audio clips can be cut (with ``clip.subclip``) modified (with- When you cut a video clip with ``videoclip.subclip(20,25)`` then the sound will also be cut, i.e. the resulting clip will have an audio clip ``videoclip.audio.subclip(20,25)``.- When you put several clips together in a CompositeVideoClip, then the sound of the CompositeVideoClip will be the composition of the sounds of the different video clips.-For an example, you can refer to :ref:`soundexample`. Like video clips, sound clips have a ``get_frame`` attribute, and creating new audio clips by modifying or putting together other audio clips does not take place in the memory. The actual sounds of the audio clips are only computed when we ask to play them or to write them to a file. You can do that as follows: ::+For an example, you can refer to :ref:`soundexample`. Like video clips, sound clips have a ``get_frame`` attribute, and creating new audio clips by modifying or putting together other audio clips does not eat the memory. The actual sounds of the audio clips are only computed when we ask to play them or to write them to a file. You can do that as follows: ::audioclip.preview() # default fps: 22050audioclip.preview(fps=44100)- audioclip.to_soundfile('myclip.wav',fps=44100) #default fps: 22050+ audioclip.to_audiofile('my_clip.mp3',fps=44100) #default fps: 22050+Operations on a clip~~~~~~~~~~~~~~~~~~~~~@@ -218,19 +239,19 @@ where ``args`` represent arguments and/or keyword arguments. To apply these funcbut this is not easy to read. To have a clearer syntax you can use ``clip.fx``: ::- newclip = clip.fx( effect_1, args1).\- fx( effect_2, args2).\- fx( effect_3, args3)+ newclip = (clip.fx( effect_1, args1)+ .fx( effect_2, args2)+ .fx( effect_3, args3))Much better ! There are already many effects implemented in the modules ``moviepy.video.fx`` and ``moviepy.audio.fx``. The fx methods in these modules are automatically applied to the sound and the mask of the clip if it is relevant, so that you don't have to worry about modifying these. For practicality, when you use ``from moviepy import.editor *``, these two modules are loaded as ``vfx`` and ``afx``, so you may write something like ::from moviepy.editor import *- clip = VideoFileClip("myvideo.avi").\- fx( vfx.resize, width=460).\ # resize (keep aspect ratio)- fx( vfx.speedx, 2).\ # double speed- fx( vfx.colorx, 0.5) # darken (decreases the RGB values)+ clip = (VideoFileClip("myvideo.avi")+ .fx( vfx.resize, width=460) # resize (keep aspect ratio)+ .fx( vfx.speedx, 2) # double speed+ .fx( vfx.colorx, 0.5)) # darken the picture-For convenience, frequently used methods such as ``resize`` can be called in a simpler way: ``clip.resize(...)`` instead of ``clip.fx( vfx.resize, ...)``+For convenience, when you use ``moviepy.editor``, frequently used methods such as ``resize`` can be called in a simpler way: ``clip.resize(...)`` instead of ``clip.fx( vfx.resize, ...)``clip.fl@@ -241,17 +262,17 @@ You can modify a clip as you want using custom *filters* with ``clip.fl_time``,You can change the timeline of the clip with ``clip.fl_time`` like this: ::- modifiedClip1 = myClip.fl_time(lambda t: 3*t)- modifiedClip2 = myClip.fl_time(lambda t: 1+sin(t))+ modifiedClip1 = my_clip.fl_time(lambda t: 3*t)+ modifiedClip2 = my_clip.fl_time(lambda t: 1+sin(t))-Now the clip ``modifiedClip1`` plays the same as ``myClip``, only three times faster, while ``modifiedClip2`` will play ``myClip`` by oscillating between the times t=0s and t=2s. Note that in the last case you have created a clip of infinite duration (which is not a problem for the moment).+Now the clip ``modifiedClip1`` plays the same as ``my_clip``, only three times faster, while ``modifiedClip2`` will play ``my_clip`` by oscillating between the times t=0s and t=2s. Note that in the last case you have created a clip of infinite duration (which is not a problem for the moment).You can also modify the display of a clip with ``clip.fl_image``. The following takes a clip and inverts the green and blue channels of the frames: ::def invert_green_blue(image):return image[:,:,[0,2,1]]- modifiedClip = myClip.fl_image( invert_green_blue )+ modifiedClip = my_clip.fl_image( invert_green_blue )Finally, you may want to process the clip by taking into account both the time and the frame picture. This is possible with the method ``clip.fl(filter)``. The filter must be a function which takes two arguments and returns a picture. the fist argument is a ``get_frame`` method (i.e. a function ``g(t)`` which given a time returns the clip's frame at that time), and the second argument is the time. ::@@ -264,7 +285,7 @@ Finally, you may want to process the clip by taking into account both the time aframe_region = frame[int(t):int(t)+360,:]return frame_region- modifiedClip = myClip.fl( scroll )+ modifiedClip = my_clip.fl( scroll )This will scroll down the clip, with a constant height of 360 pixels.@@ -274,31 +295,14 @@ new effects. The reason is that, when these effects are applied toImageClips, MoviePy will recognize that these methods do not need to be applied to each frame, which willresult in faster renderings.-Tools-~~~~~~--Advanced features of MoviePy that cannot be expressed as an ``fx`` are placed in :module:`moviepy.video.tools` (currently this module contains methods for tracking objects, segmenting, drawing, making credits) and `moviepy.audio.tools` (currently empty, will contain denoisers and utilities for synchronization).--Tips-~~~~~--MoviePy works fine on my 1.5 petaflops supercomputer but when a clip gets very complex the rendering is slow and there is not much we can do.--- Use an interactive shell, like IPython or, better, the IPython notebook. If you don't know these, you don't know what you are missing !-- If a part of your video takes a lot of time to render, save it once and for all as a video, then use this video. Choose codec 'rawvideo' or 'png' for lossless saving.-- Prefer the ``clip.show()`` option, and use it a lot. Only use ``clip.preview()`` when really necessary.-- If the previewing is shaky, it means that your computer is not good enough to render the clip in real time. Don't hesitate to play with the options of ``preview``: for instance, lower the fps of the sound (11000 Hz is still fine) and the video.-- Prototype: design your clips separately. If your composition involves a clip that is not finished yet, replace it temporarily with a basic color clip.-- There are often several ways to produce a same effect with MoviePy, but some ways are faster. For instance don't apply effects to a whole screen video if you are only using one region of the screen afterwards: first crop the selected region, then apply your effects.-- [wishful thinking] Check on the internet or in the examples of this documentation that what you do hasn't been done before. Code shared on the internet has more chances to be optimized.---To go further and learn about all the available options and-functionalities of MoviePy, see the :ref:`examples` and the reference-manual. You can also browse the code of the different video effects in-``moviepy/video/fx`` to give you ideas on how to code your own effects.-+To go further+~~~~~~~~~~~~~~-+The best way to start with moviepy is to use it with the IPython Notebook, as it has autocompletion which will help you find all you need and discover all the available methods.+Advanced features of MoviePy that cannot be expressed as an ``fx`` are placed in :module:`moviepy.video.tools` (currently this module contains methods for tracking objects, segmenting, drawing, making credits, dealing with subtitles) and `moviepy.audio.tools` (currently empty, will contain denoisers and utilities for synchronization).+To go further and learn about all the available options and+functionalities of MoviePy, see the :ref:`examples` and the :ref:`reference_manual`.+You can also browse the code of the different video effects in+``moviepy/video/fx`` to give you ideas on how to code your own effects.\ No newline at end of filediff --git a/docs/ref/ref.rst b/docs/ref/ref.rstindex 4d846187..25e8351c 100644--- a/docs/ref/ref.rst+++ b/docs/ref/ref.rst@@ -1,3 +1,6 @@+.. _reference_manual:++Reference Manual================diff --git a/moviepy/conf.py b/moviepy/conf.pyindex 76602c2f..e434a958 100644--- a/moviepy/conf.py+++ b/moviepy/conf.py@@ -1,21 +1,32 @@"""-Instructions---------------+Configuration MoviePy+This file enables you to specify a configuration for MoviePy. Inparticular you can enter the path to the FFMPEG and ImageMagickbinaries.+Instructions+--------------++FFMPEG_BINARYNormally you can leave this one to its default (None) and MoviePywill detect automatically the right name, which will be either- 'ffmpeg' (on linux) or 'ffmpeg.exe' (on windows)+ 'ffmpeg' (on linux) or 'ffmpeg.exe' (on windows). If you want to+ use a binary at a special location on you disk, enter it like that:++ FFMPEG_BINARY = r"path/to/ffmpeg(.exe)"++ Warning: the 'r' before the path is important, especially on Windows.+IMAGEMAGICK_BINARYFor linux users, 'convert' should be fine.For Windows users, you must specify the path to the ImageMagick'convert' binary. For instance:- "C:\Program Files\ImageMagick-6.8.8-Q16\convert"++ IMAGEMAGICK_BINARY = r"C:\Program Files\ImageMagick-6.8.8-Q16\convert.exe"You can run this file to check that FFMPEG has been detected."""@@ -32,9 +43,7 @@ import subprocess as spdef try_cmd(cmd):try:- proc = sp.Popen(cmd,- stdout=sp.PIPE,- stderr=sp.PIPE)+ proc = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)proc.communicate()except:return False@@ -49,9 +58,9 @@ if FFMPEG_BINARY is None:elif try_cmd(['ffmpeg.exe']):FFMPEG_BINARY = 'ffmpeg.exe'else:- raise IOError("FFMPEG binary not found. Try installing MoviePy"\- " manually and specify the path to the binary in"\- " the file conf.py")+ raise IOError("FFMPEG binary not found. Try installing MoviePy"+ " manually and specify the path to the binary in"+ " the file conf.py")if __name__ == "__main__":if try_cmd([FFMPEG_BINARY]):diff --git a/moviepy/video/io/preview.py b/moviepy/video/io/preview.pyindex de8b875f..e8f2e9de 100644--- a/moviepy/video/io/preview.py+++ b/moviepy/video/io/preview.py@@ -20,7 +20,7 @@ def imdisplay(imarray, screen=None):pg.display.flip()-def show(clip, t=0, with_mask=True):+def show(clip, t=0, with_mask=True, interactive=False):"""Splashes the frame of clip corresponding to time ``t``.@@ -44,8 +44,24 @@ def show(clip, t=0, with_mask=True):if with_mask and (clip.mask != None):import moviepy.video.compositing.CompositeVideoClip as cvcclip = cvc.CompositeVideoClip([clip.set_pos((0,0))])- imdisplay(clip.get_frame(t))+ img = clip.get_frame(t)+ imdisplay(img)+ if interactive:+ result=[]+ while True:+ for event in pg.event.get():+ if event.type == pg.KEYDOWN:+ if (event.key == pg.K_ESCAPE):+ print( "Keyboard interrupt" )+ return result+ elif event.type == pg.MOUSEBUTTONDOWN:+ x,y = pg.mouse.get_pos()+ rgb = img[y,x]+ result.append({'position':(x,y), 'color':rgb})+ print( "position, color : ", "%s, %s"%(+ str((x,y)),str(rgb)))+ time.sleep(.03)@requires_durationdef preview(clip, fps=15, audio=True, audio_fps=22050,@@ -128,25 +144,4 @@ def preview(clip, fps=15, audio=True, audio_fps=22050,t1 = time.time()time.sleep(max(0, t - (t1-t0)) )- imdisplay(img, screen)--def image_preview(clip):-- clip.show()- result=[]- while True:- for event in pg.event.get():- if event.type == pg.KEYDOWN:- if (event.key == pg.K_ESCAPE):- videoFlag.clear()- print( "Keyboard interrupt" )- return result- elif event.type == pg.MOUSEBUTTONDOWN:- x,y = pg.mouse.get_pos()- rgb = img[y,x]- result.append({'time':t, 'position':(x,y),- 'color':rgb})- print( "time, position, color : ", "%.03f, %s, %s"%(- t,str((x,y)),str(rgb)))- return result-+ imdisplay(img, screen)\ No newline at end of file