FFmpeg应用实践之命令查询

0. 前言

FFmpeg 中常用的工具有三个,分别是多媒体编解码工具ffmpeg、多媒体内容分析工具ffprobe和多媒体播放器ffplay。本文介绍的指令都是与编解码工具 ffmpeg 相关的。

学会查询内置文档能够解决我们遇到的很多参数设置问题,也可以避免因为网上各种版本不同、质量参差不齐的答案而困惑。

1. ffmpeg 帮助信息

ffmpeg 命令行指令语法格式如下

ffmpeg [global_options] {[input_file_options] -i input_url} ... {[output_file_options] output_url} ...

ffmpeg 命令行应用中,最基础的操作就是查询一些诸如版本、支持的编码方式/编解码器/滤镜等信息。

首先我们要学会使用的是最最基本的一个命令 -help,有了它我们就可以更愉快地学习与之相关的各种功能了。

执行 ffmpeg -h 后,在终端会打印出与当前使用版本相关的帮助信息。通常在 Linux 系统下使用时可以用如下所示的命令行指令,利用管道加上 less 后可以使用 /match_template 来快速匹配定位到自己关心的部分。

~$ ffmpeg -h full | less
ffmpeg version 3.4.8-0ubuntu0.2 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.5.0-3ubuntu1~18.04)
  configuration: --prefix=/usr --extra-version=0ubuntu0.2 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu
--incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth
--enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca
--enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme
--enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus
--enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr
--enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx
--enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq
--enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm
--enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
...

除了上述最基本的使用方法外,使用 -h type=name 参数可以用来查看指定名称的编/解码器、复用/解复用器、滤镜等的详细信息,非常实用。比如下面的示例中查看 H.264(AVC) 编码器的配置参数。

~$ ffmpeg -h encoder=h264 | less
...
Encoder libx264 [libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10]:
    General capabilities: delay threads
    Threading capabilities: auto
    Supported pixel formats: yuv420p yuvj420p yuv422p yuvj422p yuv444p yuvj444p nv12 nv16 nv21
libx264 AVOptions:
...

ffmpeg -h 输出信息中可以看到,ffmpeg 常见命令大概分为六类,分别是

  • ffmpeg 信息查询部分
  • 全局配置参数部分
  • 文件主要配置参数部分
  • 视频配置参数部分
  • 音频配置参数部分
  • 字幕配置参数部分

2. ffmpeg 信息查询指令

可以通过本小节的参数来查询 ffmpeg 支持的封装或者编解码格式等。

-L                  显示ffmpeg目前所支持的License协议
-version            显示版本信息
-buildconf          显示编译配置
-formats            显示可用的格式
-muxers             显示可用的复用器
-demuxers           显示可用的解复用器
-devices            显示可用的设备
-codecs             显示可用的编解码器,包括编码器和解码器
-decoders           显示可用的解码器
-encoders           显示可用的编码器
-bsfs               显示可用的比特流滤镜
-protocols          显示可用的协议
-filters            显示可用的滤镜
-pix_fmts           显示可用的像素格式
-layouts            显示标准声道名称
-sample_fmts        显示可用的音频采样格式
-colors             显示可用的颜色名称
-sources device     列出输入设备的来源
-sinks device       列出输出设备的接收器
-hwaccels           显示可用的HW加速方法

比如使用 -version 参数可以查看 ffmepg 的版本,包括子模块如libavutil、libavcodec、libavformat、libavdevice、libavfilter、libavresample、libswscale、libswresample、libpostproc等的详细版本信息。

使用 -formats 参数可以查看当前使用的 ffmpeg 是否支持对应文件的视频格式。这样当我们使用 ffmpeg 转码时如果遇到报错提示不支持生成对应的视频文件,就可以进行相应的调整。

如下示例是使用 -devices 参数列出可用设备的输出结果。

~$ ffmpeg -devices
...
Devices:
 D. = Demuxing supported
 .E = Muxing supported
 --
 DE alsa            ALSA audio output
  E caca            caca (color ASCII art) output device
 DE fbdev           Linux framebuffer
 D  iec61883        libiec61883 (new DV1394) A/V input device
 D  jack            JACK Audio Connection Kit
 D  lavfi           Libavfilter virtual input device
 D  libcdio          
 D  libdc1394       dc1394 v.2 A/V grab
 D  openal          OpenAL audio capture device
  E opengl          OpenGL output
 DE oss             OSS (Open Sound System) playback
 DE pulse           Pulse audio output
  E sdl,sdl2        SDL2 output device
 DE sndio           sndio audio playback
  E v4l2            Video4Linux2 output device
 D  video4linux2,v4l2 Video4Linux2 device grab
 D  x11grab         X11 screen capture, using XCB
  E xv              XV (XVideo) output device

3. 全局相关的配置选项

Global 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 ratio of errors (0.0: no errors, 1.0: 100% error  maximum error rate
-bits_per_raw_sample number  set the number of bits per raw sample
-vol volume         change audio volume (256=normal)

-v loglevel 用于设置日志输出级别,不同的等级输出的信息密度和重要程度有所区别,一般设置成 info 就能获取足够的信息用于调试。

-y 用于输出时覆盖已有文件。

4. 单个文件相关的配置选项

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        disposition

-f fmt 用于指定输出或输出文件的格式。

-frames number 用于设置输出文件帧的数量。

5. 多媒体视频相关的配置选项

Video options:
-vframes number     set the number of video frames to output
-r rate             set 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 用于设置输出视频帧的数量。

-r rate 用于设置输出视频的帧率。

-vn 常用于只保留音频数据的场合。

-vf filter_graph 用于配置相关的滤镜。

6. 多媒体音频相关的配置选项

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)
-vol volume         change audio volume (256=normal)
-af filter_graph    set audio filters

-ac channels 用于设置音频通道数量,单声道、立体声等。

-an 常用于只保留视频数据的场合。

7. 多媒体字幕相关的配置选项

Subtitle 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

-sn 设置输出结果中屏蔽字幕信息,只保留音频或视频。


参考资料

[1] ffmpeg Documentation http://ffmpeg.org/ffmpeg-all.html

[2] FFmpeg命令行工具-实用命令 https://www.jianshu.com/p/124aee284a61

[3] ffmpeg常用命令 #32 https://github.com/gnipbao/iblog/issues/32

A. ps

x264 编码库涉及到的可配置参数同样可以使用上述类似方法查阅

~$ x264 -h full | less

Syntax: x264 [options] -o outfile infile

Infile can be raw (in which case resolution is required),
 or YUV4MPEG (*.y4m),
 or Avisynth if compiled with support (yes).
 or libav* formats if compiled with lavf support (no) or ffms 
support (no).
Outfile type is selected by filename:
.264 -> Raw bytestream
.mkv -> Matroska
.flv -> Flash Video
.mp4 -> MP4 if compiled with GPAC or L-SMASH support (no)
Output bit depth: 8/10

Presets:

     --profile <string>      Force the limits of an H.264 profile
                                 Overrides all settings.
                                 - baseline, main, high, high10, high422, high444
     --preset <string>       Use a preset to select encoding settings [medium]
                                 Overridden by user settings.
                                 - ultrafast,superfast,veryfast,faster,fast
                                 - medium,slow,slower,veryslow,placebo
     --tune <string>         Tune the settings for a particular type of source
                             or situation
                                 Overridden by user settings.
                                 Multiple tunings are separated by commas.
                                 Only one psy tuning can be used at a time.
                                 - psy tunings: film,animation,grain,
                                                stillimage,psnr,ssim
                                 - other tunings: fastdecode,zerolatency

Frame-type options:

 -I, --keyint <integer or "infinite"> Maximum GOP size [250]
     --tff                   Enable interlaced mode (top field first)
     --bff                   Enable interlaced mode (bottom field first)
     --pulldown <string>     Use soft pulldown to change frame rate
                                 - none, 22, 32, 64, double, triple, euro (requires cfr input)

Ratecontrol:

 -B, --bitrate <integer>     Set bitrate (kbit/s)
     --crf <float>           Quality-based VBR (-12-51) [23.0]
     --vbv-maxrate <integer> Max local bitrate (kbit/s) [0]
     --vbv-bufsize <integer> Set size of the VBV buffer (kbit) [0]
 -p, --pass <integer>        Enable multipass ratecontrol
                                 - 1: First pass, creates stats file
                                 - 2: Last pass, does not overwrite stats file


Input/Output:

 -o, --output <string>       Specify output file
     --sar width:height      Specify Sample Aspect Ratio
     --fps <float|rational>  Specify framerate
     --seek <integer>        First frame to encode
     --frames <integer>      Maximum number of frames to encode
     --level <string>        Specify level (as defined by Annex A)
     --quiet                 Quiet Mode

Filtering:

     --vf, --video-filter <filter0>/<filter1>/... Apply video filtering to the input file

     Filter options may be specified in <filter>:<option>=<value> format.

     Available filters:
     crop:left,top,right,bottom
     select_every:step,offset1[,...]

本文作者 :phillee
发表日期 :2021年5月19日
本文链接https://www.cnblogs.com/phillee/p/14785534.html
版权声明 :自由转载-非商用-非衍生-保持署名(创意共享3.0许可协议/CC BY-NC-SA 3.0)。转载请注明出处!
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。

感谢您的支持

¥ 打赏

微信支付

posted @ 2021-05-19 16:06  coffee_tea_or_me  阅读(915)  评论(0编辑  收藏  举报