FFmpeg的命令非常多济榨,經(jīng)常看著會(huì)云里霧里的筒严,個(gè)人認(rèn)為沒(méi)必要去硬背,只要打開Terminal情萤,輸入ffmpeg -help
命令鸭蛙,這條命令會(huì)告述你FFmpeg支持的大部分常用命令以及使用方式。查看這些輸出的信息紫岩,基本上就會(huì)使用很多常用的命令了规惰。
你會(huì)發(fā)現(xiàn)執(zhí)行ffmpeg -help
會(huì)輸出一大坨睬塌,那都是些啥玩意呢泉蝌,不著急,咱們一步一步來(lái)慢慢品嘗揩晴。
ffmpeg version 4.1.4 ...
第一個(gè)就是輸出我們安裝的FFmpeg的版本號(hào)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.4_1
--enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
--prefix
是指FFmpeg的安裝路徑
--enable
是你安裝的FFmpeg支持的三方庫(kù) 這里可以看出支持libx264
、libx265
的編碼,以及支持videotoolbox
拧略,videotoolbox
是Mac表蝙、iOS上的一個(gè)系統(tǒng)自帶硬編解碼庫(kù),F(xiàn)Fmpeg也給予了支持劫映,非常的給力违孝。
libavutil //工具庫(kù)
libavcodec //編解碼庫(kù)
libavformat //解協(xié)議、解封裝庫(kù)
libavdevice //設(shè)備庫(kù)
libavfilter //濾鏡庫(kù)
libavresample
libswscale
libswresample
libpostproc
這是輸出FFmpeg里面包含的庫(kù)泳赋,這些都是獨(dú)立的雌桑,是可以單獨(dú)拎出來(lái)使用的,你如果只要編解碼祖今,那你就只要在項(xiàng)目中導(dǎo)入libavcodec
即可
ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
這是告訴我們命令行使用格式
[options]
這個(gè)是全局參數(shù)
[infile options]
這個(gè)是輸入文件的參數(shù)
infile
這個(gè)是輸入文件的路徑
[outfile options]
這個(gè)是輸出文件的參數(shù)
outfile
這個(gè)是輸出文件的路徑
細(xì)心的你肯定發(fā)現(xiàn)infile
校坑、outfile
的后面有個(gè)...
拣技,這是在告訴我們輸入、輸出文件可以分別有一個(gè)或者多個(gè)耍目。
比如執(zhí)行ffmpeg -i test.h264 -i test.aac -c copy test.mp4
這條命令會(huì)把一個(gè)h264文件和一個(gè)aac的音頻文件合并并輸出一個(gè)mp4格式的文件膏斤,這其中就有兩個(gè)輸入文件、一個(gè)輸出文件邪驮。
Getting help:
-h -- print basic options
-h long -- print more options
-h full -- print all options (including all format and codec specific options, very long)
-h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf
See man ffmpeg for detailed description of the options.
這些是詳細(xì)的幫助信息
-h long
打印更多的選項(xiàng)參數(shù)莫辨。
-h full
打印所有的選項(xiàng)參數(shù),包括所有針對(duì)于format
和codec
的選項(xiàng)毅访,信息特別的長(zhǎng)衔掸。
man ffmpeg
: 查看FFmpeg的幫助手冊(cè)。
-h type=name
打印指定名稱的decoder/encoder/demuxer/muxer/filter
的所有選項(xiàng)信息俺抽。
比如你要查詢scale
濾鏡的使用方式敞映,我們就執(zhí)行ffmpeg -h filter=scale
輸出如下:
scale AVOptions:
w <string> ..FV..... Output video width
width <string> ..FV..... Output video width
h <string> ..FV..... Output video height
height <string> ..FV..... Output video height
flags <string> ..FV..... Flags to pass to libswscale (default "bilinear")
interl <boolean> ..FV..... set interlacing (default false)
in_color_matrix <string> ..FV..... set input YCbCr type (default "auto")
out_color_matrix <string> ..FV..... set output YCbCr type
in_range <int> ..FV..... set input color range (from 0 to 2) (default auto)
...
這就告訴我們scale
濾鏡有w
、h
等參數(shù),我們就這樣使用scale
濾鏡磷斧,
ffmpeg -i input.mp4 -filter_complex "scale=w=iw/2h=ih/2" output.mp4
其中iw
代表輸入視頻的寬振愿,ih
代表輸入視頻的高,這條命令就把輸入的視頻縮小一倍弛饭,這里你可能會(huì)有疑問(wèn)冕末,我都不記得那些濾鏡的名字,就無(wú)法使用這個(gè)去查了侣颂,哈哈不要急档桃,還記得上面的幫助命令嗎,ffmpeg -filters
可以輸出所有的濾鏡名字了憔晒,如果你覺(jué)的輸出太多藻肄,你不好找的話,你只要記得這個(gè)濾鏡大概是叫什么名字拒担、包含什么字母嘹屯,你就借助grep
指令去輸出里面搜索關(guān)鍵字,這樣就只會(huì)輸出你關(guān)心的濾鏡名了从撼,如ffmpeg -filters | grep over
Print help / information / capabilities:
-L //show license
-h topic // show help
-? topic //show help
-help topic //show help
--help topic //show help
-version //顯示版本號(hào)
-buildconf //顯示編譯配置州弟,如上面所說(shuō)的 --enable-libx264 --enable-libx265 --enable-videotoolbox 這種你所編譯的ffmpeg所支持的三方庫(kù)功能
-formats //顯示支持的所有格式
-muxers //顯示復(fù)用器
-demuxers //顯示可用的解復(fù)用器
-devices //顯示支持的設(shè)備
-codecs //顯示可用的編解碼器
-decoders //顯示可用的解碼器
-encoders //顯示可用的編碼器
-bsfs //顯示可用的字節(jié)流濾鏡
-protocols //顯示支持的協(xié)議 http rtmp ftp hls等等
-filters //顯示支持的濾鏡
-pix_fmts //顯示支持的像素格式 yuv420p yuv422p yuv444p
-layouts //顯示支持的音頻聲道布局,最常見的就是mono單聲道低零,stereo立體雙聲道
-sample_fmts //顯示可用的音頻采樣格式
-colors //顯示支持的color名稱 比如Red是#ff0000表示紅色婆翔,當(dāng)你需要使用時(shí)就只要輸入Red,而不用輸入#ff0000
-sources device //顯示輸入設(shè)備的源列表
-sinks device //顯示列表的輸出設(shè)備
-hwaccels //顯示HW加速方法
以上是幫助命令掏婶,如果你忘記了某個(gè)編譯器啃奴、像素格式或者濾鏡的名字,你就可以用這些命令去查看气堕。
-loglevel loglevel //設(shè)置日志級(jí)別
-v loglevel //這是-loglevel的縮寫
-report generate a report
-max_alloc bytes set maximum size of a single allocated block
-y //強(qiáng)制覆蓋輸出文件
-n never overwrite output files
-ignore_unknown Ignore unknown stream types
-filter_threads number of non-complex filter threads
-filter_complex_threads number of threads for -filter_complex
-stats print progress report during encoding
-max_error_rate maximum error rate ratio of errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.
-bits_per_raw_sample number set the number of bits per raw sample
-vol volume change audio volume (256=normal)
以上就是全局參數(shù)
-f fmt //文件格式 這個(gè)可以根據(jù) ffmpeg -formats查看所支持的所有格式
-c codec //編解碼器 copy表示不進(jìn)行編解碼纺腊,直接復(fù)制
-codec codec //同-c
-pre preset //預(yù)設(shè)參數(shù)
-map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile
-t duration //時(shí)間
-to time_stop //截止實(shí)踐
-fs limit_size set the limit file size in bytes
-ss time_off //開始時(shí)間
-sseof time_off set the start time offset relative to EOF
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string add metadata
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad audio pad
-frames number set the number of frames to output
-filter filter_graph //濾鏡
-filter_script filename //從文件中讀取濾鏡的描述filtergraph
-reinit_filter reinit filtergraph on input parameter changes
-discard discard
-disposition disposition
以上是音視頻的公共參數(shù)
-vframes number // set the number of video frames to output
-r rate // set frame rate (Hz value, fraction or abbreviation)
-s size // 大小 格式是WxH
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number set the number of bits per raw sample
-vn //忽略視頻流
-vcodec codec force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff set initial TimeCode value.
-pass n select the pass number (1 to 3)
-vf filter_graph set video filters
-ab bitrate audio bitrate (please use -b:a)
-b bitrate video bitrate (please use -b:v)
-dn disable data
以上是視頻處理相關(guān)參數(shù)
-aframes number set the number of audio frames to output
-aq quality set audio quality (codec-specific)
-ar rate set audio sampling rate (in Hz)
-ac channels set number of audio channels
-an disable audio
-acodec codec force audio codec ('copy' to copy stream)
-vol volume change audio volume (256=normal)
-af filter_graph set audio filters
以上是音頻處理相關(guān)
-s size set frame size (WxH or abbreviation)
-sn disable subtitle
-scodec codec force subtitle codec ('copy' to copy stream)
-stag fourcc/tag force subtitle tag/fourcc
-fix_sub_duration fix subtitles duration
-canvas_size size set canvas size (WxH or abbreviation)
-spre preset set the subtitle options to the indicated preset
以上字幕處理相關(guān)參數(shù)
以上就是ffmpeg -help
的輸出說(shuō)明了畔咧,下面咱們來(lái)看看常用的命令。
mp4格式轉(zhuǎn)換為flv
ffmpeg -i input.mp4 output.flv
從視頻中提取yuv420格式的視頻流
ffmpeg -i input.mp4 -pix_fmt yuv420p output.h264
從視頻中提取音頻流
ffmpeg -i input.mp4 output.aac
視頻轉(zhuǎn)圖片
ffmpeg -i input.mp4 -r 10 image%02d.png
-r 10
表示1秒視頻會(huì)生成10張圖片
視頻中提取yuv原始數(shù)據(jù)
ffmpeg -i input.mp4 -pix_fmt yuv444p output.yuv
視頻中提取pcm原始數(shù)據(jù)
ffmpeg -i input.mp4 -c:a pcm_f32le -ar 44100 -ac 2 -f f32le output.pcm
根據(jù)yuv與pcm數(shù)據(jù)生成視頻
ffmpeg -pix_fmt yuv420p -s 320x240 -i input.yuv -ar 44100 -ac 2 -f f32le input.pcm -pix_fmt yuv422p output.flv
根據(jù)圖片生成視頻
ffmpeg -i image%03d.png -pix_fmt yuv420p -c:v libx264 output.avi
裁剪視頻揖膜,從第3秒開始誓沸,裁剪5秒
ffmpeg -i input.mp4 -ss 3 -t 5 output.mp4
視頻縮放
ffmpeg -i input.mp4 -filter_complex "scale=w=iw/2:h=ih/2" output.mp4
視頻疊加
ffmpeg -i main.mp4 -i overlay.mp4 -filter_complex "overlay=x=main_w-overlay_w-10:y=main_h-overlay_h-10" outpu.mp4
錄制攝像頭
ffmpeg -f avfoundation -r 30 -s 640x480 -i '1' -pix_fmt yuv420p -f flv output.flv
錄制攝像頭+錄音
ffmpeg -f avfoundation -r 30 -s 640x480 -i '1:0' -pix_fmt yuv420p -f flv output.flv
錄制攝像頭+錄音+屏幕并推送到rtmp服務(wù)器
ffmpeg -thread_queue_size 128 -y -f avfoundation -r 30 -i '1:0' -f avfoundation -r 30 -s 640x480 -i 0 -filter_complex 'overlay=100:100' -pix_fmt yuv420p -f flv rtmp://localhost:1935/app/room
-f avfoundation
指定采用avfoundation采集數(shù)據(jù)使用
-i 1:0
表示 指定視頻設(shè)備索引為1,指定錄音設(shè)備索引為0壹粟。
使用ffmpeg -list_devices 1 -f avfoundation -i ''
可以打印出設(shè)備列表拜隧,如下:
[AVFoundation input device @ 0x7fbcba520040] AVFoundation video devices:
[AVFoundation input device @ 0x7fbcba520040] [0] FaceTime HD Camera
[AVFoundation input device @ 0x7fbcba520040] [1] Capture screen 0
[AVFoundation input device @ 0x7fbcba520040] AVFoundation audio devices:
[AVFoundation input device @ 0x7fbcba520040] [0] Built-in Microphone
因此上述命令中的-i 1:0
表示采用【Capture screen 0】【Built-in Microphone】即采用屏幕和系統(tǒng)自帶的麥克風(fēng)進(jìn)行采集。