目录
目录
libavcodec
提供更广泛的编解码器的实现。libavformat
实现流协议,容器格式和基本I / O访问。libavutil
包括垫圈,解压缩器和其他实用功能。libavfilter
提供通过过滤器链改变解码的音频和视频的方法。libavdevice
提供访问捕获和回放设备的抽象。libavresample
实现音频混合和重采样例程。libswscale
实现颜色转换和缩放例程。
1.官方宣传
一个完整的跨平台解决方案,用于录制,转换和流式传输音频和视频。FFmpeg是领先的多媒体框架,能够解码,编码, 转码,复用,解复用,流式传输,过滤和播放人类和机器创建的任何内容。它支持最晦涩难懂的古代格式,直至最前沿。无论它们是由某些标准委员会,社区还是公司设计的。它还具有高度可移植性:FFmpeg 在各种构建环境,机器架构和配置下,跨Linux,Mac OS X,Microsoft Windows,BSD,Solaris等编译,运行和传递我们的测试基础架构 FATE。
2.下载与安装
2.1 下载
官网:https://ffmpeg.zeranoe.com/builds/
- FFmpeg分为3个版本:
Static
、Shared
、Dev
- 前两个版本可以直接在命令行中使用。包含了三个
exe
:ffmpeg.exe
,ffplay.exe
,ffprobe.exe
Static
版本中的exe
体积较大,那是因为相关的Dll
都已经编译进exe
里面去了。Shared
版本中exe
的体积相对小很多,是因为它们运行的时候还需要到相关的dll中调用相应的功能Dev
版本用于开发,里面包含了库文件xxx.lib
以及头文件xxx
本博主下载的是 ffmpeg-20181028-bdfd2e3-win64-static
2.2 安装
他没有安装程序,我们需要把包含exe的那个文件夹放入环境变量,重启电脑即可。
3.用法
3.1 ...\ffmpeg\bin 下三个exe的解释
借由雷神的 [总结]FFMPEG视音频编解码零基础学习方法_雷霄骅(leixiaohua1020)的专栏-CSDN博客_ffmpeg雷霄骅,我们可以了解ffmpeg.exe、ffplay.exe和ffprobe.exe 的用法
3.1.1 ffmpeg.exe
ffmpeg是用于转码的应用程序。
一个简单的转码命令可以这样写:
将input.avi转码成output.ts,并设置视频的码率为640kbps
ffmpeg -i input.avi -b:v 640k output.ts
具体的使用方法可以参考: ffmpeg参数中文详细解释
详细的使用说明(英文):http://ffmpeg.org/ffmpeg.html
3.1.2 ffplay.exe
ffplay是用于播放的应用程序。
一个简单的播放命令可以这样写:
播放test.avi
ffplay test.avi
具体的使用方法可以参考:ffplay的快捷键以及选项
详细的使用说明(英文):http://ffmpeg.org/ffplay.html
3.1.3 ffprobe.exe
ffprobe是用于查看文件格式的应用程序。
这个就不多介绍了。
详细的使用说明(英文):http://ffmpeg.org/ffprobe.html
3.2 ffmpeg 命令行
在cmd中 ffmpeg.exe -h 查看ffmpeg说明,可以看到:
ffmpeg -<options for input 1> -i input 1 -<options for input 2> -i input 2 -<options for output 1> output1 -<options for output 2> output2
Getting help:
-h -- print basic options
-h long -- print more options
-h full -- print all options (including all format and codec specific options, very long)
-h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf
See man ffmpeg for detailed description of the options.
Print help / information / capabilities:
-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
-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
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 maximum error rate ratio of 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)
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
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
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
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
enmmm,先分析一下表头:
- 打印帮助/信息/功能:
- 全局选项(影响整个程序而不仅仅是一个文件:
- 视频选项:
- 字幕选项:
3.2.1 常用后缀记录
-i 设定输入档名。
-f 设定输出格式。
-y 若输出档案已存在时则覆盖档案。
-fs 超过指定的档案大小时则结束转换。
-ss 从指定时间开始转换。
-title 设定标题。
-timestamp 设定时间戳。
-vsync 增减Frame使影音同步。
影像参数:
-b 设定影像流量,默认为200Kbit/秒。( 单位请参照下方注意事项 )
-r 设定FrameRate值,默认为25。
-s 设定画面的宽与高。
-aspect 设定画面的比例。
-vn 不处理影像,于仅针对声音做处理时使用。
-vcodec 设定影像影像编解码器,未设定时则使用与输入档案相同之编解码器。
4.思考:接下来我们该做什么?
博主从来没有见过-h出来这么多命令的exe,这简直就是在劝退所有初学者......ok,我先摸索摸索路子,船到桥头自然直嘛。记录一下,我找到一个中文版的教程ffmpeg的中文文档,看了一下,其实就是翻译,不过没乱码~感谢感谢,还有一个新一点的http://wiki.11ten.net/Resources/ffmpeg-%E4%B8%AD%E6%96%87%E6%96%87%E6%A1%A3.html。
接下来我们慢慢剖析,没事,博主头铁。
从《FFmpeg Basics》中文版-01-FFmpeg基本介绍 - 简书,我们看到,ffmpeg的基本用法是这样子的,喵喵喵??
5.基础知识
5.1比特率
英文为 bit rate,描述每秒钟输出多少 KB 的参数,单位是 Kbps,也就是 kbit/s,8Kbit/s = 1KB/s。也就是说 800Kbps 意思就是每秒视频就要占用 100KB 磁盘空间。对于音频文件也存在比特率,同理。压缩同一个视频,视频比特率越大,文件体积越大。视频比特率越大,画质越好,马赛克越少。
bitrate 可以理解为 file size / duration
比如一个视频文件 20.8 M 时长为 1min,那么
bitrate = 20.8M bit/60s = 20.8 * 1024 * 1024 * 8 bit/60s = 2831 Kbps
假设音频的码率为固定 128 Kbps,那么视频的码率就是
2831 Kbps - 128 Kbps = 2703 Kbps
H.264 标准建议
视频大小 | 分辨率 | 建议码率 |
---|---|---|
480P | 720X480 | 1800Kbps |
720P | 1280X720 | 3500Kbps |
1080P | 1920X1080 | 8500Kbps |
5.2帧数
帧数,又被叫做帧率,指的是每秒钟播放的图片数,单位 fps(英文:Frames Per Second),每秒的帧数或者帧率表示视频文件或者图形处理器场景时每秒钟能够更新的次数。
5.3分辨率
最好理解的概念了,表示画面的大小,单位是像素 px。和编码率的关系:越高的分辨率,需要越高的编码率,因为图像的细节多了,需要的文件体积也应该增大,否则还不如画面小一些,你会发现同一码率,画面越大,图像的马赛克程度越明显。
5.4采样率
每秒钟对音频信号的采样次数,采样频率越高声音还原度越高,声音更加自然。单位是赫兹 Hz。音频文件一般使用的采样率是 44100 Hz ,也就是一秒钟采样 44100 次,之所以使用这个数值是因为经过了反复实验,人们发现这个采样精度最合适,低于这个值就会有较明显的损失,而高于这个值人的耳朵已经很难分辨,而且增大了数字音频所占用的空间。我们所使用的 CD 的采样标准就是 44.1k,目前 44.1k 还是一个最通行的标准。
6.常用用法
6.0显示可以使用的编码器
ffmpeg -encoders
6.1显示文件信息
显示视频信息
ffmpeg -i input.avi
6.2将视频拆分图片 批量截图
将视频拆分多张图片,每一帧图片,保存到 frames 文件夹下,命名 frame001.png 这种。可以加上 -r 参数以用来限制每秒的帧数,-r 10
就表示每秒 10 帧。-start_number 0 表示从0开始编号
ffmpeg -i input.mp4 -start_number 0 frames/frame%03d.png
6.3图片合成视频
将多张图片合成视频
ffmpeg -y -r 10 -i \test\%05d.jpg -absf aac_adtstoasc output.mp4
-y 遇到同名的文件覆盖
ffmpeg -i frames/frame%3d.png output.mp4
使用上面命令有可能本地打不开,原因是没有设置pix_fmt。
ffmpeg -i 4th/overlay/%05d.png -pix_fmt yuv420p -c:v libx264 4th.mp4 -y
6.4从视频中提取音频
从视频文件中提取音频并保存为 mp3,
ffmpeg -i input.mp4 -f mp3 output.mp3
-f 是指定的格式
如果需要可以在中间加上 -ar 44100 -ac 2 -ab 192
系数,表示采样率 44100 ,通道 2 立体声,码率 192 kb/s,注意会议对视频的option 在哪里调节。
6.5将声音合成到视频
将声音合成到视频中
ffmpeg -i input_music.mp3 -i input_video.mp4 output.mp4
6.6转化格式
格式之间转换 大部分的情况下直接运行一下即可
ffmpeg -i input.mp4 output.avi
这是一条最简单的ffmpeg命令,可以看到,ffmpeg通过-i参数将input.mp4作为输入源输入,然后进行了转码与转封装操作,输出到output.avi中。这条命令最主要做了如下工作:
1. 获得输入源input.mp4
2. 输出文件output.avi
看似简单的两步主要的工作,其实远远不止是从后缀名为mp4的文件输出后缀名为avi的文件,因为 在ffmpeg中,mp4与avi是两种流媒体格式,并不是后缀名所可以决定的,例如上面上的命令行同样 可以这样写:
ffmpeg -i input.mp4 -f avi output.dat
这条ffmpeg命令相对前面的那条命令做了一些改变,加了一个“-f”进行约束,“-f”参数的工作 非常重要,它制定了输出的文件的容器格式,所以可以看到输出的文件为output.dat,文件后缀 为.dat,但是其主要工作依然与之前的指令相同
将 flv 转码 MP4
ffmpeg -i input.flv -vcodec copy -acodec copy out.mp4
-vcodec copy
和 -acodec copy
表示所使用的视频和音频编码格式,为原样拷贝。
转换文件格式
ffmpeg -y -i input_video.mp4 -bitexact -vcodec h263 -b 128 -r 15 -s 176x144 -acodec aac -ac 2 -ar 22500 -ab 24 -f 3gp test.3gp
或
ffmpeg -y -i test.wmv -ac 1 -acodec libamr_nb -ar 8000 -ab 12200 -s 176x144 -b 128 -r 15 test.3gp
-b 设置比特率
-r 设置帧速率(Hz值,分数或缩写),就是每秒几张
-s 字幕选项:设置框架大小(WxH或缩写)
-vcodec 改变视频编码格式,如果是copy就是不变(和input一样)
6.7视频切片操作
对视频切片操作
比如需要从视频第 1 分 45 秒地方,剪 10 秒画面,-ss 表示开始位置,-t 表示延长时间
ffmpeg -i input.mp4 -ss 00:01:45 -t 10 output.mp4
6.8加速减速视频
加速视频
ffmpeg -i input.mp4 -vf “setpts=0.5*PTS” output.mp4
同理减速视频
ffmpeg -i input.mp4 -vf “setpts=2.0*PTS” output.mp4
此操作对音频无影响
- vf 设置视频过滤器
6.9视频截图
视频 10 秒的地方 (-ss
参数)截取一张 1920x1080 尺寸大小的,格式为 jpg 的图片 -ss
后跟的时间单位为秒
ffmpeg -i input_video.mp4 -y -f image2 -t 0.001 -ss 10 -s 1920x1080 output.jpg
或者
ffmpeg -i input_video.mp4 -ss 00:00:06.000 -vframes 1 output.png
6.10合成 gif
把视频的前 30 帧转换成一个 Gif
ffmpeg -i input_video.mp4 -vframes 30 -y -f gif output.gif
将视频转成 gif
ffmpeg -ss 00:00:00.000 -i input.mp4 -pix_fmt rgb24 -r 10 -s 320x240 -t 00:00:10.000 output.gif
将输入的文件从 (-ss) 设定的时间开始以 10 帧频率,输出到 320x240 大小的 gif 中,时间长度为 -t 设定的参数。通过这样转换出来的 gif 一般都比较大,可以使用 ImageMagick 来优化图片的大小。
convert -layers Optimize output.gif output_optimized.gif
把 frame.[001-100].jpg 序列帧和 bg.mp3 音频文件利用 mpeg4 编码方式合成分辨率 720p 的视频文件 output.avi:
ffmpeg -i bg.mp3 -i frame.%3d.jpg -s hd720 -vcodec mpeg4 output.avi
6.11转换码率
ffmpeg 码率相关的参数主要有 -minrate
, maxrate
, -b:v
ffmpeg -i input.mp4 -b:v 2000k output.mp4
也就是把原始视频转换成 2 Mbps 码率视频。ffmpeg 官方建议,在设置 -b:v
时,同时加上 -bufsize
用于设置码率控制缓冲器大小,让整体码率更加趋近于希望的只,减少波动。
ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k output.mp4
而 -minrate
和 -maxrate
比较简单,设置码率不要低于或者超过某一个阈值
ffmpeg -i input.mp4 -b:v 2000k -bufsize 2000k -maxrate 2500k output.mp4
6.12查看 ffmpeg 支持格式
要查看你的 ffmpeg 支持哪些格式,可以用如下命令:
ffmpeg -formats | less
设置输出文件编码率 64 kbit/s, To set the video bitrate of the output file to 64 kbit/s:
ffmpeg -i input.avi -b:v 64k -bufsize 64k output.avi
设置输出文件帧率为 24 fps,To force the frame rate of the output file to 24 fps:
ffmpeg -i input.avi -r 24 output.avi
强制输入文件以 1 帧,输出文件 24 帧 , To force the frame rate of the input file (valid for raw formats only) to 1 fps and the frame rate of the output file to 24 fps:
ffmpeg -r 1 -i input.mp4 -r 24 output.avi
下面几步分别是,创建frames
文件夹,利用 ffmpeg 将视频文件以每秒 10 帧输出成图像保存到 frames 文件夹中,再利用 ImageMagick 将图片组成 gif。其中 convert
命令来自 ImageMagick。
mkdir frames
ffmpeg -i input.mp4 -r 10 frames/frame%03d.png
convert -delay 5 -loop 0 frames/frame*.png output.gif
Source: How do I convert a video to GIF using ffmpeg, with reasonable quality? - Super User
6.13利用 ffmpeg 屏幕录制
6.14添加水印
ffmpeg -i input.mp4 -i picture.png -filter_complex overlay="(main_w/2)-(overlay_w/2):(main_h/2)-(overlay_h)/2" output.mp4
picture.png 为水印图片, overlay 为水印位置
6.15 添加字幕
然后嵌入视频
# 给mkv文件添加srt格式的软字幕:
ffmpeg -i input.mkv -i input.srt -c copy -c:s srt output.mkv
# 给mp4文件添加srt格式的软字幕(mkv格式本身支持字幕轨道,但mp4需要使用mov_text)
ffmpeg -i input.mp4 -i input.srt -c copy -c:s mov_text output.mp4
# 给mkv文件添加ass软字幕(mp4不支持ass编码格式的字幕流,mkv支持)
ffmpeg -i input.mkv -i input.ass -c copy -c:s ass output.mkv
7.查询
7.1 查某个编码器/解码器信息
ffmpeg -h encoder=hevc_nvenc
ffmpeg -h decoder=hevc
7.2 查询所有编解码器信息(codec:编解码器)
ffmpeg -codecs
7.3 从编解码器中找关键词
ffmpeg -codecs|grep nvenc
参考
2.[总结]FFMPEG视音频编解码零基础学习方法_雷霄骅(leixiaohua1020)的专栏-CSDN博客_ffmpeg雷霄骅