FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。
Homepage
Download FFmpeg
命令选项
参数详解
https://zhuanlan.zhihu.com/p/429870096
| 1
 | ffmpeg -i [输入文件名] [参数选项] -f [输出格式] [输出文件名]
 | 
主要选项
https://www.ffmpeg.org/ffmpeg-all.html#toc-Main-options
| 基本选项 | 参数 | 说明 | 
| -i | 文件名、外接列表文件 | 表示输入文件 | 
| -stats |  | 
| -f |  | 用来指定封装格式 | 
| -map | -map 0:1(0表示文件索引 1表示流索引) | 映射流 | 
| -hide_banner |  | 禁止打印横幅(版权声明、构建选项和库版本) | 
| 视频选项 | 参数 | 说明 | 
| -s |  | 用来指定导出帧序列的大小(可缺省) | 
| -r |  | 表示提取的帧率(可缺省) | 
| -vn |  | 忽略视频流 | 
| -vf | -vf scale=640:640 | 视频滤镜 | 
| -b:v |  | 视频编码 | 
| -vcodec | h264、h265 | 视频编码 | 
| 音频选项 | 参数 | 说明 | 
| -ab | 32、64、96、128 | 音频比特率 | 
| -ac |  | 声道数 | 
| -an |  | 忽略音频流 | 
| -ar | 22050、441000、48000 | 音频采样率 | 
| -af |  | 音频滤镜 | 
| -c:a | libmp3lame | 音频编码 | 
| -q:a | 质量范围为0~6,0最高,6最低 | 音频质量 | 
| -acodec | aac | 音频编码 | 
输入文件名
filelist.txt| 12
 3
 4
 5
 
 | file '1.mkv'file '2.mkv'
 file '3.mkv'
 file '4.mkv'
 file '5.mkv'
 
 | 
 
| 12
 3
 4
 
 | ffmpeg -f concat -i filelist.txt -c copy output.mkv
 # -i filelist.txt(表示外接文件列表)
 # -f concat(表示输出格式跟随输入)
 
 | 
命令案例
编码器详细参数| 1
 | ffmpeg -h encoder=libx264
 | 
 
查看流信息| 12
 3
 4
 5
 
 | ffmpeg -i video.mkvffmpeg -i audio.mp3
 ffmpeg -i cover.png
 
 # -i(导入文件不加任何参数可查看流信息)
 
 | 
 
h264编码| 12
 3
 4
 5
 
 | ffmpeg -y -i input.mp4 -vcodec libx264 -acodec copy output.mp4
 # -y(表示覆盖)
 # -vcodec libx264(选择视频编码器)
 # -acodec copy(音频编码复制)
 
 | 
 
提取帧| 12
 3
 4
 5
 6
 
 | ffmpeg -i input.mp4 –s 1920x1080 -r 25 sequence/%04d.jpg
 # –s 1920x1080(表示导出帧序列的大小)
 # -r 25(表示提取的帧率为25帧)
 # sequence/(表示帧缓存文件夹,需要提前创建)
 # %04d.jpg(%04d 表示按索引排序,四位数补零 0001~9999)
 
 | 
 
39.71 drawtext
https://www.ffmpeg.org/ffmpeg-all.html#toc-drawtext-1
加水印使用ffmpeg命令给视频添加水印| 12
 3
 4
 5
 6
 7
 8
 9
 
 | ffmpeg -i input.mp4 -acodec copy -vf "movie=img.png[watermark];[in][watermark]overlay=20:20" output.mp4
 -vf "drawtext=fontfile=simhei.ttf: text='1':x=30:y=30:fontsize=60:fontcolor=yellow:shadowy=2"
 -vf "drawtext=fontsize=60:text='%{localtime\:%T}'"(效果 00:00:00)
 -vf "drawtext=fontsize=60:text='%{pts\:hms}'"(效果 00:00:00.000)
 -vf "movie=img.png[watermark];[in][watermark]overlay=20:20"
 
 # -vf "movie=img.png...overlay=20:20"(img.png为水印图片,overlay=20:20 是XY坐标。)
 # -vf "drawtext..."(表示绘制文本)
 
 | 
 
视频中生成gif| 12
 3
 4
 5
 
 | ffmpeg -i input.mp4 -t 10 -s 320x240 -pix_fmt rgb24 output.gif
 # -t 10(表示时间)
 # -s 320x240(表示导出帧序列的大小)
 # -pix_fmt rgb24(设置视频编码器使用的图像格式,RGB还是YUV)
 
 | 
 
视频静帧封面ffmpeg 给音频添加封面,ffmpeg对音视频metadata| 12
 3
 4
 5
 6
 7
 8
 9
 
 | ffmpeg -i input.mp4 -i img.png -map 0 -map 1 -c copy -c:v:1 png -disposition:v:1 attached_pic output.mp4
 # -c:v:1 png(指定视频编码器为 png 或 mjpeg)
 # -disposition:v:1 attached_pic(disposition 选择对应的图片)
 
 ffmpeg -i input.mp4 -i img.png -map 0:a -map 1:v output.mp4
 
 # map参数 冒号前:可以用数字定位文件索引
 # map参数 冒号后:可以用数字或a、v则分别表示视频和音频
 
 | 
 
给MP3加专辑封面21.23 mp3| 12
 3
 4
 5
 6
 7
 
 | ffmpeg -i input.mp3 -i cover.png -c copy -map 0 -map 1-metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" out.mp3
 
 #  -c copy(表示编码复制)
 # -map 0(音频文件 input.mp3)
 # -map 1(封面图片 cover.png)
 # -metadata:s:v(给所有视频流添加元数据)
 
 | 
 
视频抽取音频并添加专辑封面| 12
 3
 4
 5
 6
 7
 
 | ffmpeg -i input.mp4 -i cover.png -map 0:a -map 1 -metadata:s:v comment="Cover (Front)" -id3v2_version 3 output.mp3
 # -map 0:a(映射视频文件的音频流)
 # -map 1(映射专辑封面)
 # -metadata:s:v title="Album cover"(给视频流元数据添加标题)
 # -metadata:s:v comment="Cover (Front)"(给视频流元数据添加封面格式)
 # -id3v2_version 3(一定要设置ID3系统,否则缩略图显示不出来)
 
 | 
 
元数据
https://ffmpeg.org/ffmpeg-formats.html#Metadata-1
ffmpeg 给音频添加封面,ffmpeg对音视频metadata 相关操作
https://www.jianshu.com/p/e8e0e47fcccb
添加元数据| 12
 3
 4
 
 | ffmpeg -i input.mkv -vn -metadata title="标题" -metadata artist="作者" output.mp3
 # -vn(表示不对视频进行处理)
 # -metadata title="标题"(表示为元数据添加标签)
 
 | 
 
| 参数(只是部分) | 说明 | 
| title | 标题 | 
| artist | 作者 | 
| album | 专辑 | 
| comment | 评论 | 
| album_artist | 专辑作者 | 
| year | 年 | 
| track | 跟踪编号 | 
| genre | 风格 | 
| composer | 作曲家 | 
| description | 作描述 | 
官方文档
| 命令名称 | 说明 | 
| -h | 打印基本选项 | 
| -h long | 打印更多选项 | 
| -h full | 打印所有选项(包括所有格式和编解码器特定的选项,很长) | 
| -h type=name | 打印指定解码器/编码器/解复用器/复用器/过滤器/bsf/协议的所有选项 | 
有关选项的详细说明,请参见 man ffmpeg。
全局选项影响整个程序而不单单只是文件
| Global options | 说明 | 
| -loglevel loglevel | 设置日志级别 | 
| -v loglevel | 查看日志级别 | 
| -report | 生成报告 | 
| -max_alloc bytes | set maximum size of a single allocated block | 
| -y | 表示覆盖同名文件 | 
| -n | 从不覆盖输出文件 | 
| -ignore_unknown | 忽略未知类型的流 | 
| -filter_threads | number of non-complex filter threads | 
| -filter_complex_threads | number of threads for -filter_complex | 
| -stats | 编码过程打印进度 | 
| -max_error_rate | 解码的最大错误率(0.0:无错误,1.0:100% 错误),超过该比率 ffmpeg 返回错误而不是成功。 | 
| -vol volume | 更改音频音量 (256=正常) | 
| Per-file main options | 说明 | 
| -f fmt | 强制格式 | 
| -c codec | 编码名称 | 
| -codec codec | 编码名称 | 
| -pre preset | 预设名称 | 
| -map_metadata outfile[,metadata]:infile[,metadata] | 从输入文件中设置输出文件的元数据 | 
| -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 | 设置开始时间偏移 | 
| -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 | 添加元数据 | 
| -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 | 
| Video options | 说明 | 
| -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 | 设置帧尺寸 (WxH 或缩写) | 
| -aspect aspect | 设置纵横比 (4:3, 16:9 或 1.3333, 1.7777) | 
| -vn | 禁用视频 | 
| -vcodec codec | force video codec (‘copy’ to copy stream) | 
| -timecode hh:mm:ss[:;.]ff | 设置起始时间码 | 
| -pass n | select the pass number (1 to 3) | 
| -vf filter_graph | 设置视频过滤器 | 
| -ab bitrate | audio bitrate (please use -b:a) | 
| -b bitrate | video bitrate (please use -b:v) | 
| -dn | 禁用数据 | 
| Audio options | 说明 | 
| -aframes number | set the number of audio frames to output | 
| -aq quality | 设置音频质量 (codec-specific) | 
| -ar rate | 设置音频采样率 (in Hz) | 
| -ac channels | 设置音频通道数 | 
| -an | 禁用音频 | 
| -acodec codec | force audio codec (‘copy’ to copy stream) | 
| -vol volume | 更改音量 (256=正常) | 
| -af filter_graph | 设置音频过滤器 | 
| Subtitle options | 说明 | 
| -s size | 设置帧尺寸 (WxH 或缩写) | 
| -sn | 禁用字幕 | 
| -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 | 设置画布大小 (WxH 或缩写) | 
| -spre preset | set the subtitle options to the indicated preset |