MoviePy – Creating Video Clip
Last Updated :
23 Feb, 2022
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
import matplotlib.pyplot as plt
import numpy as np
from moviepy.editor import VideoClip
from moviepy.video.io.bindings import mplfig_to_npimage
x = np.linspace( - 2 , 2 , 200 )
fig, ax = plt.subplots()
duration = 2
def make_frame(t):
ax.clear()
ax.plot(x, np.sin(x + 2 * np.pi / duration * t), lw = 3 )
ax.set_ylim( - 1.5 , 2.5 )
return mplfig_to_npimage(fig)
clip = VideoClip(make_frame, duration = 3 )
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
import matplotlib.pyplot as plt
import numpy as np
from moviepy.editor import VideoClip
from moviepy.video.io.bindings import mplfig_to_npimage
x = np.linspace( - 4 , 4 , 100 )
fig, ax = plt.subplots()
duration = 2
def make_frame(t):
ax.clear()
ax.plot(x, np.cos(x + 4 * np.pi / duration * t), lw = 3 )
ax.set_ylim( - 1.5 , 2.5 )
return mplfig_to_npimage(fig)
clip = VideoClip(make_frame, duration = 3 )
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
Share your thoughts in the comments
Please Login to comment...