Summary
This article describes the FFmpeg command line interface specifications and application examples.
Introduction to FFmpeg
FFmpeg is a command line program for processing audio and video. It can be used in various operating systems such as Linux, Windows and Mac OS. It supports various operations such as transcoding, editing, merging, separating, mixing, and screen recording of multiple audio and video formats, and can meet a variety of audio and video processing needs.
The FFmpeg command line parameters are very rich. You need to refer to the documentation and practice repeatedly to use them proficiently.
FFmpeg command usage
The string starting with "-" is the default parameter name of FFmpeg, and the string in the form of ${value} is the parameter value specified by the user.
Typical syntax for FFmpeg commands is:
#URL可以是本地文件路径或网络串流地址
#省略号“...”表示可能存在多个输入或输出参数集。
#在多个输入URL的情况下,“输入选项列表”仅描述紧跟其后的“输入URL”,多输出URL的情况类似。
ffmpeg [全局选项] [[输入URL选项列表] -i 输入URL]... [[输出URL选项列表] 输出URL]...
#“全局选项”示例
ffmpeg -h #查看帮助信息
ffmpeg -version #查看版本信息
#“输入URL”示例
#查看本地媒体文件信息
ffmpeg -i video.mp4
#查看网络串流信息
ffmpeg -i http://127.0.0.1/videofiles/test.flv
#“输出URL”示例
#复制视频流,禁用音频流。
ffmpeg -i test.mp4 -vcodec copy -an video_only.mp4
#复制音频流,禁用视频流。
ffmpeg -i test.mp4 -acodec copy -vn audio_only.mp4
View FFmpeg self-describing information
Internal information about the FFmpeg program can be queried through the following parameters.
-version 显示版本
-buildconf 显示编译配置
-protocols 显示可用的协议
-formats 显示可用格式(muxers+demuxers)
-filters 显示可用的过滤器
-muxers 显示可用复用器
-demuxers 显示可用解复用器
-codecs 显示可用编解码器 (decoders+encoders)}
-decoders 显示可用解码器
-encoders 显示可用编码器
-layouts 显示标准声道名称
-pix_fmts 显示可用的像素格式
-sample_fmts 显示可用的音频采样格式
-bsfs 显示可用比特流filter
-colors 显示可用的颜色名称
Global options
Global options apply to all input and output files, while non-global options apply only to specific input or output files.
-loglevel ${level} 日志级别。
-v loglevel 日志级别。
-report 生成报告。
-max_alloc ${bytes} 单个分配块的最大尺寸。
-y 是否覆盖输出文件。
-n 从不覆盖输出文件。
-ignore_unknown 忽略未知流类型。
-filter_threads 非复杂滤镜线程数量。
-filter_complex_threads 复杂滤镜线程数量。
-stats 编码过程中打印进度报告
-max_error_rate ${rate} 最大解码错误率(取值范围[0.0, 1.0]),超出此阈值将返回错误。
-vol ${volume} 改变音量(256=正常音量)
Main options
-i ${input_file} 输入文件路径或URL。
-f ${format} 强制输入或输出文件格式。输入文件的格式通常是自动检测的,并根据输出文件的文件扩展名进行猜测,因此在大多数情况下不需要此选项。
-ss ${position} 录制或转码的起始时间。
-t ${duration} 录制或转码的持续时长。
-to ${time_stop} 录制或转码的截止时间。
Control options
-re 用一倍帧率读取输入文件,等价于-readrate 1。
-stream_loop ${loop_count} 输入流的循环次数,-1可表示无限次。
Video encoding options
-vcodec ${codec} 指定视频编解码器。
-b:v ${bitrate} 指定视频比特率。
-r ${framerate} 指定视频帧率。
-s ${size} 指定视频分辨率。
-crf ${value} 设置视频质量(Constant Rate Factor)。
Audio encoding options
-acodec ${codec} 指定音频编解码器。
-b:a ${bitrate} 指定音频比特率。
-ar ${samplerate} 指定音频采样率。
-ac ${channels} 指定音频通道数。
Subtitle parameters
-s ${size} 设置视频帧尺寸(WxH 或 缩写)
-sn 禁用字幕
-scodec ${codec} 强制字幕使用编码器${codec} ('copy' to copy stream)
-stag ${fourcc/tag} 强制字幕fourcc/tag
-fix_sub_duration 修正字幕时长duration
-canvas_size ${size} 设置画布大小(WxH 或 缩写)
-spre ${preset} 用指定的preset(预设置文件)作为字幕选项
Format output parameters
-movflags ${faststart} 针对MP4格式的文件,优化其播放过程。
-fpre ${preset_file} 指定预设文件,包含了一系列的编码参数设置。
-metadata ${key}=${value} 设置媒体文件的元数据。
Graph-based filter parameters
-vf ${filter}:应用视频滤镜效果。
-af ${filter}:应用音频滤镜效果。
Application example
#网络串流录制成mp4
ffmpeg -i "http://127.0.0.1/live/stream.m3u8" -c copy output.mp4
#网络串流录制,视频用x264编码,音频用aac编码,视频码率为1Mbps,编码算法配置为medium
ffmpeg -i "http://127.0.0.1/live/stream.m3u8" -c:v libx264 -c:a aac -b:v 1M -preset medium -f mp4 output.mp4
#将mp4文件推流到RTMP地址。
#参数“-re”表示模拟实际播放的速度进行推流。
ffmpeg -re -i input.mp4 -c copy -f flv rtmp://127.0.0.1/live/streamkey
#将mp4文件推流到RTP地址。
#参数“-stream_loop -l”表示无限循环(负1代表无穷次)播放input.mp4。
ffmpeg -re -stream_loop -1 -i "input.mp4" -vcodec h264 -acodec libopus -f rtp_mpegts rtp://127.0.0.1:10000
#参数“-bf 0”表示视频编码时,BFrame数量为0,即禁止使用B帧,避免WebRTC播放时卡顿。
ffmpeg -re -stream_loop -1 -i "input.mp4" -bf 0 -vcodec h264 -acodec libopus -f rtsp -rtsp_transport tcp rtsp://127.0.0.1/live/test
References
20+ FFmpeg command examples for newbies
FFmpeg command line, from beginner to collection and dust
Audio and video learning: ffmpeg common basic commands organized