上一篇:给Android 开发者的 FFmpeg 教程 (一) FFmpeg的介绍与编译
上一篇内容我们探讨了如何对FFmpeg做交叉编译,这一篇的内容我们正式进入FFmpeg的使用学习。观看很多其他的FFmpeg的教程,一般都是从命令行开始介绍。为什么要这样设计呢?因为FFmpeg内置的命令行工具十分强大,我们常用的绝大多数音视频的需求都可以通过命令行来直接实现,根本无需繁杂的编码工作。这一篇我们将从FFmpeg的命令行入手,来初步认识一下FFmpeg常用的命令行。
首先让我们在linux下编译生成一份FFmpeg工具,具体的代码如下:
./configure --prefix=$(pwd)/install --disable-x86asm
make -j4
make install
编译完成后,我们可以在install
目录下看到一个名为ffmpeg
的可执行文件,我们将利用这个文件来学习FFmpeg的命令行。
cd到install
目录,执行指令./ffmpeg -h
,可以查阅FFmpeg的常用命令。
FFmpeg的命令行结构如下所示:
ffmpeg [global_options
] {[input_file_options
] -i input_url
} ... {[output_file_options
] output_url
} ...
FFmpeg的常用命令大致分为6个部分,具体如下:
- FFmpeg的信息查询部分
- 公共操作参数部分
- 文件主要操作参数部分
- 视频操作参数部分
- 音频操作参数部分
- 字幕操作参数部分
下面我们来具体看看这些命令代表的含义,限于篇幅的原因,以下只关注一些常用的且能够在android中用得上的命令。
FFmpeg的信息查询部分
FFmpeg的信息查询部分的常用命令如下所示:
-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 methods
可以通过-decoders
查看FFmpeg支持的解码器、通过-encorders
查看FFmpeg支持的编码器,通过-protocals
查看FFmpeg支持的协议。上述命令行都比较简单,建议各位读者自行尝试一下,在此不再赘述。
公共操作参数部分
FFmpeg的公共操作参数部分的常用命令如下所示:
-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.
-bits_per_raw_sample number set the number of bits per raw sample
-vol volume change audio volume (256=normal)
-loglevel [flags+]loglevel | -v [flags+]loglevel
设置库使用的日志记录级别和标志。
可选的'flags'前缀可以由以下值组成:
-
repeat
指示不应将重复日志输出压缩到第一行,并将省略“最后一条消息重复n次”行。.
-
level
指示日志输出应为每条消息行添加“[level]”前缀。这可以作为日志着色的替代方法,例如在将日志转储到文件时。
也可以通过添加“+”/“-”前缀来单独使用标志,以设置/重置单个标志,而不影响其他“标志”或更改“日志级别”。当同时设置'flags'和'loglevel'时,在最后一个'flags'值和'loglevel'之前应该有一个'+'分隔符.
loglevel
取值为数字或字符串,具体如下所示:
-
quiet, -8
不输出任何日志。
-
panic, 0
仅显示可能导致进程崩溃的致命错误,例如断言失败。目前代码里还没有使用这个级别的log。
-
fatal, 8
仅显示致命错误。发生这些错误之后,程序绝对无法继续执行。
-
error, 16
显示所有错误,包括可以从中恢复的错误。
-
warning, 24
显示所有警告和错误。将显示与可能不正确或意外事件相关的任何消息。
-
info, 32
在处理过程中显示信息性消息。此为默认值。
-
verbose, 40
与
info
相同,只是更详细一些。 -
debug, 48
显示所有内容,包括调试信息。
例如,要启用重复日志输出,请添加级别前缀,并将loglevel设置为verbose:
./ffmpeg -loglevel repeat+level+verbose -i input output
另一个示例,在不影响级别前缀标志或loglevel的当前状态的情况下启用重复日志输出:
./ffmpeg [...] -loglevel +repeat
-y
无需询问即可覆盖输出文件。
-n
不要覆盖输出文件,如果指定的输出文件已存在,则立即退出。
文件主要操作参数部分
FFmpeg的文件主要操作参数部分的常用命令如下所示:
-i url input file url
-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 disposition
-f
强制指定输入或输出文件格式。输入文件的格式通常是自动检测的,而输出文件的格式是根据文件扩展名推断的,因此在大多数情况下不需要此选项。
-c[:stream_specifier] codec (input/output,per-stream)
-codec[:stream_specifier] codec (input/output,per-stream)
为一个或多个流选择编码器(在输出文件之前使用时)或解码器(在输入文件之前使用时)。codec是解码器/编码器的名称或特殊值copy(仅用于输出),用于指示流不做重编码工作。
例如:
./ffmpeg -i INPUT -map 0 -c:v libx264 -c:a copy OUTPUT
使用libx264对所有视频流进行编码,并复制所有音频流。
-t duration (input/output)
当用作输入选项(在-i之前)时,限制从输入文件读取数据的持续时间。
当用作输出选项时(在输出url之前),在输出持续时间达到持续时间后停止写入输出。
-to
和-t
是互斥的,并且-t
具有更高优先级。
-to position (input/output)
在指定位置停止写入输出或读取输入。
-fs limit_size (output)
设置文件大小限制,以字节表示。超过限制后,不再写入更多的字节块。通常输出文件的大小会略大于设置的文件大小。
-metadata[:metadata_specifier] key=value (output,per-metadata)
设置metadata键/值对,也可以使用空值删除metadata。
例如,要在输出文件中设置标题:
./ffmpeg -i in.avi -metadata title="my title" out.flv
要设置第一个音频流的语言,请执行以下操作:
./ffmpeg -i INPUT -metadata:s:a:0 language=eng OUTPUT
视频操作参数部分
FFmpeg的视频操作参数部分的常用命令如下所示:
-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)
-bits_per_raw_sample number set the number of bits per raw sample
-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
-vframes number (output)
设置要输出的视频帧数。这是-frames:v
的废弃的别名,您应该改用-frames:v
。
-r[:stream_specifier] fps (input/output,per-stream)
设置帧速率(Hz值、分数或缩写)。
作为一个输入选项,忽略文件中存储的任何时间戳,而是假设恒定帧速率fps生成时间戳.
作为输出选项,复制或删除输入帧以实现恒定的输出帧速率fps。
例如设置帧率为29.97fps,下面三种方式具有相同的结果:
./ffmpeg -i input.avi -r 29.97 output.mp4
./ffmpeg -i input.avi -r 30000/1001 output.mp4
./ffmpeg -i input.avi -r netsc output.mp4
-fpsmax[:stream_specifier] fps (output,per-stream)
设置最大帧速率(Hz值、分数或缩写)。
当输出帧速率为自动设置且高于此值时,钳制输出帧速率。在批处理或输入帧率错误检测为非常高时有用。它不能与-r一起设置。在streamcopy期间将忽略它。
-s[:stream_specifier] size (input/output,per-stream)
设置帧大小。
-aspect[:stream_specifier] aspect (output,per-stream)
设置视频文件的宽高比。
宽高比可以是浮点数字符串,也可以是形式为num:den的字符串,其中num和den是宽高比的分子和分母。例如,“4:3”、“16:9”、“1.3333”和“1.7777”都是有效的参数值。
-ab bitrate
设置音频码率。推荐使用-b:a
。
-b bitrate
设置视频码率,推荐使用-b:v
。
音频操作参数部分
FFmpeg的音频操作参数部分的常用命令如下所示:
-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
aframes number (output)
设置要输出的音频帧数。这是-frames:a的废弃的别名,您应该改用-frames:a。
-ar[:stream_specifier] freq (input/output,per-stream)
设置音频采样频率。
-aq q (output)
设置音频质量(特定于编解码器,VBR)。这是-q:a的别名。
-ac[:stream_specifier] channels (input/output,per-stream)
设置音频通道的数量。
-sample_fmt[:stream_specifier] sample_fmt (output,per-stream)
设置音频采样格式。可以通过-sample_fmts
查看支持的采样格式。
字幕操作参数部分
FFmpeg的字幕操作参数部分的常用命令如下所示:
-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
-scodec codec (input/output)
设置字幕编解码器。这是-codec:s的别名。
-canvas_size size
设置用于渲染字幕的画布的大小。
-fix_sub_duration
修正字幕持续时间。对于每个字幕,等待同一流中的下一个数据包,并调整第一个数据包的持续时间以避免重叠。对于某些字幕编解码器,尤其是DVB字幕,这是必要的,因为原始数据包中的持续时间只是一个粗略估计,而结束实际上是由一个空字幕帧标记的。如果在必要时不使用此选项,可能会由于非单调时间戳而导致持续时间过长或复用失败。
请注意,此选项将延迟所有数据的输出,直到下一个字幕数据包被解码:它可能会大大增加内存消耗和延迟。
以上对FFmpeg一些命令做了初步的介绍,但FFmpeg的命令远远不止列举的这些,这里只是起到一个抛砖引玉的作用,有兴趣的同学可以翻阅FFmpeg的官方文档查看FFmpeg的全部命令。
下一篇我将会介绍如何在Android上运行FFmpeg的命令,FFmpeg的其他的命令的用法也会在接下来进行穿插性探讨。