在 Github 上,我發(fā)現(xiàn)很多開(kāi)源庫(kù)的 README 里都有 gif 文件,平時(shí)聊天我也發(fā)現(xiàn)經(jīng)常有些小伙伴發(fā)一些自制的 gif 文件。怎么將 mov,mp4 等其他格式的文件轉(zhuǎn)為 gif 文件呢?網(wǎng)上有很多介紹各種軟件的,大家可以去搜索一下。
我今天給大家講講怎么用 終端+命令行 的方式實(shí)現(xiàn) mov/mp4 等其他視頻格式轉(zhuǎn)成 gif 文件。
安裝
在執(zhí)行轉(zhuǎn)換命令之前,我們得先安裝幾個(gè)軟件包。
Homebrew
如果你已經(jīng)裝好了 Homebrew,就當(dāng)沒(méi)看見(jiàn)這一步。否則把下面兩行腳本粘貼到你的終端,并執(zhí)行。首先:
xcode-select --install
然后
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
等待,不出意外的話(huà),Homebrew 就裝好了。
gifify
接下來(lái)就要安裝真正的主角了。
首先,安裝 Node.js 環(huán)境(如果以前已經(jīng)裝好了,就跳過(guò)這步):
brew install node
其次,安裝 FFmpeg
brew install ffmpeg
然后,安裝 convert
brew install imagemagick
再安裝 giflossy
brew install giflossy
最后安裝 gifify
npm install -g gifify
檢驗(yàn)是否安裝成功,執(zhí)行
gifify -h
如果沒(méi)問(wèn)題的話(huà),就會(huì)出現(xiàn)如下結(jié)果:
192:~ dyf$ gifify -h
Usage: gifify [options] [file]
Options:
-V, --version output the version number
--colors <n> Number of colors, up to 255, defaults to 80 (default: 80)
--compress <n> Compression (quality) level, from 0 (no compression) to 100, defaults to 40 (default: 40)
--from <position> Start position, hh:mm:ss or seconds, defaults to 0
--fps <n> Frames Per Second, defaults to 10 (default: 10)
-o, --output <file> Output file, defaults to stdout
--resize <W:H> Resize output, use -1 when specifying only width or height. `350:100`, `400:-1`, `-1:200`
--reverse Reverses movie
--speed <n> Movie speed, defaults to 1 (default: 1)
--subtitles <filepath> Subtitle filepath to burn to the GIF
--text <string> Add some text at the bottom of the movie
--to <position> End position, hh:mm:ss or seconds, defaults to end of movie
--no-loop Will show every frame once without looping
-h, --help output usage information
簡(jiǎn)單使用
進(jìn)入到你要轉(zhuǎn)換文件的目錄下,比如我的 mov 文件放在文檔目錄,執(zhí)行
cd ~/Documents/
然后執(zhí)行
gifify ru.mov -o ru.gif
使用 ffmpeg 轉(zhuǎn)換
- 將視頻 MP4 轉(zhuǎn)化為 GIF
ffmpeg -i small.mp4 small.gif
- 將視頻 MP4 指定寬高和幀率轉(zhuǎn)化為 GIF
ffmpeg -i small.mp4 -s 414x736 -r 15 small.gif
- 將視頻中的一部分轉(zhuǎn)換為 GIF
# 從視頻中第二秒開(kāi)始,截取時(shí)長(zhǎng)為3秒的片段轉(zhuǎn)化為 gif
ffmpeg -t 3 -ss 00:00:02 -i small.mp4 small-clip.gif
- 轉(zhuǎn)化高質(zhì)量 GIF
# 默認(rèn)轉(zhuǎn)化是中等質(zhì)量模式,若要轉(zhuǎn)化出高質(zhì)量的 gif,可以修改比特率
ffmpeg -i small.mp4 -b 2048k small.gif
- 將 GIF 轉(zhuǎn)化為 MP4
ffmpeg -f gif -i animation.gif animation.mp4
- 將 Gif 轉(zhuǎn)為其他視頻格式
ffmpeg -f gif -i animation.gif animation.mpeg
ffmpeg -f gif -i animation.gif animation.webm
- 加倍速播放視頻
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
- 慢倍速播放視頻
ffmpeg -i input.mov -filter:v "setpts=2.0*PTS" output.mov
- 靜音視頻(移除視頻中的音頻)
# -an 就是禁止音頻輸出
ffmpeg -i input.mov -an mute-output.mov
- 視頻提取幀
# 將視頻提取10幀
ffmpeg -i index.mp4 -r 10 %03d.jpg;
- 主要參數(shù)
-i——設(shè)置輸入檔名。
-f——設(shè)置輸出格式。
-y——若輸出文件已存在時(shí)則覆蓋文件。
-fs——超過(guò)指定的文件大小時(shí)則結(jié)束轉(zhuǎn)換。
-ss——從指定時(shí)間開(kāi)始轉(zhuǎn)換。
-t從-ss時(shí)間開(kāi)始轉(zhuǎn)換(如-ss 00:00:01.00 -t 00:00:10.00即從00:00:01.00開(kāi)始到00:00:11.00)。
-title——設(shè)置標(biāo)題。
-timestamp——設(shè)置時(shí)間戳。
-vsync——增減Frame使影音同步。
視頻參數(shù)
-b:v——設(shè)置視頻流量,默認(rèn)為200Kbit/秒。(單位請(qǐng)引用下方注意事項(xiàng))
-r——設(shè)置幀率值,默認(rèn)為25。
-s——設(shè)置畫(huà)面的寬與高。
-aspect——設(shè)置畫(huà)面的比例。
-vn——不處理視頻,于僅針對(duì)聲音做處理時(shí)使用。
-vcodec( -c:v )——設(shè)置視頻視頻編解碼器,未設(shè)置時(shí)則使用與輸入文件相同之編解碼器。
聲音參數(shù)
-b:a——設(shè)置每Channel(最近的SVN版為所有Channel的總合)的流量。(單位請(qǐng)引用下方注意事項(xiàng))
-ar——設(shè)置采樣率。
-ac——設(shè)置聲音的Channel數(shù)。
-acodec ( -c:a ) ——設(shè)置聲音編解碼器,未設(shè)置時(shí)與視頻相同,使用與輸入文件相同之編解碼器。
-an——不處理聲音,于僅針對(duì)視頻做處理時(shí)使用。
-vol——設(shè)置音量大小,256為標(biāo)準(zhǔn)音量。(要設(shè)置成兩倍音量時(shí)則輸入512,依此類(lèi)推。)
參考鏈接
點(diǎn)贊+關(guān)注,第一時(shí)間獲取技術(shù)干貨和最新知識(shí)點(diǎn),謝謝你的支持!
最后祝大家生活愉快~