MoviePy is a Python module for video editing, which can be used for basic operations (like cuts, concatenations, title insertions), video compositing (a.k.a. non-linear editing), video processing, or to create advanced effects. It can read and write the most common video formats, including GIF.
Installation
To install the Movie Editor Library, open terminal and write :
pip install moviepy
Note: This module automatically installs FFmpeg. However, you might prompt to install in some cases.
Even after installing MoviePy there will be some features that can’t be used unless ImageMagick is installed, features like adding text on the video or on GIFs.
Installation of ImageMagick
ImageMagick is not strictly required, only if user want to write texts. It can also be used as a back-end for GIFs although user can do GIFs with MoviePy without ImageMagick. Below is the link for downloading ImageMagick
https://imagemagick.org/script/download.php
Once it is installed, ImageMagick will be automatically detected by MoviePy, except on Windows. Windows users have to go into the moviepy/config_defaults.py file and provide the path to the ImageMagick binary called magick.
IMAGEMAGICK_BINARY = "C:\\Program Files\\ImageMagick_VERSION\\magick.exe" or for some older versions of ImageMagick it will be IMAGEMAGICK_BINARY = "C:\\Program Files\\ImageMagick_VERSION\\convert.exe"
To get video’s used when the article is improved – Click Here
Example 1
We will load the video, and we will cut a clip from the whole video then the video will be rotated upside down, in this example, there is no need for ImageMagick installation.
Below is the implementation
Python3
# Import everything needed to edit video clips from moviepy.editor import * # loading video dsa gfg intro video clip = VideoFileClip( "dsa_geek.webm" ) # clipping of the video # getting video for only starting 10 seconds clip = clip.subclip( 0 , 10 ) # rotating video by 180 degree clip = clip.rotate( 180 ) # Reduce the audio volume (volume x 0.5) clip = clip.volumex( 0.5 ) # showing clip clip.ipython_display(width = 280 ) |
Output :
Moviepy - Building video __temp__.mp4. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4
Example 2:
We will load the video and we will cut a clip from the whole video then we will add text in the video, in this example we have to install ImageMagick otherwise it will not work.
Below is the implementation
Python3
# Import everything needed to edit video clips from moviepy.editor import * # loading video dsa gfg intro video clip = VideoFileClip( "dsa_geek.webm" ) # clipping of the video # getting video for only starting 10 seconds clip = clip.subclip( 0 , 10 ) # Reduce the audio volume (volume x 0.8) clip = clip.volumex( 0.8 ) # Generate a text clip txt_clip = TextClip( "Lazyroar" , fontsize = 70 , color = 'white' ) # setting position of text in the center and duration will be 10 seconds txt_clip = txt_clip.set_pos( 'center' ).set_duration( 10 ) # Overlay the text clip on the first video clip video = CompositeVideoClip([clip, txt_clip]) # showing video video.ipython_display(width = 280 ) |
Output :
Moviepy - Building video __temp__.mp4. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4
Exporting Video files:
You need function write_videofile(arg1, arg2), If you do not, it won’t reflect on the hard-disk file as the file was loaded in RAM.
Python3
import moviepy.editor as me # Our focus is on how to export video file, # you can use any code, or just refer following code ''' vid= me.VideoFileClip('Video_1.mp4') # Grabbing Object from storage # Creating a page with 'My Title' text in red colour with given size and background colour white and fontsize 30 ''' SYNTAX: obj = me.TextClip( "Text That you want" , color = '{as string}' , size = (as, tuple ), bg_color = 'as string' , fontsize = int ) ''' title=me.TextClip('My Title',color='red',size=(1920,1000),bg_color='white',fontsize=30) # title.set_duration(int seconds) title_clip_ = title.set_duration(3) #For 3 seconds My Title willbe shown in video render=me.concatenate_videoclips([title_clip_,vid]) # Combining our manually created Video i.e My Title of 3 sec with grabbed video ''' me.write_videofile( "Name you want" .mp4,endcoding) # If successfully executor there will be file.mp4 or at path # If you have explicitly mentioned else in same folder as program print ( "Done" ) |
Output:
Merging Video files –
We can merge 2 video files to a single file in order of our requirement
Python3
import moviepy.editor ''' Grabbing The video's from storage syntax: variable_holding_video_name= moviepy.editor.VideoFileCLip("{{Filename}}.{{extension}}") ''' clip_1 = moviepy.editor.VideoFileClip( "Video_1.mp4" ) clip_2 = moviepy.editor.VideoFileClip( "Video_2.mp4" ) ''' TO join the video's i.e. concatenate the videos' use function concatenate_videoclips(list_of_clips_to_mearged) syntax: variable_to_hold_mearged_video= moviepy.editor.concatenate_videclips([video_1,Video_2,. . .]) ''' Mearged_video = moviepy.editor.concatenate_videoclips([clip_1,clip_2]) # Saving File as output.mp4 in same folder # libx264 is encoding lib for creating video stream(H.264) Mearged_video.write_videofile( "Output.mp4" ,codec = 'libx264' ) print ( "Done" ) |
Output:
To know the length of the video:
Python3
import moviepy.editor as me # Grabbing file vid = me.VideoFileClip( "Video_1.mp4" ) print ( str (vid.duration)) |
Output:
9.04
Advantages of MoviePy
- Simple: Basic operations can be done in one line, code is easy to learn and easy to understand for newcomers.
- Flexible: Users have total control over the frames of the video and audio, and creating their own effects is easy as Py.
- Portable: The code uses very common software such as Numpy and FFMPEG. And can run on almost any machine with almost any version of Python.
Disadvantages of MoviePy :
- MoviePy cannot yet stream videos (read from a webcam, or render a video live on a distant machine)
- It is not really designed for video processing involving many successive frames of a movie (like video stabilization, you’ll need another software for that)
- Memory problems can arise if the user use many video, audio, and image sources at the same time (>100)