[轉(zhuǎn)]將視頻 MP4 轉(zhuǎn)化為 GIF

原文地址: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

將視頻 MP4 轉(zhuǎn)化為 GIF

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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 原文鏈接 GIF替代方案 維基百科的解釋Video alternative to GIF已經(jīng)提出來的替代gif的方...
    TryEnough閱讀 2,710評論 0 4
  • 0 概述 FFmpeg是一套領(lǐng)先的音視頻多媒體處理開源框架,采用LGPL或GPL許可證。它提供了對音視頻的采集、編...
    但行耕者閱讀 6,954評論 0 19
  • 教程一:視頻截圖(Tutorial 01: Making Screencaps) 首先我們需要了解視頻文件的一些基...
    90后的思維閱讀 4,782評論 0 3
  • 寶寶最近好像很愛看書啊!曾經(jīng)不被待見的《拉拉布書》、曾經(jīng)撕不動就扔的《小波紙板書系列》《小兔比利觸摸書》、當(dāng)然也包...
    給你們_樂樂與開心閱讀 197評論 0 1
  • *感賞自己今天好勤快,早上起來沒吃早點就開始收拾樓上屋子…… ...
    我就是我hxh閱讀 222評論 0 0