原文地址:http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/
在 Mac OSX 上使用 Homebrew 安裝 ffmpeg:
brew install ffmpegbrew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-libass --with-libquvi --with-libvorbis --with-libvpx --with-opus --with-x265brew update && brew upgrade ffmpeg
ffmpeg -i small.mp4 small.gif```
[轉(zhuǎn)化視頻中的一部分為 GIF](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#gif)
ffmpeg -t 3 -ss 00:00:02 -i small.webm small-clip.gif
從視頻中第二秒開始,截取時長為3秒的片段轉(zhuǎn)化為 gif
[轉(zhuǎn)化高質(zhì)量 GIF](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#gif-1)
默認(rèn)轉(zhuǎn)化是中等質(zhì)量模式,若要轉(zhuǎn)化出高質(zhì)量的 gif,可以修改比特率
ffmpeg -i small.mp4 -b 2048k small.gif
[視頻屬性調(diào)整](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#section)
[縮放視頻尺寸](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#section-1)
ffmpeg -i big.mov -vf scale=360:-1 small.mov
注意 sacle
值必須是偶數(shù),這里的 -1
表示保持長寬比,根據(jù)寬度值自適應(yīng)高度。
如果要求壓縮出來的視頻尺寸長寬都保持為偶數(shù),可以使用 -2
[加倍速播放視頻](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#section-2)
ffmpeg -i input.mov -filter:v "setpts=0.5*PTS" output.mov
定義幀率 16fps:
ffmpeg -i input.mov -r 16 -filter:v "setpts=0.125*PTS" -an output.mov
[慢倍速播放視頻](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#section-3)
ffmpeg -i input.mov -filter:v "setpts=2.0*PTS" output.mov
[靜音視頻(移除視頻中的音頻)](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#section-4)
ffmpeg -i input.mov -an mute-output.mov
`-an`
就是禁止音頻輸出
[將 GIF 轉(zhuǎn)化為 MP4](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#gif--mp4)
ffmpeg -f gif -i animation.gif animation.mp4
也可以將 gif 轉(zhuǎn)為其他視頻格式
ffmpeg -f gif -i animation.gif animation.mpegffmpeg -f gif -i animation.gif animation.webm
[獲取 GIF 的第一幀圖片](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#gif-)
使用 [ImageMagick](http://www.imagemagick.org/) 可以方便第提取 gif 圖片的第 N 幀圖像。
安裝 ImageMagick
brew install imagemagick
提取第一幀
convert 'animation.gif[0]' animation-first-frame.gif
通過 [0]
就可以提取出 gif 的第一幀圖像。
[GIF 轉(zhuǎn)出來的 MP4 播放不了?](http://note.rpsh.net/posts/2015/04/21/mac-osx-ffmpeg-mp4-gif-convert/#gif--mp4-)
有些 GIF 轉(zhuǎn)化出來的 MP4 不能被 Mac QuickTime Player.app 播放,需要添加 pixel formal
參數(shù)
ffmpeg -i input.gif -vf scale=420:-2,format=yuv420p out.mp4
使用 yunv420p
需要保證長寬為偶數(shù),這里同時使用了 scale=420:-2
。
[wiki 解釋](https://trac.ffmpeg.org/wiki/Encode/H.264#Encodingfordumbplayers): QuickTime Player 對 H.264 視頻只支持 YUV 色域 4:2:0 方式的二次插值算法。
參考資料
[FFmpeg CompilationGuide/MacOSX](https://trac.ffmpeg.org/wiki/CompilationGuide/MacOSX)
[Convert Video to GIF or GIF to Video](http://davidwalsh.name/convert-video-gif)
[Get the First Frame of an Animated GIF with ImageMagick](http://davidwalsh.name/first-frame-animated-gif)
[Create an Image Preview from a Video](http://davidwalsh.name/create-image-preview-video)
[How to speed up / slow down a video](https://trac.ffmpeg.org/wiki/How%20to%20speed%20up%20/%20slow%20down%20a%20video)
[ffmpeg useful commands (FFMPEG 命令大全)](http://siwei.me/blog/posts/ffmpeg-useful-commands)
[ffmpeg 文檔](http://siwei.me/blog/posts/ffmpeg-useful-commands)