v5tech / notes

notes
https://ameizi.gitee.io/notes
MIT License
1.53k stars 378 forks source link

使用 ffmpeg 实现 MP4 与 GIF 的互转 #95

Open v5tech opened 9 years ago

v5tech commented 9 years ago

http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert

在 Mac OSX 上使用 Homebrew 安装 ffmpeg:

 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 的第一帧图像。