FFmpeg命令行使用

FFmpeg的命令非常多,經(jīng)??粗鴷评镬F里的,個人認(rèn)為沒必要去硬背,只要打開Terminal,輸入ffmpeg -help命令,這條命令會告述你FFmpeg支持的大部分常用命令以及使用方式。查看這些輸出的信息,基本上就會使用很多常用的命令了。
你會發(fā)現(xiàn)執(zhí)行ffmpeg -help會輸出一大坨,那都是些啥玩意呢,不著急,咱們一步一步來慢慢品嘗。

ffmpeg version 4.1.4 ...

第一個就是輸出我們安裝的FFmpeg的版本號

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支持的三方庫 這里可以看出支持libx264、libx265的編碼,以及支持videotoolbox,videotoolbox是Mac、iOS上的一個系統(tǒng)自帶硬編解碼庫,F(xiàn)Fmpeg也給予了支持,非常的給力。

 libavutil      //工具庫
 libavcodec    //編解碼庫
 libavformat    //解協(xié)議、解封裝庫
 libavdevice    //設(shè)備庫
 libavfilter   //濾鏡庫
 libavresample  
 libswscale    
 libswresample 
 libpostproc  

這是輸出FFmpeg里面包含的庫,這些都是獨(dú)立的,是可以單獨(dú)拎出來使用的,你如果只要編解碼,那你就只要在項(xiàng)目中導(dǎo)入libavcodec即可

 ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

這是告訴我們命令行使用格式
[options]這個是全局參數(shù)
[infile options] 這個是輸入文件的參數(shù)
infile 這個是輸入文件的路徑
[outfile options] 這個是輸出文件的參數(shù)
outfile 這個是輸出文件的路徑
細(xì)心的你肯定發(fā)現(xiàn)infile、outfile的后面有個...,這是在告訴我們輸入、輸出文件可以分別有一個或者多個。
比如執(zhí)行ffmpeg -i test.h264 -i test.aac -c copy test.mp4
這條命令會把一個h264文件和一個aac的音頻文件合并并輸出一個mp4格式的文件,這其中就有兩個輸入文件、一個輸出文件。

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ù),包括所有針對于formatcodec的選項(xiàng),信息特別的長。
man ffmpeg: 查看FFmpeg的幫助手冊。
-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代表輸入視頻的高,這條命令就把輸入的視頻縮小一倍,這里你可能會有疑問,我都不記得那些濾鏡的名字,就無法使用這個去查了,哈哈不要急,還記得上面的幫助命令嗎,ffmpeg -filters可以輸出所有的濾鏡名字了,如果你覺的輸出太多,你不好找的話,你只要記得這個濾鏡大概是叫什么名字、包含什么字母,你就借助grep指令去輸出里面搜索關(guān)鍵字,這樣就只會輸出你關(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            //顯示版本號
-buildconf         //顯示編譯配置,如上面所說的 --enable-libx264 --enable-libx265 --enable-videotoolbox  這種你所編譯的ffmpeg所支持的三方庫功能
-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)你需要使用時就只要輸入Red,而不用輸入#ff0000
-sources device     //顯示輸入設(shè)備的源列表
-sinks device      //顯示列表的輸出設(shè)備
-hwaccels           //顯示HW加速方法

以上是幫助命令,如果你忘記了某個編譯器、像素格式或者濾鏡的名字,你就可以用這些命令去查看。

-loglevel loglevel  //設(shè)置日志級別
-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              //文件格式 這個可以根據(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        //時間
-to time_stop     //截止實(shí)踐
-fs limit_size      set the limit file size in bytes
-ss time_off        //開始時間
-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的輸出說明了,下面咱們來看看常用的命令。

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秒視頻會生成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)行采集。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容