FFmpeg使用教程(二)-常用的FFmpeg命令,包含提取音频、剪辑、转码、截屏、加logo...

在上一篇文章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

 

 
13079544-50f92ca196448322.png
转换成功

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 不处理视频 

 

 
13079544-7cc9fe6514c11845.png
提取成功

3. 提取视频
提取纯视频文件。

 

ffmpeg -i D:\media\test.mp4 -vcodec copy -an D:\media\output_提取.mp4
-an 不处理音频

 

 
13079544-4411fdd2f94a9b32.png
 

提取出来的视频总比特率改变了,其他的没有变化。
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
 
13079544-df316c16e0088132.png
 

但是这种格式的命令可能比较慢,推荐下面这种写法:

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

 
13079544-966d67289820d815.png
 

 

ffmpeg -i D:\media\test.mp4 -i D:\media\jianshu-logo.png -filter_complex overlay D:\media\output_加logo.mp4 -y

加上logo后的视频:

 
 
13079544-a5e0a5242349806e.gif
加上logo后

9. 抓取视频的一些帧,存为图片
提取视频的某些帧存储为图片

ffmpeg -i input.mp4 -r 1 -q:v 2 -f image2 pic-%03d.jpeg

-r 表示每一秒几帧
-q:v表示存储jpeg的图像质量,一般2是高质量。
-f:指定文件格式(format),这里指定为image2的格式

 
13079544-bb6018b0201c6d66.png
截取的图片

我们也可以设置要截取开始的时间和想要截取的总时长。

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

原文作者技术博客:https://www.jianshu.com/u/ac4daaeecdfe
95后前端妹子一枚,爱阅读,爱交友,将工作中遇到的问题记录在这里,希望给每一个看到的你能带来一点帮助。
欢迎留言交流

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值