Skip to content
Related Articles

Related Articles

MoviePy – Creating Video Clip
  • Last Updated : 05 May, 2021

In this article we will see how we can create a video clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. Video is formed by the frames, combination of frames creates a video each frame is an individual image. VideoClip is the base class for all the other video clips in MoviePy. If all you want is to edit video files, we will never need it. This class is practical when we want to make animations from frames that are generated by another library. All we need is to define a function make_frame(t) which returns a HxWx3 numpy array (of 8-bits integers) representing the frame at time t. 
 

In order to do this we will use VideoClip method
Syntax : VideoClip(make_frame, duration) 
Argument : It takes method and duration as argument
Return : It returns VideoClip object 
 

Below is the implementation 
 

Python3




# importing matplotlib and numpy
import matplotlib.pyplot as plt
import numpy as np
 
# importing movie py libraries
from moviepy.editor import VideoClip
from moviepy.video.io.bindings import mplfig_to_npimage
 
# numpy array
x = np.linspace(-2, 2, 200)
 
# matplot subplot
fig, ax = plt.subplots()
duration = 2
 
# method to get frames
def make_frame(t):
     
    # clear
    ax.clear()
     
    # plotting line
    ax.plot(x, np.sin(x + 2 * np.pi / duration * t), lw = 3)
    ax.set_ylim(-1.5, 2.5)
     
    # returning mumpy image
    return mplfig_to_npimage(fig)
 
# creating Video Clip
clip = VideoClip(make_frame, duration = 3)
 
# displaying clip
clip .ipython_display(fps = 20, loop = True, autoplay = True)

Output : 
 



Moviepy - Building video __temp__.mp4.
Moviepy - Writing video __temp__.mp4

                                                                                                                       
Moviepy - Done !
Moviepy - video ready __temp__.mp4

 

Another example 
 

Python3




# importing matplotlib and numpy
import matplotlib.pyplot as plt
import numpy as np
 
# importing movie py libraries
from moviepy.editor import VideoClip
from moviepy.video.io.bindings import mplfig_to_npimage
 
# numpy array
x = np.linspace(-4, 4, 100)
 
# matplot subplot
fig, ax = plt.subplots()
 
duration = 2
 
# method to get frames
def make_frame(t):
     
    # clear
    ax.clear()
     
    # plotting line
    ax.plot(x, np.cos(x + 4 * np.pi / duration * t), lw = 3)
    ax.set_ylim(-1.5, 2.5)
     
    # returning mumpy image
    return mplfig_to_npimage(fig)
 
# creating Video Clip
clip = VideoClip(make_frame, duration = 3)
 
# displaying clip
clip .ipython_display(fps = 20, loop = True, autoplay = True)

Output : 
 

Moviepy - Building video __temp__.mp4.
Moviepy - Writing video __temp__.mp4

                                                                                                                       
Moviepy - Done !
Moviepy - video ready __temp__.mp4

 

 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :