天天看點

FFmpeg簡介及常見用法

作者:音視訊流媒體技術

本文主要介紹 FFmpeg(Fast Forward MPEG)的相關知識及其常見用法。

說明:本文也會介紹 FFmpeg 包含的其他工具,如 ffprobe。

1 概述

引用官網的介紹:

FFmpeg is the leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created. It supports the most obscure ancient formats up to the cutting edge. No matter if they were designed by some standards committee, the community or a corporation. It is also highly portable: FFmpeg compiles, runs, and passes our testing infrastructure FATE across Linux, Mac OS X, Microsoft Windows, the BSDs, Solaris, etc. under a wide variety of build environments, machine architectures, and configurations.

引用官方的簡介:

A complete, cross-platform solution to record, convert and stream audio and video.

簡單說,FFmpeg 提供了一個跨平台的解決方案,其包含了視音頻的記錄(采集)、編解碼、格式轉換和流化功能。

使用 FFmpeg 進行視音頻的轉換非常容易,例如使用如下指令将視訊由mp4格式轉換為avi格式:

ffmpeg -i input.mp4 output.avi           

2 包含的工具和庫

2.1 包含的工具

目前 FFmpeg 包含以下幾種工具:

ffmpeg: a command line tool to convert multimedia files between formats.

ffplay: a simple media player based on SDL and the FFmpeg libraries.

ffprobe: a simple multimedia stream analyzer.

2.1 包含的庫

目前 FFmpeg 包含以下幾種庫:

libavutil: libavutil is a library containing functions for simplifying programming, including random number generators, data structures, mathematics routines, core multimedia utilities, and much more.

libavcodec: libavcodec is a library containing decoders and encoders for audio/video codecs.

libavformat: libavformat is a library containing demuxers and muxers for multimedia container formats.

libavdevice: libavdevice is a library containing input and output devices for grabbing from and rendering to many common multimedia input/output software frameworks, including Video4Linux, Video4Linux2, VfW, and ALSA.

libavfilter: libavfilter is a library containing media filters.

libswscale: libswscale is a library performing highly optimized image scaling and color space/pixel format conversion operations.

libswresample: libswresample is a library performing highly optimized audio resampling, rematrixing and sample format conversion operations.

相關學習資料推薦,點選下方連結免費報名,先碼住不迷路~】

【免費分享】音視訊學習資料包、大廠面試題、技術視訊和學習路線圖,資料包括(C/C++,Linux,FFmpeg webRTC rtmp hls rtsp ffplay srs 等等)有需要的可以點選加群免費領取~

FFmpeg簡介及常見用法

3 Linux平台下的常見用法

本章介紹在 Linux 平台下的 ffmpeg 指令的常見用法。

說明:

對于重複出現的 ffmpeg 指令選項,隻在該選項首次出現時進行介紹;

ffmpeg 的很多選項會區分“輸入選項(input option)”和“輸出選項(output option)”場景,即同一個指令選項,在作為輸入選項和輸出選項時,其作用有時是不同的。ffmpeg 規定,當選項位于“-i”之前,則其用為輸入選項;當選項位于“輸出url”之前,則其用為輸出選項。

3.1 将本地視訊檔案轉為直播流

示例指令如下:

ffmpeg -re -i feast-2880x1440.mp4 -codec copy -f flv rtmp://192.168.110.168:1935/live/zb           

上面的指令将本地視訊檔案 feast-2880x1440.mp4 轉換為直播流 rtmp://192.168.110.168:1935/live/zb。

下面詳細介紹上面的指令中涉及的選項。

-re (input): Read input at native frame rate. Mainly used to simulate a grab device, or live input stream (e.g. when reading from a file). Should not be used with actual grab devices or live input streams (where it can cause packet loss). By default ffmpeg attempts to read the input(s) as fast as possible. This option will slow down the reading of the input(s) to the native frame rate of the input(s). It is useful for real-time output (e.g. live streaming).

-i url (input): input file url

-codec[:stream_specifier] codec (input/output,per-stream):Select an encoder (when used before an output file) or a decoder (when used before an input file) for one or more streams. codec is the name of a decoder/encoder or a special value "copy" (output only) to indicate that the stream is not to be re-encoded.

說明:目前ffmpeg版本支援的“codec”的值,可通過下列指令查詢:

ffmpeg -codecs           

本示例用到了 -codec copy 的用法,即 stream copy 的概念。關于 stream copy,解釋如下:

Stream copy is a mode selected by supplying the copy parameter to the -codec option. It makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only demuxing and muxing. It is useful for changing the container format or modifying container-level metadata. The diagram above will, in this case, simplify to this:

_______              ______________            ________
|       |            |              |          |        |
| input |  demuxer   | encoded data |  muxer   | output |
| file  | ---------> | packets      | -------> | file   |
|_______|            |______________|          |________|           

Since there is no decoding or encoding, it is very fast and there is no quality loss. However, it might not work in some cases because of many factors. Applying filters is obviously also impossible, since filters work on uncompressed data.

-f fmt (input/output): Force input or output file format. The format is normally auto detected for input files and guessed from the file extension for output files, so this option is not needed in most cases.

3.2 從視訊檔案中提取H264資料

示例指令如下:

ffmpeg -i video_AFP.mp4 -c:v copy -bsf:v h264_mp4toannexb -an video_AFP.h264           

上面的指令從本地視訊檔案 video_AFP.mp4 中提取H264資料,并導出至轉換檔案 video_AFP.h264 中。

下面詳細介紹上面的指令中涉及的選項。

-c[:stream_specifier] codec (input/output,per-stream): 等同于前文介紹的選項“-codec[:stream_specifier] codec (input/output,per-stream)”。

-bsf[:stream_specifier] bitstream_filters (output,per-stream): Set bitstream filters for matching streams. bitstream_filters is a comma-separated list of bitstream filters. Use the "-bsfs" option to get the list of bitstream filters.

-an (input/output):

As an input option, blocks all audio streams of a file from being filtered or being automatically selected or mapped for any output. See "-discard" option to disable streams individually.

As an output option, disables audio recording i.e. automatic selection or mapping of any audio stream. For full manual control see the "-map" option.

3.3 捕獲直播流内容并存儲在本地(或轉發)

示例指令如下:

ffmpeg -i rtsp://10.11.111.161:8554/live -codec copy -y capture.mp4           

上面的指令捕獲 rtsp 協定直播流的内容,并将捕獲到的内容存儲到本地檔案“capture.mp4”中。如果想要将捕獲到的内容進行轉發,則使用直播流URL替換本地檔案名稱即可。

-y (global): Overwrite output files without asking.

3.4 禁用音頻

示例指令如下:

ffmpeg -i rtsp://10.11.111.162:8554/live -vcodec copy -an -y capture_without_audio.mp4           

上面的指令捕獲 rtsp 協定直播流的内容,然後去除捕獲内容中的音頻流資料,隻将捕獲内容中的視訊流内容存儲到本地檔案“capture_without_audio.mp4”中。

-an (input/output):

As an input option, blocks all audio streams of a file from being filtered or being automatically selected or mapped for any output. See "-discard" option to disable streams individually.

As an output option, disables audio recording i.e. automatic selection or mapping of any audio stream. For full manual control see the "-map" option.

3.5 截圖

示例指令如下:

ffmpeg -i "rtsp://192.168.237.162:8554/live" -frames:v 1 -y snapshot.png           

上面的指令用于截取接收到的 rtsp 協定直播流的第一幀内容,并将截取的圖檔内容儲存至 snapshot.png 圖檔檔案。其中,“-frames:v 1”指定了待截取的幀數總數為1。

-frames[:stream_specifier] framecount (output,per-stream)

Stop writing to the stream after framecount frames.

說明:使用 ffmpeg 指令進行截圖操作時,輸入的 url 可以是本地檔案,也可以是直播流内容,不過如果視訊類型是直播流,那麼由于 ffmpeg 擷取直播流資訊需要花費時間(根據直播流協定等資訊的不同,所需花費的時間不同,一般約為1-4秒),是以,通常對于直播流的截圖操作所需時間大于對于本地檔案的截圖操作。

3.6 截取視訊片段

示例指令如下:

ffmpeg -ss 00:11:00 -to 00:24:25 -i livestream.mp4 -f mp4 -codec copy -q:v 1 output.mp4           

上面的指令用于截取視訊檔案中的指定時間間隔内容,得到的視訊檔案為 output.mp4。下面對上述指令中的選項進行說明。

-ss position (input/output):

When used as an input option (before "-i"), seeks in this input file to position. Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved.

When used as an output option (before an output url), decodes but discards input until the timestamps reach position.

position must be a time duration specification, see the Time duration section in the ffmpeg-utils(1) manual.

-to position (input/output):

Stop writing the output or reading the input at position. position must be a time duration specification, see the Time duration section in the ffmpeg-utils(1) manual.

-to and -t are mutually exclusive and -t has priority.

-q:v,等同于下列選項:

-q[:stream_specifier] q (output,per-stream)

-qscale[:stream_specifier] q (output,per-stream)

Use fixed quality scale (VBR). The meaning of q/qscale is codec-dependent. If qscale is used without a stream_specifier then it applies only to the video stream, this is to maintain compatibility with previous behavior and as specifying the same codecspecific value to 2 different codecs that is audio and video generally is not what is intended when no stream_specifier is used.

“-q:v”選項可以簡單了解為設定輸出視訊的品質,其中 q 取值範圍是[1, 35],取值為 1 的時候,對應着最佳的視訊品質。

3.7 檢視視訊資訊

示例指令如下:

ffprobe rtsp://192.168.237.161:8554/live

上面命名的執行結果如下:

FFmpeg簡介及常見用法

通過上面的圖檔内容可知,ffprobe 可以檢視視訊直播流的編碼格式、分辨了、幀率、掃描方式等資訊。

将上述示例中的直播流URL改為視訊檔案名,如 xxx.mp4,即可檢視對應視訊檔案的相關資訊了。

原文連結:FFmpeg簡介及常見用法_liitdar的部落格-CSDN部落格