ffmpeg 实现 MP4 与 GIF 的互转

在 Mac OSX 上使用 [Homebrew][1] 安装 ffmpeg

   [1]: http://brew.sh/

brew install ffmpeg

brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-libass --with-libquvi --with-libvorbis --with-libvpx --with-opus --with-x265

brew update && brew upgrade ffmpeg

将视频 MP4 转化为 GIF

ffmpeg -i small.mp4 small.gif

转化视频中的一部分为 GIF

ffmpeg -t 3 -ss 00:00:02 -i small.webm small-clip.gif

从视频中第二秒开始,截取时长为3秒的片段转化为 gif

转化高质量 GIF

默认转化是中等质量模式,若要转化出高质量的 gif,可以修改比特率

ffmpeg -i small.mp4 -b 2048k small.gif

将 GIF 转化为 MP4

ffmpeg -f gif -i animation.gif animation.mp4

也可以将 gif 转为其他视频格式

ffmpeg -f gif -i animation.gif animation.mpeg

ffmpeg -f gif -i animation.gif animation.webm

获取 GIF 的第一帧图片

使用 ImageMagick 可以方便第提取 gif 图片的第 N 帧图像。

安装 ImageMagick

brew install imagemagick

提取第一帧

convert 'animation.gif[0]' animation-first-frame.gif

通过 [0] 就可以提取出 gif 的第一帧图像。

参考资料
FFmpeg CompilationGuide/MacOSX
Convert Video to GIF or GIF to Video
Get the First Frame of an Animated GIF with ImageMagick
Create an Image Preview from a Video