0%

视音频技术-FFmpeg命令行基础

1、FFmpeg命令行基础

FFmpeg官网:

1
http://ffmpeg.org/

FFmpeg程序下载:包括Windows、MacOS、Linux等操作系统,包括FFmpeg、FFprobe、FFplay、FFserver等程序。

1
http://ffmpeg.org/download.html

FFmpeg命令行文档:

1
http://ffmpeg.org/ffmpeg.html

FFplay命令行文档:

1
http://ffmpeg.org/ffplay.html

2、配置FFmpeg环境变量

MacOS上配置FFmpeg环境变量:

1
vi ~/.bash_profile

按I键编辑文本:

1
export PATH=${PATH}:/Users/zhangchaozhou/Documents/Software/ffmpeg-4.4

按ESC停止编辑,并按:wq键保存退出编辑。
更新配置:

1
source ~/.bash_profile

如果环境变量PATH被修改,需在命令行执行:

1
export PATH=/bin:/usr/bin:$PATH

执行FFmpeg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
TeochewZhang:video zhangchaozhou$ ffmpeg -hide_banner -help
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

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/protocol
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 decoding 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)
-fpsmax rate set max 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

3、常用命令

3.1、获取文件信息

使用-i参数。

1
$ ffmpeg -i input.mp4

加上-hide_banner参数,可以只显示元信息,去除冗余信息。

1
$ ffmpeg -i input.mp4 -hide_banner

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
TeochewZhang:video zhangchaozhou$ ffmpeg -i 那些年,我们一起追的女孩.mp4 -hide_banner
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '那些年,我们一起追的女孩.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.19.100
Duration: 00:00:30.32, start: 0.000000, bitrate: 397 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x208 [SAR 16:15 DAR 32:13], 261 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]

3.2、转换编码格式/transcoding

将视频文件从一种编码转成另一种编码。比如转成 H.264 编码,一般使用编码器libx264,所以只需指定输出文件的视频编码器即可。

1
$ ffmpeg -i [input.file] -c:v libx264 output.mp4

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
TeochewZhang:video zhangchaozhou$ ffmpeg -i Forrest_Gump_IMAX.mp4 -c:v libx264 output.mp4 -hide_banner
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Forrest_Gump_IMAX.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf56.19.100
Duration: 00:00:31.21, start: 0.000000, bitrate: 878 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x352, 748 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x7fcc4e803e00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x7fcc4e803e00] profile High, level 3.0, 4:2:0, 8-bit
[libx264 @ 0x7fcc4e803e00] 264 - core 161 r3048 b86ae3c - H.264/MPEG-4 AVC codec - Copyleft 2003-2021 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=11 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=23 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'output.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.76.100
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p(progressive), 640x352, q=2-31, 23.98 fps, 24k tbn (default)
Metadata:
handler_name : VideoHandler
vendor_id : [0][0][0][0]
encoder : Lavc58.134.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
vendor_id : [0][0][0][0]
encoder : Lavc58.134.100 aac
frame= 744 fps=297 q=-1.0 Lsize= 3080kB time=00:00:31.18 bitrate= 809.1kbits/s speed=12.4x
video:2595kB audio:460kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.800888%
[libx264 @ 0x7fcc4e803e00] frame I:20 Avg QP:17.34 size: 24048
[libx264 @ 0x7fcc4e803e00] frame P:245 Avg QP:22.46 size: 6089
[libx264 @ 0x7fcc4e803e00] frame B:479 Avg QP:24.79 size: 1428
[libx264 @ 0x7fcc4e803e00] consecutive B-frames: 7.0% 18.8% 8.1% 66.1%
[libx264 @ 0x7fcc4e803e00] mb I I16..4: 34.0% 22.0% 44.0%
[libx264 @ 0x7fcc4e803e00] mb P I16..4: 5.4% 8.7% 3.1% P16..4: 30.5% 12.4% 7.2% 0.0% 0.0% skip:32.7%
[libx264 @ 0x7fcc4e803e00] mb B I16..4: 0.7% 0.6% 0.4% B16..8: 30.4% 4.7% 1.6% direct: 2.1% skip:59.5% L0:45.2% L1:43.1% BI:11.7%
[libx264 @ 0x7fcc4e803e00] 8x8 transform intra:40.6% inter:46.7%
[libx264 @ 0x7fcc4e803e00] coded y,uvDC,uvAC intra: 45.5% 49.2% 16.2% inter: 12.0% 10.6% 0.5%
[libx264 @ 0x7fcc4e803e00] i16 v,h,dc,p: 39% 35% 13% 13%
[libx264 @ 0x7fcc4e803e00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 15% 34% 20% 3% 5% 5% 6% 4% 6%
[libx264 @ 0x7fcc4e803e00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 15% 30% 13% 5% 7% 7% 8% 6% 8%
[libx264 @ 0x7fcc4e803e00] i8c dc,h,v,p: 53% 31% 11% 5%
[libx264 @ 0x7fcc4e803e00] Weighted P-Frames: Y:24.9% UV:20.4%
[libx264 @ 0x7fcc4e803e00] ref P L0: 62.2% 20.9% 13.6% 3.0% 0.3%
[libx264 @ 0x7fcc4e803e00] ref B L0: 89.2% 9.0% 1.7%
[libx264 @ 0x7fcc4e803e00] ref B L1: 98.3% 1.7%
[libx264 @ 0x7fcc4e803e00] kb/s:684.96
[aac @ 0x7fcc4e805600] Qavg: 4577.948

3.3、转换容器格式/transmuxing

将视频文件从一种容器转到另一种容器。
内部的编码格式不变,使用-c copy指定直接拷贝,不经过转码。

1
$ ffmpeg -i input.mp4 -c copy output.webm

3.4、调整码率/transrating

改变编码的比特率,一般用来将视频文件的体积变小。
指定码率最小为964K,最大为3856K,缓冲区大小为 2000K。

1
2
3
4
$ ffmpeg \
-i input.mp4 \
-minrate 964K -maxrate 3856K -bufsize 2000K \
output.mp4

3.5、改变分辨率/transsizing

从 1080p 转为 480p 。

1
2
3
4
$ ffmpeg \
-i input.mp4 \
-vf scale=480:-1 \
output.mp4

3.6、提取音频/demuxing

从视频里面提取音频,-vn表示去掉视频,-c:a copy表示不改变音频编码,直接拷贝。

1
2
3
4
$ ffmpeg \
-i input.mp4 \
-vn -c:a copy \
output.aac

3.7、添加音轨/muxing

将外部音频加入视频,比如添加背景音乐或旁白。

1
2
3
$ ffmpeg \
-i input.aac -i input.mp4 \
output.mp4

3.8、截图

从指定时间开始,连续对1秒钟的视频进行截图。

1
2
3
4
5
$ ffmpeg \
-y \
-i input.mp4 \
-ss 00:01:24 -t 00:00:01 \
output_%3d.jpg

指定只截取一,-vframes 1指定只截取一帧,-q:v 2表示输出的图片质量,一般是1到5之间(1 为质量最高)。

1
2
3
4
5
$ ffmpeg \
-ss 01:23:45 \
-i input \
-vframes 1 -q:v 2 \
output.jpg

3.9、裁剪/cutting

截取原始视频里面的一个片段,输出为一个新视频。可以指定开始时间(start)和持续时间(duration),也可以指定结束时间(end)。
-c copy表示不改变音频和视频的编码格式,直接拷贝。

1
2
3

$ ffmpeg -ss [start] -i [input] -t [duration] -c copy [output]
$ ffmpeg -ss [start] -i [input] -to [end] -c copy [output]
1
2
$ ffmpeg -ss 00:01:50 -i [input] -t 10.5 -c copy [output]
$ ffmpeg -ss 2.5 -i [input] -to 10 -c copy [output]

3.10、为音频添加封面

有些视频网站只允许上传视频文件。如果要上传音频文件,必须为音频添加封面,将其转为视频,然后上传。
将音频文件,转为带封面的视频文件。

-loop 1参数表示图片无限循环,-shortest参数表示音频文件结束,输出视频就结束。

1
2
3
4
5
6

$ ffmpeg \
-loop 1 \
-i cover.jpg -i input.mp3 \
-c:v libx264 -c:a aac -b:a 192k -shortest \
output.mp4