利用imageio将视频转成gif

话不多说直接上代码:

import imageio
reader = imageio.get_reader('1234.mp4')
fps=reader.get_meta_data()['fps']
imageio.mimsave("test.gif", reader, fps=fps)

四行代码简单搞定

我们可以看到一下reader.get_meta_data()的结果:

{'codec': 'h264',
 'duration': 28.74,
 'ffmpeg_version': '4.1 built with gcc 8.2.1 (GCC) 20181017',
 'fps': 29.97,
 'nframes': inf,
 'pix_fmt': 'yuv420p',
 'plugin': 'ffmpeg',
 'size': (852, 480),
 'source_size': (852, 480)}

可以看到,fps为29.97

注意的是这里的比较大一些,需要对其进行压缩,压缩工具是gifsicle

视频转gif得到的结果默认都是比较大的,我这里得到的结果是290Mb

gifsicle具体安装:

https://www.jianshu.com/p/15bc420679a4

使用:

gifsicle  -O3 test.gif -o new.gif

明显大小变小了很多,不过还是很大

 而且,转出来的gif文件观看不流畅,于是换了一个方法

import imageio
import moviepy.editor as mpy
content = mpy.VideoFileClip("1234.mp4")
content.write_gif("test225.gif")

转出来的大小差不多,但是很流畅,这里看一下结果,由于大小限制,这里只放前几秒的

import imageio
import moviepy.editor as mpy
content = mpy.VideoFileClip("1234.mp4")
c1 = content.subclip((0,0),(0,3))
c1.write_gif("gav2.gif")

然后压缩

gifsicle  -O3 gav2.gif --resize 300x200 -o newgav2.gif

猜你喜欢

转载自blog.csdn.net/zhou_438/article/details/108463926
今日推荐