MoviePy - 创建视频剪辑
在本文中,我们将了解如何在 MoviePy 中创建视频剪辑。 MoviePy 是一个用于视频编辑的Python模块,可用于视频和 GIF 的基本操作。视频是由帧组成的,帧的组合创建一个视频,每一帧都是一个单独的图像。 VideoClip 是 MoviePy 中所有其他视频剪辑的基类。如果您只想编辑视频文件,我们将永远不需要它。当我们想从另一个库生成的帧制作动画时,这个类很实用。我们只需要定义一个函数make_frame(t),它返回一个 HxWx3 numpy 数组(8 位整数),表示时间 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
下面是实现
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 numpy 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)
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 numpy 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)
输出 :
Moviepy - Building video __temp__.mp4.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4
另一个例子
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 numpy 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)
输出 :
Moviepy - Building video __temp__.mp4.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4