1:安装moviepy库
2:安装IPython库
代码如下:
from moviepy.editor import * from IPython.display import Image def Base(path="2.mp4", duration=((0, 4.8), (0, 6.3)), savepath=r"out11.gif"): VideoFileClip(path). subclip(duration[0], duration[1]). speedx(0.3). resize(0.5). write_gif(savepath, fps=10, program="ffmpeg") # 截取想要的部分的小姐姐图像 def SecondLevel(path="2.mp4", duration=((0, 4.8), (0, 6.3)), savepath="out1.gif", region=(0, 0, 540, 480)): VideoFileClip(path). subclip(duration[0], duration[1]). crop(x1=region[0], y1=region[1], x2=region[2], y2=region[3]). speedx(0.3). resize(0.5). write_gif(savepath, fps=10, program="ffmpeg") def ThirdLevel(path="2.mp4", duration=((0, 4.8), (0, 6.3)), savepath="out2.gif", region=(0, 0, 540, 480), text="菜鸟学python"): video = VideoFileClip(path). subclip(duration[0], duration[1]). crop(x1=region[0], y1=region[1], x2=region[2], y2=region[3]). speedx(0.3). resize(0.5) # 创建文字 text = TextClip(text, fontsize=20, color='white', interline=25, font="FangSong") .set_position((2,1)). set_duration(video.duration) compose = CompositeVideoClip([video, text]) compose.write_gif(savepath, program="ffmpeg") if __name__ == '__main__': ThirdLevel()
代码解析
这里运行会报一个错误:
解决办法:https://blog.csdn.net/kd_2015/article/details/80157713 此文章又详细的解决步骤,我就不再记录了
然后重新运行你的代码,生成新的图片就是你需要的动态表情包加文字了