文章目录

  • ffplay
    • 命令说明
      • 基础命令
      • 高级命令
    • 使用示例
      • 播放
      • 简单过滤器
  • ffprobe 查看视频信息
    • 选项说明
    • 使用示例
      • 基本使用
      • 输出格式
      • frame 帧信息
  • ffmpeg
    • 命令说明
      • 基本选项
      • 流标识
      • 音频选项
      • 视频选项
      • 高级选项
    • 使用示例
      • 查看支持信息
      • 操作视频
      • 编码格式转换
      • 视频压缩
      • 视频拼接
    • 视频帧操作
    • 图片与视频
    • 图片格式转换
    • 硬解码与软解码
    • 分解与复用命令
    • 处理原始数据的命令
    • 滤镜命令
    • 裁剪与合并命令
    • 图片与视频互转命令
    • 直播拉流和推流

官方文档的 Command Line Tools Documentation 部分,有命令的详细介绍
https://ffmpeg.org/documentation.html

示例来源于网络,待整理更新。


ffplay

命令说明

基础命令

帮助

  • -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

设置

  • -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
  • -sources device, list sources of the input device
  • -sinks device, list sinks of the output device
  • -x width, force displayed width | 强制显示宽带
  • -y height, force displayed height | | 强制显示高度
  • -s size, set frame size (WxH or abbreviation)
  • -fs, force full screen | 以全屏模式启动
  • -an, disable audio | 禁用音频(不播放声音)
  • -vn, disable video | 禁用视频(不播放视频)
  • -sn, disable subtitling | 禁用字幕(不显示字幕)
  • -ss pos, seek to a given position in seconds 跳转到指定的位置,注意时间单位;比如:
    • 55, 55 seconds
    • 12:03:45, 12 hours, 03 minutes and 45 seconds
    • 23.189, 23.189 second
  • -t duration , play “duration” seconds of audio/video | 设置播放视频/音频长度,时间单位如 -ss选项
  • -bytes val, seek by bytes 0=off 1=on -1=auto | 按字节进行跳转(0=off 1=on -1=auto)
  • -seek_interval seconds, set seek interval for left/right keys, in seconds 自定义左/右键定位拖动间隔(以秒为单位),默认值为10秒
  • -nodisp, disable graphical display 关闭图形化显示窗口,视频将不显示
  • -noborder, borderless window 无边框窗口
  • -alwaysontop, window always on top
  • -volume volume, set startup volume 0=min 100=max | 设置起始音量。音量范围[0 ~100]
  • -f fmt, force format
  • -window_title window title, set window title | 设置窗口标题(默认为输入文件名)
  • -af filter_graph, set audio filters
  • -showmode mode, select show mode (0 = video, 1 = waves, 2 = RDFT)
    设置显示模式,可用的模式值:0 显示视频,1 显示音频波形,2 显示音频频谱。
    缺省为0,如果视频不存在则自动选择2
  • -i input_file, read specified file
  • -codec decoder_name, force decoder
  • -autorotate, automatically rotate video

高级命令

  • -cpuflags flags, force specific cpu flags
    打印多个回放统计信息,包括显示流持续时间,编解码器参数,流中的当前位置,以及音频/视频同步差值。
    默认情况下处于启用状态,要显式禁用它则需要指定-nostats。
  • -cpucount count, force specific cpu count
  • -hide_banner, hide_banner do not show program banner
  • -ast stream_specifier, select desired audio stream
    指定音频流索引,比如-ast 3,播放流索引为3的音频流
  • -vst stream_specifier, select desired video stream
    指定视频流索引,比如-vst 4,播放流索引为4的视频流
  • -sst stream_specifier, select desired subtitle stream
    指定字幕流索引,比如-sst 5,播放流索引为5的字幕流
  • -pix_fmt format, force pixel format
  • -stats, show status
  • -fast, non spec compliant optimizations
    非标准化规范的多媒体兼容优化。
  • -genpts, generate pts | 生成pts。
  • -drp, let decoder reorder pts 0=off 1=on -1=auto
  • -lowres
  • -sync type, force audio-video sync. type (type=audio/video/ext)
    同步类型 将主时钟设置为audio(type=audio),video(type=video)或external(type=ext),默认是audio为主时钟。
  • -autoexit, force at the end | 视频播放完毕后退出
  • -exitonkeydown, force on key down | 键盘按下任何键退出播放
  • -exitonmousedown, force on mouse down | 鼠标按下任何键退出播放
  • -loop loop count, force number of times the playback shall be looped
  • -framedrop, drop frames when cpu is too slow | 如果视频不同步则丢弃视频帧。当主时钟非视频时钟时默认开启。 若需禁用则使用 -noframedrop
  • -infbuf, don’t limit the input buffer size (useful with realtime streams)
    不限制输入缓冲区大小。尽可能快地从输入中读取尽可能多的数据。 播放实时流时默认启用,如果未及时读取数据,则可能会丢弃数据。
    此选项将不限制缓冲区的大小。若需禁用则使用 -noinfbuf
  • -left x pos, force the x position for the left of the window
  • -top y pos, force the y position for the top of the window
  • -vf filter_graph, force video filters
  • -rdftspeed msecs, rdft speed
  • -default, generic catch all option
  • -acodec decoder_name, force audio decoder | 强制使用设置的音频解码器进行音频解码
  • -scodec decoder_name, force subtitle decoder | 强制使用设置的字幕解码器进行字幕解码
  • -vcodec decoder_name, force video decoder | 强制使用设置的视频解码器进行视频解码
  • -find_stream_info, read and decode the streams to fill missing information with heuristics
  • -filter_threads, number of filter threads per graph

使用示例

播放

# 播放音频、视频文件
ffplay test.mp3ffplay input.mp4# 播放完自动退出
ffplay -autoexit input.mp4# 循环播放文件10次
ffplay 文件名 -loop 10# 播放本地文件, 设置标题
ffplay -window_title "test time" -ss 2 -t 10 -autoexit test.mp4# 播放网络流
ffplay -window_title "rtmp stream" rtmp://10.0.100.88:443/webcast/bshdlive-pc# 禁用音频或视频
# 禁用音频:
ffplay test.mp4 -an# 禁用视频:
ffplay test.mp4 -vn# 播放视频第一路音频流;参数如果是2 就是第二路音频流,如果没有就会静音。
ffplay 文件名 -ast 1# 播放视频第一路视频流;参数如果是2 就是第二路视频流 没有显示黑屏
ffplay 文件名 -vst 1# 播放pcm文件 必须设置参数正确
ffplay .pcm文件 -f 格式 -channels 2 声道数 -ar 采样率# -f rawvideo 代表原始格式
ffplay -f rawvideo -pixel_format yuv420p -s 480480 .yuv文件# -s 480480 宽高
-pixel_format yuv420p 表示格式# 播放rgb原始数据
ffplay -f rawvideo -pixel_format rgb24 -s 480*480 .rgb文件# 指定ffplay使用音频为基准进行音视频同步默认ffplay也是这样
ffplay 文件名 -sync audio# 指定ffplay使用视频为基准进行音视频同步
ffplay 文件名 -sync video# 指定ffplay使用外部时钟为基准进行音视频同步
ffplay 文件名 -ext video

# 强制解码器
# mpeg4解码器
ffplay -vcodec mpeg4 test.mp4# h264解码器:
ffplay -vcodec h264 test.mp4 # 播放YUV数据
ffplay -pixel_format yuv420p -video_size 320x240 -framerate 5 yuv420p_320x240.yuv# 播放RGB数据
ffplay -pixel_format rgb24 -video_size 320x240 -i rgb24_320x240.rgbffplay -pixel_format rgb24 -video_size 320x240 -framerate 5 -i rgb24_320x240.rgb# 播放PCM数据
ffplay -ar 48000 -ac 2 -f f32le 48000_2_f32le.pcm-ar set audio sampling rate (in Hz) (from 0 to INT_MAX) (default 0)-ac set number of audio channels (from 0 to INT_MAX) (default 0)

简单过滤器

# 视频旋转
ffplay -i test.mp4 -vf transpose=1# 视频反转
ffplay test.mp4 -vf hflip
ffplay test.mp4 -vf vflip# 视频旋转和反转
ffplay test.mp4 -vf hflip,transpose=1# 音频变速播放
ffplay -i test.mp4 -af atempo=2# 视频变速播放
ffplay -i test.mp4 -vf setpts=PTS/2# 音视频同时变速
ffplay -i test.mp4 -vf setpts=PTS/2 -af atempo=2

ffprobe 查看视频信息

选项说明

  • -L, 显示协议
  • -h/-?/-help/--help topic, 帮助可以选择话题
  • -version, 显示版本
  • -buildconf, 展示编译配置选项
  • -formats, 显示支持的编码
  • -muxers, 展示支持的封装器
  • -demuxers, 展示支持的解封装器
  • -devices, 展示支持的设备
  • -codecs, 展示支持的编码
  • -decoders, 显示支持的解码器
  • -encoders, 显示支持的编码器

  • -bsfs, 显示支持的比特流过滤器
  • -protocols, 展示支持的协议
  • -filters, 展示支持的过滤器
  • -pix_fmts, 显示支持的像素格式
  • -layouts, 展示支持的声道格式
  • -sample_fmts, 显示支持的采样格式
  • -colors, 展示支持的颜色名称
  • -loglevel loglevel, 设置日志级别
  • -v loglevel, 设置日志级别
  • -report, 生成报告
  • -max_alloc bytes, 设置单个已分配块的最大大小
  • -cpuflags flags, 指定cpu标志
  • -hide_banner hide_banner, 不显示程序横幅
  • -sources device, 列出源的输出设备
  • -sinks device, 列出输出设备的接收器
  • -f format, 指定格式
  • -unit, 显示显示值的单位
  • -prefix, 对显示的值使用SI前缀
  • -byte_binary_prefix, 对字节单位使用二进制前缀
  • -sexagesimal, 对时间单位使用六十进制格式 HOURS:MM:SS.MICROSECONDS
  • -pretty, 美化显示输出的值,让人可读

  • -print_format format, 设置打印格式 (available formats are: default, compact, csv, flat, ini, json, xml)
  • -of format, -print_format的编码

  • -select_streams stream_specifier, 选择指定的stream
  • -sections, 打印节结构和节信息,然后退出

  • -show_data, 显示数据包信息
  • -show_data_hash, 显示数据包hash值
  • -show_error, 显示探测中的错误
  • -show_format, 显示格式/容器信息
  • -show_frames, 显示帧信息
  • -show_format_entry entry, 显示格式/容器信息中的特定条目
  • -show_entries entry_list, 显示一组指定的项
  • -show_log, 显示log
  • -show_packets, 显示packet信息
  • -show_programs , 显示程序信息
  • -show_streams, 显示stream的信息
  • -show_chapters, 显示chapters的信息
  • -count_frames, 每个stream中的帧数
  • -count_packets, 每个stream中的包数量

  • -show_program_version, ffprobe的版本
  • -show_library_versions, 库的版本
  • -show_versions, 程序和库的版本号

  • -show_pixel_formats, 展示像素格式描述
  • -show_private_data, 显示私有数据
  • -private, 和显示私有数据一样
  • -bitexact, 强制提取bit输出
  • -read_intervals read_intervals, 设置读取间隔
  • -default, 默认所有选项
  • -i input_file, 读取指定文件
  • -print_filename print_file, 重新显示输入的文件名
  • -find_stream_info, 读取并解码流,用启发式方法填充缺失的信息

使用示例

基本使用

# 查看音频视频文件信息
ffprobe 文件名# 查看文件的输出格式信息,时间长度,文件大小,比特率,流数目等。
ffprobe -show_format 文件名# 以json格式输出 流 信息
ffprobe -print_format json -show_streams 文件名# 显示帧信息
ffprobe -show_frames 文件名# 查看包信息
ffprobe -show_packets 文件名

输出格式

支持格式:xml、ini、json、csv、flat

ffprobe -show_format test.mp4 -print_format jsonffprobe -show_frames -of json input.flv

frame 帧信息

 # 查看视频总帧数
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 gemfield.mp4ffprobe -select_streams v -of xml -show_entries frame=pkt_pts,pkt_dts,pkt_size,pkt_duration,pict_type a.mp4# 查看音频总帧数
ffprobe -v error -count_frames -select_streams a:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 -skip_frame nokey gemfield.mp4 # 查看 key frame 所在的时间
ffprobe -v error -skip_frame nokey -select_streams v:0 -show_entries frame=pkt_pts_time -of csv=print_section=0 gemfield.mp4 # 查看 key frame 分布的情况
ffprobe -v error -show_frames gemfield.mp4 | grep pict_type# 查看 key frame 所在的帧数
ffprobe -v error -select_streams v -show_frames -show_entries frame=pict_type -of csv gemfield.mp4 | grep -n I | cut -d ':' -f 1# 重新设置 key frame interval
ffmpeg -i gemfield.mp4 -vcodec libx264 -x264-params keyint=1:scenecut=0 -acodec copy out.mp4# 查看视频波特率
ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 gemfield.mp4

# 只查看音频(a)、视频(v)、字幕(s)的信息
ffprobe -show_frames -select_streams v -of json input.mp4
ffprobe -show_frames -select_streams a -of json input.mp4

# packet 数据包
ffprobe -show_packets video.mp4

show_packets 查看的多媒体包信息使用PACKET标签所包括起来,其中包含的信息主要如下:

  • codec_type: 多媒体类型,例如视频包,音频包等;
  • stream_index: 多媒体的stream索引;
  • pts: 多媒体的显示时间值
  • pts_time: 根据不同格式计算过后的多媒体的显示时间
  • dts: 多媒体解码时间值
  • dts_time: 根据不同格式计算过后的多媒体解码时间
  • duration: 多媒体包占用的时间值
  • duration_time: 根据不同格式计算过后的多媒体包占用的时间值
  • size: 多媒体包的大小
  • pos: 多媒体包所在的文件偏移位置
  • flags: 多媒体包标记,关键包与非关键包的标记

ffmpeg

ffmpeg [global options] {[infile options]['-i' 'infile'] ...} {[outfile options] 'outfile' ...}

参数选项由三部分组成:可选的一组全局参数、一组或多组输入文件参数、一组或多组输出文件参数。
其中,每组输入文件参数以 -i 为结束标记;
每组输出文件参数以输出文件名为结束标记。


ffmepg 中的转码过程,如下图:

 _______              ______________
|       |            |              |
| input |  demuxer   | encoded data |   decoder
| file  | ---------> | packets      | -----+
|_______|            |______________|      |v_________|         || decoded || frames  ||_________|________             ______________       |
|        |           |              |      |
| output | <-------- | encoded data | <----+
| file   |   muxer   | packets      |   encoder
|________|           |______________|

Filtering

Simple filtergraphs

 _________                        ______________
|         |                      |              |
| decoded |                      | encoded data |
| frames  |\                   _ | packets      |
|_________| \                  /||______________|\   __________   /simple     _\||          | /  encoderfiltergraph   | filtered |/| frames   ||__________|_______        _____________        _______        ________
|       |      |             |      |       |      |        |
| input | ---> | deinterlace | ---> | scale | ---> | output |
|_______|      |_____________|      |_______|      |________|

Complex filtergraphs

 _________
|         |
| input 0 |\                    __________
|_________| \                  |          |\   _________    /| output 0 |\ |         |  / |__________|_________     \| complex | /
|         |     |         |/
| input 1 |---->| filter  |\
|_________|     |         | \   __________/| graph   |  \ |          |/ |         |   \| output 1 |_________   /  |_________|    |__________|
|         | /
| input 2 |/
|_________|

命令说明

基本选项

支持能力

  • -formats, 列出支持的文件格式。
  • -codecs, 列出支持的编解码器。
  • -decoders, 列出支持的解码器。
  • -encoders, 列出支持的编码器。
  • -protocols, 列出支持的协议。
  • -bsfs, 列出支持的比特流过滤器。
  • -filters, 列出支持的滤镜。
  • -pix_fmts, 列出支持的图像采样格式。
  • -sample_fmts, 列出支持的声音采样格式。

常用输入选项

  • -i filename, 指定输入文件名。
  • -f fmt, 强制设定文件格式,需使用能力集列表中的名称(缺省是根据扩展名选择的)。
  • -ss hh:mm:ss[.xxx], 设定输入文件的起始时间点,启动后将跳转到此时间点然后开始读取数据。
  • -y, 覆盖已有文件。
  • -t duration, 指定时长。
  • -fs limit_size, 设置文件大小的上限。
  • -ss limit_size, 从指定的时间(单位为秒)开始,也支持[-]hh:mm:ss[.xxx]格式
  • -re, 代表按照帧率发送。在作为推流工具的时候一定要加入该参数,否则ffmpeg会按照最高速率向流媒体服务器不停地发送数据。
  • -map, 指定输出文件的流映射关系。如果没有-map选项,则ffmpeg采用默认的映射关系。

对于输入,以下选项通常是自动识别的,但也可以强制设定。

  • -c codec, 指定解码器,需使用能力集列表中的名称。
  • -acodec codec, 指定声音的解码器,需使用能力集列表中的名称。
  • -vcodec codec, 指定视频的解码器,需使用能力集列表中的名称。
  • -b:v bitrate, 设定视频流的比特率,整数,单位bps。
  • -r fps, 设定视频流的帧率,整数,单位fps。
  • -s WxH, 设定视频的画面大小。也可以通过挂载画面缩放滤镜实现。
  • -pix_fmt format, 设定视频流的图像格式(如RGB还是YUV)。
  • -ar sample rate, 设定音频流的采样率,整数,单位Hz。
  • -ab bitrate, 设定音频流的比特率,整数,单位bps。
  • -ac channels, 设置音频流的声道数目。

常用输出选项

  • -f fmt, 强制设定文件格式,需使用能力集列表中的名称(缺省是根据扩展名选择的)。
  • -c codec, 指定编码器,需使用能力集列表中的名称(编码器设定为”copy“表示不进行编解码)。
  • -acodec codec, 指定声音的编码器,需使用能力集列表中的名称(编码器设定为”copy“表示不进行编解码)。
  • -vcodec codec, 指定视频的编码器,需使用能力集列表中的名称(编解码器设定为”copy“表示不进行编解码)。
  • -r fps, 设定视频编码器的帧率,整数,单位fps。
  • -pix_fmt format, 设置视频编码器使用的图像格式(如RGB还是YUV)。
  • -ar sample rate, 设定音频编码器的采样率,整数,单位Hz。
  • -b bitrate, 设定音视频编码器输出的比特率,整数,单位bps。
  • -ab bitrate, 设定音频编码器输出的比特率,整数,单位bps。
  • -ac channels, 设置音频编码器的声道数目。
  • -an, 忽略任何音频流。
  • -vn, 忽略任何视频流。
  • -t hh:mm:ss[.xxx], 设定输出文件的时间长度。
  • -to hh:mm:ss[.xxx], 如果没有设定输出文件的时间长度的画可以设定终止时间点。

流标识

FFMPEG的某些选项可以对一个特定的媒体流起作用,这种情况下需要在选项后面增加一个流标识。流标识允许以下几种格式`,