在上一篇文章 FFMPEG 使用教程(一)-Windows 安装配置 FFMPEG
配置了环境变量后, 输入 FFMPEG -h 可打印帮助文档, 如下:
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation. 保留所有权利.
- C:\Users\admin>FFMPEG -h
- FFMPEG version N-68211-g4a0b1d9 Copyright (c) 2000-2014 the FFMPEG developers
- built on Dec 4 2014 22:12:51 with gcc 4.9.2 (GCC)
- configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
- isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
- le-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --
- enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-lib
- modplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrw
- b --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinge
- r --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --en
- able-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis
- --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-
- libx265 --enable-libxavs --enable-libxvid --enable-decklink --enable-zlib
- libavutil 54. 15.100 / 54. 15.100
- libavcodec 56. 14.100 / 56. 14.100
- libavformat 56. 15.102 / 56. 15.102
- libavdevice 56. 3.100 / 56. 3.100
- libavfilter 5. 2.103 / 5. 2.103
- libswscale 3. 1.101 / 3. 1.101
- libswresample 1. 1.100 / 1. 1.100
- libpostproc 53. 3.100 / 53. 3.100
- Hyper fast Audio and Video encoder
- usage: FFMPEG [options] [[infile options] -i infile]... {[outfile options] outfi
- le}...
- Getting help:
- -h -- print basic options
- -h long -- print more options
- -h full -- print all options (including all format and codec specific option
- s, very long)
- 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
- -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
- 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
- -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)
- 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
- -timestamp time set the recording timestamp ('now' to set the current time)
- -metadata string=string add metadata
- -target type specify target file type ("vcd", "svcd", "dvd", "dv", "dv50"
- , "pal-vcd", "ntsc-svcd", ...)
- -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
- 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
常用命令
主要参数:
-i 设定输入流
-f 设定输出格式
-ss 开始时间
视频参数:
-r 设定帧速率, 默认为 25
-s 设定画面的宽与高
-aspect 设定画面的比例
-vn 不处理视频
-vcodec 设定视频编解码器, 未设定时则使用与输入流相同的编解码器
音频参数:
-ac 设定声音的 Channel 数
-acodec 设定声音编解码器, 未设定时则使用与输入流相同的编解码器
-an 不处理音频
1. 视频格式转换
比如一个 mp4 文件, 想转为 avi 格式.
FFMPEG -i D:\media\test.mp4 D:\media\output_test.avi
转换成功
2. 提取音频
提取视频里的音频.
FFMPEG -i D:\media\test.mp4 -acodec copy -vn D:\media\output.aac
上面的命令, 默认 mp4 的 audio codec 是 aac, 将任何格式的视频, 都转为最常见的 aac.
FFMPEG -i D:\media\test.mp4 -acodec aac -vn D:\media\output.aac
-vn 不处理视频
提取成功
3. 提取视频
提取纯视频文件.
FFMPEG -i D:\media\test.mp4 -vcodec copy -an D:\media\output_提取. mp4
-an 不处理音频
提取出来的视频总比特率改变了, 其他的没有变化.
4. 视频剪切
如果我们想从几个 G 的视频里剪切某一片段, 我们可以剪切, 从时间为 00:00:02 开始, 截取 3 秒钟的视频.
FFMPEG -i D:\media\test.mp4 -vcodec copy -acodec copy -ss 00:00:02 -t 00:00:03 D:\media\output_剪切. mp4 -y
-ss 设置从视频的哪个时间点开始截取, 上文从视频的第 2s 开始截取.
-t 表示截取多长的时间, 上文截取的视频共 3s.
-vcodec copy 表示使用跟原视频一样的视频编解码器.
-acodec copy 表示使用跟原视频一样的音频编解码器.
-i 表示源视频文件
-y 表示如果输出文件已存在则覆盖.
-to 截到视频的哪个时间点结束. 如果上面的命令将 - t 换成 - to 就是:
FFMPEG -i D:\media\test.mp4 -vcodec copy -acodec copy -ss 00:00:02 -to 00:00:05 D:\media\output_剪切 1.mp4 -y
但是这种格式的命令可能比较慢, 推荐下面这种写法:
FFMPEG -ss 00:00:02 -i D:\media\test.mp4 -t 3 -c:v copy -c:a copy D:\media\output_剪切 2.mp4
把 - ss 00:00:02 放到 - i 前面, 与原来的区别是, 这样会先跳转到第 2 秒在开始解码输入视频, 而原来的会从开始解码, 只是丢弃掉前 2 秒的结果.
-c:v 和 -c:a 分别指定视频和音频的编码格式.
-c:v copy -c:a copy 标示视频与音频的编码不发生改变, 而是直接复制, 这样会大大提升速度.
注意一个问题, FFMPEG 在切割视频的时候无法做到时间绝对准确, 因为视频编码中关键帧 (I 帧) 和跟随它的 B 帧, P 帧是无法分割开的, 否则就需要进行重新帧内编码, 会让视频体积增大. 所以, 如果切割的位置刚好在两个关键帧中间, 那么 FFMPEG 会向前 / 向后切割, 所以最后切割出的 chunk 长度总是会大于等于应有的长度.
5. 码率控制
码率控制对于在线视频比较重要. 因为在线视频需要考虑其能提供的带宽, 在压缩视频时, 经常需要控制码率.
那么, 什么是码率? 很简单: bitrate = file size / duration
比如一个文件 20.8M, 时长 1 分钟, 那么, 码率就是:
biterate = 20.8M bit/60s = 20.8*1024*1024*8 bit/60s= 2831Kbps
一般音频的码率只有固定几种, 比如是 128Kbps, 那么, video 的就是
video biterate = 2831Kbps -128Kbps = 2703Kbps.
那么 FFMPEG 如何控制码率的呢? ffmpg 控制码率有 3 种选择:-minrate -b:v -maxrate
-b:v 主要是控制平均码率. 比如一个视频源的码率太高了, 有 10Mbps, 文件太大, 想把文件弄小一点, 但是又不破坏分辨率. FFMPEG -i input.mp4 -b:v 2000k output.mp4 上面把码率从原码率转成 2Mbps 码率, 这样其实也间接让文件变小了.
不过, FFMPEG 官方 wiki 比较建议, 设置 b:v 时, 同时加上 -bufsize
-bufsize 用于设置码率控制缓冲器的大小, 设置的好处是, 让整体的码率更趋近于希望的值, 减少波动.(简单来说, 比如 1 2 的平均值是 1.5, 1.49 1.51 也是 1.5, 当然是第二种比较好)
FFMPEG -i input.mp4 -b:v 2000k -bufsize 2000k output.mp4
-minrate -maxrate 就简单了, 在线视频有时候, 希望码率波动, 不要超过一个阈值, 可以设置 maxrate.
FFMPEG -i input.mp4 -b:v 2000k -bufsize 2000k -maxrate 2500k output.mp4
6. 视频编码格式转换
比如一个视频的编码是 MPEG4, 想用 H264 编码, 我们可以这样:
FFMPEG -i input.mp4 -vcodec h264 output.mp4
相反也一样
FFMPEG -i input.mp4 -vcodec mpeg4 output.mp4
7. 将分辨率为 1920x1080 的视频缩小为 960x540 输出
FFMPEG -i D:\media\test.mp4 -vf scale=960:540 D:\media\output_改变帧宽度. mp4
如果 540 不写, 写成 - 1, 即 scale=960:-1, 那也是可以的, FFMPEG 会通知缩放滤镜在输出时保持原始的宽高比.
帧宽度变小的同时视频文件也会变小.
8. 为视频添加 logo
为视频加上下图的 logo
FFMPEG -i D:\media\test.mp4 -i D:\media\jianshu-logo.PNG -filter_complex overlay D:\media\output_加 logo.mp4 -y
加上 logo 后的视频:
加上 logo 后
9. 抓取视频的一些帧, 存为图片
提取视频的某些帧存储为图片
FFMPEG -i input.mp4 -r 1 -q:v 2 -f image2 pic-%03d.jpeg
-r 表示每一秒几帧
-q:v 表示存储 jpeg 的图像质量, 一般 2 是高质量.
-f: 指定文件格式(format), 这里指定为 image2 的格式
截取的图片
我们也可以设置要截取开始的时间和想要截取的总时长.
FFMPEG -i D:\media\test.mp4 -ss 00:00:20 -t 3 -r 1 -q:v 2 -f image2 D:\media\pic-%03d.jpeg
-ss 表示开始时间
-t 表示共要多少时间.
上面的命令 FFMPEG 会从 test.mp4 的第 20s 时间开始, 往下 3s, 即 20~23s 这 3 秒钟之间, 每隔 1s 就抓一帧, 总共会抓 3 帧.
10. 把视频的前 30 帧转换成一个 Animated Gif 动态图
FFMPEG -i D:\media\test.mp4 -vframes 30 -y -f gif D:\media \ 前 30 帧. gif
来源: http://www.jianshu.com/p/7987505abd35