MoviePy – Creating Video Clip

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing matplotlib and numpy
import matplotlib.pyplot as plt
import numpy as np
  
# importig 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)

chevron_right


Output :

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

                                                                                                                       
Moviepy - Done !
Moviepy - video ready __temp__.mp4

Another example

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing matplotlib and numpy
import matplotlib.pyplot as plt
import numpy as np
  
# importig 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)

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.