FFmpeg
FFmpeg 是處理多媒體內(nèi)容(如音頻虐唠、視頻讯榕、字幕和相關(guān)元數(shù)據(jù))的庫(libraries)和工具(tools)的集合蚊惯。
Libraries
-
libavcodec
音視頻編解碼模塊吆倦,提供常用的編解碼器说订。 -
libavformat
音視頻格式封裝模塊抄瓦,提供常用格式的封裝和解封裝能力,如 MP4陶冷、FLV等文件封裝格式钙姊,RTMP、HLS 等網(wǎng)絡協(xié)議封裝格式埂伦。 -
libavutil
包括 hashers(散列), decompressors(解壓縮) 和 miscellaneous(雜項) 等實用功能煞额。 -
libavfilter
提供通過連接過濾器實現(xiàn)的有向圖來改變解碼音頻和視頻的方法,即提供一個通用的音頻沾谜、視頻膊毁、字幕等的處理框架。 -
libavdevice
提供一個抽象來訪問捕獲設(shè)備(麥克風基跑、攝像頭婚温、桌面)和播放設(shè)備(揚聲器、顯示器)媳否。 -
libswresample
實現(xiàn)音頻混合和重采樣例程栅螟。 -
libswscale
實現(xiàn)顏色和縮放例程。
Tools
- ffmpeg 是一個命令行工具箱篱竭,用于操作嵌巷、轉(zhuǎn)換和流式傳輸多媒體內(nèi)容。
- ffplay 是一款簡約的多媒體播放器室抽。
- ffprobe 是一個檢查多媒體內(nèi)容的簡單分析工具搪哪。
stream specifiers
Stream specifiers (流說明符) 被用于精確地指定給定的選項(option)所應用的 stream 。
一個 stream specifier 通常是附加在選項名稱后并用冒號與選項名稱分隔的字符串坪圾。例如:-codec:a:1 ac3
包含 stream specifier a:1
即第二個音頻流晓折,它將為第二個音頻流選擇 ac3
編解碼器。
stream specifier 可以匹配多個流兽泄,以便將該選項應用于所有流漓概。例如 -b:a 128k
中 a
等價于匹配所有音頻流。
空 stream specifier 匹配所有流病梢。例如, -codec copy
或 -codec: copy
將復制所有流胃珍,而不需要重新編碼梁肿。
stream specifier 的可能格式如下:
-
stream_index
- stream_index 的 stream number 是 基于 libavformat 檢測到的流順序;如果指定了
program_id
觅彰,stream number 基于程序中流的順序吩蔑。stream_index 可以用于匹配應用選項的流。例如-threads:1 4
會將第二個流的線程數(shù)設(shè)置為 4 填抬。如果 stream_index 用作附加的流說明符(見下文的additional_stream_specifier
) (例如:-codec:a:1 ac3
的a:1
) 烛芬,那么它將從匹配的流中選擇 stream_index 。
- stream_index 的 stream number 是 基于 libavformat 檢測到的流順序;如果指定了
-
stream_type[:additional_stream_specifier]
-
stream_type
的v
或V
指 video,a
指 audio,s
指 subtitle,t
指 attachments 飒责。v
匹配所有視頻流赘娄,V
只匹配沒有附加圖片、視頻縮略圖或封面藝術(shù)的視頻流宏蛉。如果使用additional_stream_specifier
遣臼,那么它將在 stream_type 指向的 streams 中匹配additional_stream_specifier
的流。additional_stream_specifier
是指定類型的 stream number (不是全局的 stream number)拾并。
-
-
program_id[:additional_stream_specifier]
-
program_id
被用于匹配program_id
的 streams揍堰,如果使用additional_stream_specifier
,那么它將在該program_id
指向的 streams 中匹配additional_stream_specifier
的流辟灰。
-
ffmpeg 常用命令
ffmpeg [global_options] {[input_file_options] -i input_file} ... {[output_file_options] output_file} ...
常用選項:
-i input_file input_file
-f fmt force format
-c codec codec name ('copy' to copy stream)
-
-i
指定輸入文件 -
-c
指定輸出的編碼格式,'copy' 表示復制流篡石,不重新解碼和編碼 -
-f
指定輸出的封裝格式
轉(zhuǎn)碼和轉(zhuǎn)封裝
_______ ______________
| | | |
| input | demuxer | encoded data | decoder
| file | ---------> | packets | -----+
|_______| |______________| |
v
_________
| |
| decoded |
| frames |
|_________|
________ ______________ |
| | | | |
| output | <-------- | encoded data | <----+
| file | muxer | packets | encoder
|________| |______________|
- 解封裝
- 解碼
- 重新編碼
- 重新封裝
示例
ffmpeg -i input.rmvb -vcodec mpeg4 -b:v 200k -an output.mp4
參數(shù)說明:
Video 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)
-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
-ab bitrate audio bitrate (please use -b:a)
-b bitrate video bitrate (please use -b:v)
-dn disable data
Audio 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)
-af filter_graph set audio filters
命令說明:
- 封裝格式從 RMVB 格式 (
.rmvb
) 轉(zhuǎn)為 MP4 格式 (.mp4
) - 視頻編碼轉(zhuǎn)為 MPEG4 編碼 (
-vcodec mpeg4
) - 視頻碼率轉(zhuǎn)為 200 kbit/s (
-b:v 200k
) - 轉(zhuǎn)碼后的文件中不包括音頻 (
-an
)
發(fā)布與錄制 RTMP 流
可能用到的選項:
-re read input at native frame rate; equivalent to -readrate 1
-readrate speed read input at specified rate
-stream_loop times set number of times input stream shall be looped
-
-re
等價于-readrate 1
芥喇,采用媒體文件的原生幀率讀取輸入 -
-readrate speed
采用指定的 speed 讀取輸入 -
-stream_loop times
設(shè)置循環(huán)輸入流的次數(shù) (-1 表示無限循環(huán))
RTMP 相關(guān)選項:
rtmpt AVOptions:
-rtmp_app <string> ED......... Name of application to connect to on the RTMP server
-rtmp_buffer <int> ED......... Set buffer time in milliseconds. The default is 3000. (from 0 to INT_MAX) (default 3000)
-rtmp_conn <string> ED......... Append arbitrary AMF data to the Connect message
-rtmp_flashver <string> ED......... Version of the Flash plugin used to run the SWF player.
-rtmp_flush_interval <int> E.......... Number of packets flushed in the same request (RTMPT only). (from 0 to INT_MAX) (default 10)
-rtmp_live <int> .D......... Specify that the media is a live stream. (from INT_MIN to INT_MAX) (default any)
any -2 .D......... both
live -1 .D......... live stream
recorded 0 .D......... recorded stream
-rtmp_pageurl <string> .D......... URL of the web page in which the media was embedded. By default no value will be sent.
-rtmp_playpath <string> ED......... Stream identifier to play or to publish
-rtmp_subscribe <string> .D......... Name of live stream to subscribe to. Defaults to rtmp_playpath.
-rtmp_swfhash <binary> .D......... SHA256 hash of the decompressed SWF file (32 bytes).
-rtmp_swfsize <int> .D......... Size of the decompressed SWF file, required for SWFVerification. (from 0 to INT_MAX) (default 0)
-rtmp_swfurl <string> ED......... URL of the SWF player. By default no value will be sent
-rtmp_swfverify <string> .D......... URL to player swf file, compute hash/size automatically.
-rtmp_tcurl <string> ED......... URL of the target stream. Defaults to proto://host[:port]/app.
-rtmp_listen <int> .D......... Listen for incoming rtmp connections (from INT_MIN to INT_MAX) (default 0)
-listen <int> .D......... Listen for incoming rtmp connections (from INT_MIN to INT_MAX) (default 0)
-tcp_nodelay <int> ED......... Use TCP_NODELAY to disable Nagle's algorithm (from 0 to 1) (default 0)
-timeout <int> .D......... Maximum timeout (in seconds) to wait for incoming connections. -1 is infinite. Implies -rtmp_listen 1 (from INT_MIN to INT_MAX) (default -1)
常用參數(shù):
-
-rtmp_app
: 指定 connect 的 RTMP server 的 application,用于 RTMP 的 connect command -
-rtmp_playpath
: 指定 play 或 publish 的流名 凰萨,用于 RTMP 的 play/publish command -
-rtmp_tcurl
: 目標流的 url 继控,用于 RTMP 的 connect command
示例:
- 發(fā)布命令:
ffmpeg -re -stream_loop -1 -i input.mp4 -c copy -f flv -rtmp_tcurl "mylive.com" -rtmp_app live -rtmp_playpath test_stream "rtmp://127.0.0.1"
- 錄制命令:
ffmpeg -i -rtmp_tcurl "mylive.com" -rtmp_app live -rtmp_playpath test_stream "rtmp://127.0.0.1" -c copy -f flv output.flv
等價于:
- 發(fā)布命令:
ffmpeg -re -stream_loop -1 -i input.mp4 -c copy -f flv "rtmp://mylive.com/live/test_stream"
- 錄制命令:
ffmpeg -i "rtmp://mylive.com/live/test_stream" -c copy -f flv output.flv
錄制 HTTP 流
http 相關(guān)選項:
http AVOptions:
-seekable <boolean> .D......... control seekability of connection (default auto)
-chunked_post <boolean> E.......... use chunked transfer-encoding for posts (default true)
-http_proxy <string> ED......... set HTTP proxy to tunnel through
-headers <string> ED......... set custom HTTP headers, can override built in default headers
-content_type <string> ED......... set a specific content type for the POST messages
-user_agent <string> .D......... override User-Agent header (default "Lavf/59.33.100")
-referer <string> .D......... override referer header
-multiple_requests <boolean> ED......... use persistent connections (default false)
-post_data <binary> ED......... set custom HTTP post data
-cookies <string> .D......... set cookies to be sent in applicable future requests, use newline delimited Set-Cookie HTTP field value syntax
-icy <boolean> .D......... request ICY metadata (default true)
-auth_type <int> ED......... HTTP authentication type (from 0 to 1) (default none)
none 0 ED......... No auth method set, autodetect
basic 1 ED......... HTTP basic authentication
-send_expect_100 <boolean> E.......... Force sending an Expect: 100-continue header for POST (default auto)
-location <string> ED......... The actual location of the data received
-offset <int64> .D......... initial byte offset (from 0 to I64_MAX) (default 0)
-end_offset <int64> .D......... try to limit the request to bytes preceding this offset (from 0 to I64_MAX) (default 0)
-method <string> ED......... Override the HTTP method or set the expected HTTP method from a client
-reconnect <boolean> .D......... auto reconnect after disconnect before EOF (default false)
-reconnect_at_eof <boolean> .D......... auto reconnect at EOF (default false)
-reconnect_on_network_error <boolean> .D......... auto reconnect in case of tcp/tls error during connect (default false)
-reconnect_on_http_error <string> .D......... list of http status codes to reconnect on
-reconnect_streamed <boolean> .D......... auto reconnect streamed / non seekable streams (default false)
-reconnect_delay_max <int> .D......... max reconnect delay in seconds after which to give up (from 0 to 4294) (default 120)
-listen <int> ED......... listen on HTTP (from 0 to 2) (default 0)
-resource <string> E.......... The resource requested by a client
-reply_code <int> E.......... The http status code to return to a client (from INT_MIN to 599) (default 200)
-short_seek_size <int> .D......... Threshold to favor readahead over seek. (from 0 to INT_MAX) (default 0)
常用選項:
-seekable
在使用 FFmpeg 打開直播或點播文件時,可以通過 seek (-ss
)操作進行播放進度移動胖眷、定位等操作:
ffmpeg -ss 300 -seekable 1 -i "http://127.0.0.1/test.flv" -c copy output.flv
-headers
在使用 FFmpeg 拉取 HTTP 流時武通,可以設(shè)置 HTTP 的 header,例如:
ffmpeg -headers "referer: http://127.0.0.1/index.html" -i "http://127.0.0.1/test.flv" -c copy output.flv
-user_agent
在使用 FFmpeg 拉取 HTTP 流時珊搀,可以設(shè)置 HTTP 的 User-Agent冶忱。在流媒體中常見的 User-Agent 有 Android 的 StageFright、IOS 的 QuickTime 等境析,F(xiàn)Fmpeg 也有自己的特殊的默認User-Agent:Lavf 囚枪。
ffmpeg -user_agent "my_user_agent" -i "http://127.0.0.1/test.flv" -c copy output.flv
當然,可以不使用上述的選項:
ffmpeg -i "http://127.0.0.1/test.flv" -c copy output.flv
ffplay 常用命令
在 FFmpeg 中通常使用 ffplay 作為播放器劳淆。不過链沼,ffplay 不僅是播放器,并且可以進行可視化的媒體參數(shù)分析沛鸵。
播放
ffplay input.mp4
ffplay "rtmp://mylive.com/live/test_stream"
ffplay "http://mylive.com/live/test_stream.flv"
更多參數(shù)詳見:ffplay -h
數(shù)據(jù)可視化分析
顯示音頻波形:
ffplay -showmode 1 intput.mp3
顯示宏塊:
ffplay -debug vis_mb_type input.mp4
顯示運動向量信息
ffplay -vismv pf input.mp4
-
pf
: P 幀向前運動估計顯示 -
bf
: B 幀向前運動估計顯示 -
bb
: B 幀向后運動估計顯示
ffprobe 常用命令
ffprobe [options] input_file
常用選項:
-print_format format set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)
-of format alias for -print_format
-select_streams stream_specifier select the specified streams
-show_data show packets data
-show_format show format/container info
-show_frames show frames info
-show_packets show packets info
-show_streams show streams info
-
-print_format
設(shè)置輸出格式括勺,可以是csv
,json
,xml
等。 -
-select_streams stream_specifier
選擇分析的 stream,stream_specifier 詳見上文疾捍。 -
-show_data
查看媒體包數(shù)據(jù) (-show_packets -show_data
一起使用)奈辰。 -
-show_format
查看媒體格式或容器的信息。 -
-show_frames
查看每一幀信息拾氓。 -
-show_packets
查看每一個數(shù)據(jù)包信息(不包含包的data)冯挎。 -
-show_streams
查看流信息。
示例:
- 查看視頻流的幀信息:
ffprobe -show_frames -select_streams v input.flv
- 查看音頻流的包信息(并包含data):
ffprobe -show_packets -show_data -select_streams a input.flv
- 查看流信息
ffprobe -show_streams input.flv
Filter
Filter 分為 simple filter 和 complex filter :
-
simple filter
僅支持相同類型的流的單個輸入和單個輸出咙鞍,使用-filter
選項 -
complex filter
可支持不同類型的流的多個輸入和多個輸出房官,使用-filter_complex
選項
FFmpeg 使用 filter 的參數(shù)排列
"[輸入流或標記名] filter_arg [臨時標記名];[輸入流或標記名] filter_arg [臨時標記名]..."
simple filter
-filter[:stream_specifier] filtergraph_description (output,per-stream)
創(chuàng)建由 filtergraph_description
指定的 filtergraph 并使用它來 filter 流(per-stream 或stream_specifier)。filtergraph_description
是對要應用于流的 filtergraph 的描述续滋。在 filtergraph_description
中翰守,輸入與標簽 in
相關(guān)聯(lián),輸出與標簽 out
相關(guān)聯(lián)疲酌。有關(guān) filtergraph 語法的更多信息蜡峰,請參閱 ffmpeg-filter 手冊。
由于是simple filter
朗恳,因此必須具有相同類型的流的單個輸入和單個輸出(不同類型的多輸入和多輸出詳見:complex filter
)湿颅。如果要創(chuàng)建具有多個輸入和/或輸出的過濾器圖,請參閱 -filter_complex
選項粥诫。
其他選項
-vf filtergraph_description set video filters
等價于
-filter:v filtergraph_description
simple filtergraph
_________ ______________
| | | |
| decoded | | encoded data |
| frames |\ _ | packets |
|_________| \ /||______________|
\ __________ /
simple _\|| | / encoder
filtergraph | filtered |/
| frames |
|__________|
示例
文字水佑秃健:
ffmpeg -i input.mp4 -filter:v "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':x=20:y=20" output.mp4
圖片水印:
ffmpeg -i input.mp4 -filter:v "movie=logo.png[wm]; [in][wm]overlay=30:10[out]" output.mp4
-
movie=logo.png[wm]
中movie
指定水印路徑logo.png
怀浆,臨時標記為wm
谊囚。 -
[in][wm]overlay=30:10[out]
中in
是輸入標簽即input.mp4
,out
是輸出標簽即output.mp4
执赡,wm
是logo.png
的標記镰踏,logo.png
將顯示在input.mp4
的 x 坐標 30、y 坐標 10 的位置沙合。
complex filter
-filter_complex graph_description create a complex filtergraph
complex filtergraph
_________
| |
| input 0 |\ __________
|_________| \ | |
\ _________ /| output 0 |
\ | | / |__________|
_________ \| complex | /
| | | |/
| input 1 |---->| filter |\
|_________| | | \ __________
/| graph | \ | |
/ | | \| output 1 |
_________ / |_________| |__________|
| | /
| input 2 |/
|_________|
示例
圖片水拥煳薄:
ffmpeg -i input.mp4 -i logo.png -filter_complex "[1:v]scale=176:144[logo];[0:v][logo]overlay=x=0:y=0" output.mp4
-
[1:v]scale=176:144[logo]
將[1:v]
表示的logo.png
圖像流縮放為176:144
并標記為logo
。 -
[0:v][logo]overlay=x=0:y=0
將logo
鋪在輸入的視頻input.mp4
的視頻流[0:v]
的左上角首懈。
FFmpeg 采集設(shè)備
Linux 平臺設(shè)備操作
查看設(shè)備列表
ffmpeg -devices
Devices:
D. = Demuxing supported
.E = Muxing supported
--
DE fbdev Linux framebuffer
D lavfi Libavfilter virtual input device
DE oss OSS (Open Sound System) playback
DE video4linux2,v4l2 Video4Linux2 output device
fbdev
FrameBuffer 是 Linux 中專門用于圖像展示操作芳来。
FFmpeg 采集 Linux 下的 v412
設(shè)備時,主要用來采集終端中的圖像(命令行操作)猜拾。
ffmpeg -framerate 30 -f fbdev -i /dev/fb0 output.mp4
v412
Linux 常見視頻設(shè)備 video4linux2
縮寫為 v4l2
即舌。
FFmpeg 采集 Linux 下的 v4l2
設(shè)備時,主要用來采集攝像頭挎袜。
ffmpeg -framerate 30 -f v4l2 -i /dev/video0 output.mp4
x11grab
FFmpeg 采集 Linux 下的 x11grab
設(shè)備時顽聂,主要用來采集桌面肥惭。
x11grab
的設(shè)備名規(guī)則:
[主機名]:顯示編號id.屏幕編號id[+起始x軸, 起始y軸]
其中,主機名紊搪,起始x軸和起始y軸均為可選參數(shù)蜜葱。
桌面錄制:
ffmpeg -f x11grab -framerate 30 -video_size 1352x1288 -i :0.0 output.mp4
桌面錄制指定起始位置
ffmpeg -f x11grab -framerate 30 -video_size 352x288 -i :0.0+300,200 output.mp4
OS X 平臺設(shè)備操作
查看設(shè)備列表
ffmpeg -devices
D avfoundation AVFoundation input device
D lavfi Libavfilter virtual input device
D qtkit QTKit input device
avfoundation
采集內(nèi)置攝像頭:
ffmpeg -f avfoundation -i "FaceTime HD Camera (Built-in)" output.mp4
采集 OS X 桌面:
ffmpeg -f avfoundation -i "Capture screen 0" -r:v 30 output.mp4
采集麥克風:
ffmpeg -f avfoundation -i "0:0" output.aac
Windows 設(shè)備操作
使用 dshow
枚舉和采集音視頻設(shè)備
枚舉設(shè)備:
./ffmpeg.exe -f dshow -list_devices true -i dummy
采集攝像頭和麥克風:
./ffmpeg.exe -f dshow -i video="Integrated Camera" -f dshow -i audio="virtual-audio-capturer" output.mp4
使用 gdigrab
采集桌面或窗口
使用 gdigrab 采集整個桌面:
./ffmpeg.exe -f gdigrab -framerate 6 -i desktop output.mp4
使用 gdigrab 采集某個窗口:
./ffmpeg.exe -f gdigrab -framerate 6 -i title=ffmpeg output.mp4
-
-i title=
指定窗口的標題來進行窗口的查找
帶偏移量(offset_x 和 offset_y)和大小(video_size)的采集:
./ffmpeg.exe -f gdigrab -framerate 6 -offset_x 50 -offset_y 50 -video_size 400x400 -i title=ffmpeg output.mp4
參考資料
《FFmpeg官方文檔》
《FFmpeg從入門到精通》