宜賓網(wǎng)站建設(shè)費用抖音推廣引流
文章目錄
- 前言
- 一、ffmpeg 常用命令
- 1、ffmpeg 的封裝轉(zhuǎn)換
- 2、ffmpeg 的編轉(zhuǎn)碼
- 3、ffmpeg 的基本編轉(zhuǎn)碼原理
- 二、ffprobe 常用參數(shù)
- 1、show_format
- 2、show_frames
- 3、show_streams
- 4、print_format
- 5、select_streams
- 三、ffplay 的常用命令
- 1、ffplay 常用參數(shù)
- 2、ffplay 高級參數(shù)
- 3、ffplay 的數(shù)據(jù)可視化分析應(yīng)用
前言
FFmpeg 有三個作用不同的工具軟件 ffmpeg.exe
,ffplay.exe
,ffprobe.exe
,本文主要講講這些工具軟件的使用方法。
一、ffmpeg 常用命令
ffmpeg 在做音視頻編解碼時非常方便,所以很多場景下轉(zhuǎn)碼使用的是 ffmpeg,通過 ffmpeg -help
命令操作可以看到 ffmpeg 常見的命令大概分為六部分:
- ffmpeg 信息查詢部分
- 公共操作參數(shù)部分
- 文件主要操作參數(shù)部分
- 視頻操作參數(shù)部分
- 音頻操作參數(shù)部分
- 字幕操作參數(shù)部分
D:\Work\test>ffmpeg -help
ffmpeg version 6.0-essentials_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developersbuilt with gcc 12.2.0 (Rev10, Built by MSYS2 project)configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libvpl --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberbandlibavutil 58. 2.100 / 58. 2.100libavcodec 60. 3.100 / 60. 3.100libavformat 60. 3.100 / 60. 3.100libavdevice 60. 1.100 / 60. 1.100libavfilter 9. 3.100 / 9. 3.100libswscale 7. 1.100 / 7. 1.100libswresample 4. 10.100 / 4. 10.100libpostproc 57. 1.100 / 57. 1.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...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/protocolSee man ffmpeg for detailed description of the options.Print help / information / capabilities:
-L show license
-h topic show help
-? topic show help
-help topic show help
--help topic show help
-version show version
-buildconf show build configuration
-formats show available formats
-muxers show available muxers
-demuxers show available demuxers
-devices show available devices
-codecs show available codecs
-decoders show available decoders
-encoders show available encoders
-bsfs show available bit stream filters
-protocols show available protocols
-filters show available filters
-pix_fmts show available pixel formats
-layouts show standard channel layouts
-sample_fmts show available audio sample formats
-dispositions show available stream dispositions
-colors show available color names
-sources device list sources of the input device
-sinks device list sinks of the output device
-hwaccels show available HW acceleration methodsGlobal options (affect whole program instead of just one file):
-loglevel loglevel set logging level
-v loglevel set logging level
-report generate a report
-max_alloc bytes set maximum size of a single allocated block
-y overwrite output files
-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 decoding errors (0.0: no errors, 1.0: 100% errors) above which ffmpeg returns an error instead of success.Per-file main options:
-f fmt force format
-c codec codec name
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile
-t duration record or transcode "duration" seconds of audio/video
-to time_stop record or transcode stop time
-fs limit_size set the limit file size in bytes
-ss time_off set the start time offset
-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 set stream filtergraph
-filter_script filename read stream filtergraph description from a file
-reinit_filter reinit filtergraph on input parameter changes
-discard discard
-disposition dispositionVideo options:
-vframes number set the number of video frames to output
-r rate set frame rate (Hz value, fraction or abbreviation)
-fpsmax rate set max frame rate (Hz value, fraction or abbreviation)
-s size set frame size (WxH or abbreviation)
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-display_rotation angle set pure counter-clockwise rotation in degrees for stream(s)
-display_hflip set display horizontal flip for stream(s) (overrides any display rotation if it is not set)
-display_vflip set display vertical flip for stream(s) (overrides any display rotation if it is not set)
-vn disable video
-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
-b bitrate video bitrate (please use -b:v)
-dn disable dataAudio options:
-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)
-ab bitrate audio bitrate (please use -b:a)
-af filter_graph set audio filtersSubtitle options:
-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
ffmpeg 信息查詢部分主要參數(shù):
其中通過 ffmpeg --help 查看到的 help 信息是基礎(chǔ)的信息,如果想獲得高級參數(shù)部分,可以通過使用 ffmpeg --help long 參數(shù)來查看,如果希望獲得全部的幫助信息,可以同過使用 ffmpeg --help full 參數(shù)來獲得。
通過 -L
參數(shù),可以看到 ffmpeg 目前所支持的 license 協(xié)議,通過 -version
查看 ffmpeg 的版本,這里所查看的版本, 是詳細(xì)的版本,包含 libavformat
、libavcodec
、libavutil
、libavfilter
、libswscale
、libswresample
的版本。
有時候使用 ffmpeg 轉(zhuǎn)碼,發(fā)現(xiàn)無法解析視頻文件,或者無法生成視頻文件,報一些不支持生成對應(yīng)的視頻文件的錯誤,這個時候就需要查看當(dāng)前使用的 ffmpeg 是否支對應(yīng)的視頻文件格式,需要使用 ffmpeg -formats
來查看。
封裝格式/解封裝格式如下圖:
注:因列表比較長,因此只截取一部分
在上面輸出的內(nèi)容中,分為三部門:
- 第一列是多媒體文件格式的 Demuxing 支持與 Muxing 支持;
- 第二列是多媒體文件格式;
- 第三列是文件格式的詳細(xì)說明。
1、ffmpeg 的封裝轉(zhuǎn)換
- Avformat
- Avcodec
- Avutil
FFmpeg 的封裝轉(zhuǎn)換部分,主要是通過 libavformat 這部分來完成的功能,通過 libavformat 庫進行 mux 和 demux 操作,多媒體文件的格式有很多種,但是還是有好多 demux 與 mux 的操作參數(shù)是公用的,下面來詳細(xì)介紹一下這些公用的參數(shù):
avioflags
:format 的緩沖設(shè)置,默認(rèn)為 0,就是有緩沖;direct
:無緩沖狀態(tài);probesize
:獲得文件內(nèi)容的大小;formatprobesiz
:設(shè)置一個獲得文件內(nèi)容的大小來解析文件的 format;fflags
: 設(shè)置讀取或者寫出的格式的形式的標(biāo)簽,分為多種方式:flush_packets,ignidx,genpts,nofillin,noparse,igndts,discardcorrupt,sortdts,keepside,fastseek,latm,nobuffer,bitexact
下面針對這些 format 讀寫方式進行一一詳解:
AVPacket(壓縮的 h264/aac),AVFrame(原始未壓縮:yuv,pcm)
flush_packets
:立即將 packets 數(shù)據(jù)刷新入文件中;genpts
:輸出是產(chǎn)生 pts; //pts, presentation timestamp, dts:decoding timestampnofillin
:不填寫可以精確計算缺失的值;igndts
:忽略 dts;discardcorrupt
:丟棄損壞的幀;sortdts
:嘗試以 dts 的順序為準(zhǔn)輸出;keepside
:不合并數(shù)據(jù);fastseek
:快速 seek,但是不夠精確;latm
:設(shè)置 RTP MP4_LATM 生效;nobuffer
:直接讀取或者寫出,不存 buffer;bitexact
:不寫入隨機或者不穩(wěn)定的數(shù)據(jù);seek2any
:支持隨意 seek,這個 seek 可以不以 keyframe 為參考;analyzeduration
:指定解析媒體所需要花銷的時間,這里設(shè)置的值越高,解析的越準(zhǔn)確,如果在直播中為了降低延遲, 這個可以設(shè)置得低一些;codec_whitelist
:設(shè)置可以解析的 codec 的白名單;format_whitelist
:設(shè)置可以解析的 format 的白名單;
2、ffmpeg 的編轉(zhuǎn)碼
FFmpeg 的編解碼部分主要是通過 libavcodec 這個庫來完成的功能,通過 libavcodec 庫進行 encode 與 decode 操作,多媒體的編碼格式種類很多,但是還是有好多基本的操作參數(shù)是共同可以設(shè)置的,下面來詳細(xì)介紹一下這些公用的參數(shù):
通過查看 ffmpeg 的 help full 參數(shù)可以看到 AVCodecContext AVOptions,在這個選項下面的所有參數(shù)均為編解碼可以使用的參數(shù):
b
:設(shè)置音頻與視頻碼率,可以認(rèn)為是音視頻加起來的碼率,默認(rèn)為 200k;使用這個參數(shù)可以根據(jù)b:v
設(shè)置視頻碼率,b:a
設(shè)置音頻碼率;ab
:設(shè)置音頻的碼率, 默認(rèn)是 128k;g
:設(shè)置視頻 GOP 大小,關(guān)鍵幀間隔,默認(rèn)是 12 幀一個 gop;//r:
幀率,比如 25 幀一個 gopar
:設(shè)置音頻采樣率,默認(rèn)為 0;ac
:設(shè)置音頻通道數(shù),默認(rèn)為 0;bf
:設(shè)置非 B 幀之間的 B 幀個數(shù),默認(rèn)為 0; // Ibbpbbpbbp, Ippppppppmaxrate
:最大碼率設(shè)置,與 bufsize 一同使用即可, 默認(rèn)為 0;minrate
:最小碼率設(shè)置,配合 maxrate 與 bufsize 可以設(shè)置為 CBR 模式,平時基本沒用, 默認(rèn)為 0;bufsize
:設(shè)置控制碼率的 buffer 的大小,默認(rèn)為 0;keyint_min
:設(shè)置最小 IDR 幀間隔,默認(rèn)為 25;// I 幀,IDR 幀不同;sc_threshold
:設(shè)置場景切換支持,默認(rèn)為 0;me_threshold
:設(shè)置運動估計閥值,默認(rèn)為 0;mb_threshold
:設(shè)置宏塊閥:threshold 值, 默認(rèn)為 0;profile
:設(shè)置音視頻的 profile,默認(rèn)為 -99;level
:設(shè)置音視頻的 level,默認(rèn)為 -99;imecode_frame_start
:設(shè)置 GOP 幀的開始時間, 需要在 non-drop-frame 默認(rèn)情況下使用;channel_layout
:設(shè)置音頻通道的布局格式;threads
:設(shè)置編解碼等工作的線程數(shù);
3、ffmpeg 的基本編轉(zhuǎn)碼原理
ffmpeg 工具主要用途為編碼、解碼和轉(zhuǎn)碼,常用 ffmpeg 做的為轉(zhuǎn)碼操作,使用 ffmpeg 轉(zhuǎn)碼主要原理為:
二、ffprobe 常用參數(shù)
ffprobe 常用參數(shù)比較多, 可以用過 ffprobe --help
來查看詳細(xì)的信息.
ffprobe -show_packets "input_file"
:查看的多媒體包信息使用 PACKET 標(biāo)簽所包括起來, 其中包含的信息主要如下:
codec_type
:多媒體類型,例如視頻包,音頻包等;stream_index
:多媒體的 stream 索引;pts
:多媒體的顯示時間值;//presentation timestamppts_time
:根據(jù)不同格式計算過后的多媒體的顯示時間;dts
:多媒體解碼時間值; //decoding timestampduration
:多媒體包占用的時間值;duration_time
:根據(jù)不同格式計算過后的多媒體包占用的時間值;size
:多媒體包的大小;pos
:多媒體包所在的文件偏移位置;flags
:多媒體包標(biāo)記,關(guān)鍵包與非關(guān)鍵包的標(biāo)記;
1、show_format
除了 packets 與 data 外,ffprobe 還可以分析多媒體的封裝格式,通過 show_format 參數(shù)可以查看多媒體的封裝格式,封裝格式使用 FORMAT 標(biāo)簽括起來顯示:
ffprobe -show_format "input_file"
通過讀取 format 信息,可以看到這個視頻文件有兩個流通道,起始時間是 0.000000,長度為 105.282000,文件大小為 21045377 字節(jié),碼率為 1599162 字節(jié)每秒。
2、show_frames
通過 ffprobe 的 show_frames 的參數(shù)可以查看視頻文件中的幀信息, 輸出的幀信息使用 FRAME 標(biāo)簽括起來
通過 -show_frames 參數(shù)查看到如圖的信息, 能夠看到每一幀的信息, 下面介紹一下重要的信息:
3、show_streams
通過 -show_streams 參數(shù)可以查看到多媒體文件中的流信息,流的信息使用 STREAMS 標(biāo)簽括起來:
如圖所示,可以看到流的信息:
4、print_format
ffprobe 使用前面的參數(shù)可以獲得到對應(yīng)的 key-value,但是閱讀起來因習(xí)慣不同,所以有的人認(rèn)為方便,有的人認(rèn)為不方便,這樣就需要用到 ffprobe 的 print_format 參數(shù)來進行相應(yīng)的格式輸出,下面舉幾種輸出的例子:
ffprobe -show_frames "input_file" -print_format json
最后一項可選:xml
,json
,ini
,csv
,flat
5、select_streams
如果只查看音頻流或視頻流,使用 select_streams 參數(shù)即可,例如只查看視頻流的 frames 信息
ffprobe -show_frames -select_streams v -of xml "input_file"
三、ffplay 的常用命令
通常使用 ffplay 作為播放器,其實 ffplay 不但可以做播放器,同樣可以作為很多圖像化音視頻數(shù)據(jù)的分析根據(jù),通過 ffplay 可以看到視頻圖像的運動估計方向,音頻數(shù)據(jù)的波形等。
1、ffplay 常用參數(shù)
ffplay 不僅僅是播放器,同時也是測試 ffmpeg 的 codec 引擎,format 引擎,以及 filter 引擎的工具,并且也可以做可視化的媒體參數(shù)分析,可以通過 ffplay --help 進行查看:
常見參數(shù)可以手動進行嘗試:
- 如果希望從視頻的第 30 秒開始播放,播放 10 秒鐘的文件,則可以使用如下命令:
ffplay -ss 30 -t 10 input.mp4
- 如果希望視頻播放時播放器的窗口顯示標(biāo)題為自定義標(biāo)題,則使用如下命令:
ffplay -window_title "Hello World, This is a sample" output.mp4
- 使用 ffplay 打開網(wǎng)絡(luò)直播流,可以使用命令:
ffplay -window_title "播放測試" rtmp://up.v.test.com/live/stream
2、ffplay 高級參數(shù)
通過使用 ffplay --help 參數(shù)看到幫助信息比較多,其中包含了高級參數(shù)介紹,下面詳細(xì)介紹一下
3、ffplay 的數(shù)據(jù)可視化分析應(yīng)用
使用 ffplay 除了可以播放視頻流媒體文件之外,還可以作為可視化的視頻流媒體分析工具,例如當(dāng)播放音頻文件時,不確定文件的聲音是否正常,噪聲數(shù)據(jù)等分析,可以直接使用 ffplay 播放音頻文件,播放的時候?qū)呀獯a后的音頻數(shù)據(jù)以音頻波形顯示出來:
ffplay -showmode 1 1.mp3
從圖中可以看到,音頻的播放時的波形可以通過振幅顯示出來,可以用來看到音頻的播放情況
我的qq:2442391036,歡迎交流!