MoviePy – 将视频文件保存为 GIF
在本文中,我们将了解如何在 MoviePy 中将视频文件剪辑保存为 GIF。 MoviePy 是一个用于视频编辑的Python模块,可用于视频和 GIF 的基本操作。视频是由帧组成的,帧的组合创建一个视频,每一帧都是一个单独的图像。图形交换格式是一种位图图像格式,由在线服务提供商 CompuServe 的一个团队开发。
In order to do this we will use write_gif
method with the VideoFileClip object
Syntax : clip.write_gif(name)
Argument : It takes string i.e file name as argument
Return : It returns None
下面是实现
# Import everything needed to edit video clips
from moviepy.editor import *
# loading video dsa gfg intro video
clip = VideoFileClip("dsa_geek.webm")
# getting only first 3 seconds
clip = clip.subclip(0, 3)
# saving video clip as gif
clip.write_gif("gfg_gif.gif")
# loading gif
gif = VideoFileClip("gfg_gif.gif")
# showing gif
gif.ipython_display()
输出 :
MoviePy - Building file gfg_gif.gif with imageio.
Moviepy - Building video __temp__.mp4.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4
另一个例子
# Import everything needed to edit video clips
from moviepy.editor import *
# loading video gfg
clip = VideoFileClip("geeks.mp4")
# getting only first 3 seconds
clip = clip.subclip(0, 3)
# saving video clip as gif
clip.write_gif("intro_gif.gif")
# loading gif
gif = VideoFileClip("intro_gif.gif")
# showing gif
gif.ipython_display()
输出 :
MoviePy - Building file intro_gif.gif with imageio.
Moviepy - Building video __temp__.mp4.
Moviepy - Writing video __temp__.mp4
Moviepy - Done !
Moviepy - video ready __temp__.mp4