0%

srs中ffmpeg配置转码参数的一些介绍


srs 所有转码参数介绍

srs版本v2

参考来自srs官方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
111
112
113
listen              1935;
vhost __defaultVhost__ {
# the streaming transcode configs.
transcode {
# whether the transcode enabled.
# if off, donot transcode.
# default: off.
enabled on;
# the ffmpeg
ffmpeg ./objs/ffmpeg/bin/ffmpeg;
# the transcode engine for matched stream.
# all matched stream will transcoded to the following stream.
# the transcode set name(ie. hd) is optional and not used.
engine example {
# whether the engine is enabled
# default: off.
enabled on;
# input format, can be:
# off, do not specifies the format, ffmpeg will guess it.
# flv, for flv or RTMP stream.
# other format, for example, mp4/aac whatever.
# default: flv
iformat flv;
# ffmpeg filters, follows the main input.
vfilter {
# the logo input file.
i ./doc/ffmpeg-logo.png;
# the ffmpeg complex filter.
# for filters, @see: http://ffmpeg.org/ffmpeg-filters.html
filter_complex 'overlay=10:10';
}
# video encoder name. can be:
# libx264: use h.264(libx264) video encoder.
# copy: donot encoder the video stream, copy it.
# vn: disable video output.
vcodec libx264;
# video bitrate, in kbps
# @remark 0 to use source video bitrate.
# default: 0
vbitrate 1500;
# video framerate.
# @remark 0 to use source video fps.
# default: 0
vfps 25;
# video width, must be even numbers.
# @remark 0 to use source video width.
# default: 0
vwidth 768;
# video height, must be even numbers.
# @remark 0 to use source video height.
# default: 0
vheight 320;
# the max threads for ffmpeg to used.
# default: 1
vthreads 12;
# x264 profile, @see x264 -help, can be:
# high,main,baseline
vprofile main;
# x264 preset, @see x264 -help, can be:
# ultrafast,superfast,veryfast,faster,fast
# medium,slow,slower,veryslow,placebo
vpreset medium;
# other x264 or ffmpeg video params
vparams {
# ffmpeg options, @see: http://ffmpeg.org/ffmpeg.html
t 100;
# 264 params, @see: http://ffmpeg.org/ffmpeg-codecs.html#libx264
coder 1;
b_strategy 2;
bf 3;
refs 10;
}
# audio encoder name. can be:
# libfdk_aac: use aac(libfdk_aac) audio encoder.
# copy: donot encoder the audio stream, copy it.
# an: disable audio output.
acodec libfdk_aac;
# audio bitrate, in kbps. [16, 72] for libfdk_aac.
# @remark 0 to use source audio bitrate.
# default: 0
abitrate 70;
# audio sample rate. for flv/rtmp, it must be:
# 44100,22050,11025,5512
# @remark 0 to use source audio sample rate.
# default: 0
asample_rate 44100;
# audio channel, 1 for mono, 2 for stereo.
# @remark 0 to use source audio channels.
# default: 0
achannels 2;
# other ffmpeg audio params
aparams {
# audio params, @see: http://ffmpeg.org/ffmpeg-codecs.html#Audio-Encoders
# @remark SRS supported aac profile for HLS is: aac_low, aac_he, aac_he_v2
profile:a aac_low;
bsf:a aac_adtstoasc;
}
# output format, can be:
# off, do not specifies the format, ffmpeg will guess it.
# flv, for flv or RTMP stream.
# other format, for example, mp4/aac whatever.
# default: flv
oformat flv;
# output stream. variables:
# [vhost] the input stream vhost.
# [port] the intput stream port.
# [app] the input stream app.
# [stream] the input stream name.
# [engine] the tanscode engine name.
output rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_[engine];
}
}
}

转码engine配置

一个 transcode 中可以配置多个 engine 来进行转码:

1
2
3
4
5
6
7
8
9
10
11
12
13
vhost www.test.com {
transcode {
engine 360p {
...
}
engine 480p {
...
}
engine 720p {
...
}
}
}

srs 中可使用的转码参数


整体参数

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
# 输入格式,其中flv表示输入格式为flv或者是RTMP。
iformat flv;

# ffmpeg 滤镜
vfilter {
}

# 视频相关参数
vcodec libx264;
vbitrate 1500;
vfps 25;
vwidth 768;
vheight 320;
vthreads 12;
vprofile main;
vpreset medium;
vparams {
t 100;
coder 1;
b_strategy 2;
bf 3;
refs 10;
}

# 音频相关参数
acodec libfdk_aac;
abitrate 70;
asample_rate 44100;
achannels 2;
aparams {
profile:a aac_low;
bsf:a aac_adtstoasc;
}

# 输出格式,其中flv表示输出格式为flv或者是RTMP。
oformat flv;

# 转码后的输出流
output rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_[engine];

视频相关参数

vcodec:

视频编码器名称,通常使用 libx264。

vbitrate:

视频编码所使用的码率,单位kbps。这里所设置的应该是视频的平均码率。

vfps:

视频帧率。

vwidth:

视频宽度。

vheight:

视频高度。

vthreads:

使用多少个线程来进行转码。开启多个线程需要codec(编解码器)支持才行。

vprofile:

可以简单理解为H264的版本,可以选择high,main,baseline。 暂时知道:压缩率 high > main > baseline。 暂时不知道它与画质有没有直接关系。

H264 Profile对比分析

vpreset:

用于调节编码速度和质量的平衡。 可以设置为:ultrafast,superfast,veryfast,faster,fast,medium,slow,slower,veryslow,placebo

vparams:

可以添加一些x264或者视频相关的参数。

  • t:限制从输入文件读取的时长
  • coder:x264使用,熵编码相关参数。
  • b_strategy:x264使用,自适应的添加B帧数目,Use only on first-pass
  • bf:max number of B frames
  • refs:Set reference frames to consider for motion compensation

实际上可以在这个参数中添加各种参数,而这些参数都会被添加在-preset后面。


视频滤镜 vfilter

i:

表示输入,例如加水印时,就需要额外的图片作为输入。

可以有多个输入,并且输入也可以是流的形式,意思是可以支持多路流合并成为一路流。

filter_complex:

也就是FFmpeg的filter_complex,一个filtergraph,支持多个输入流。

使用它可以做各种事情,后面使用举例说明。

详细使用方法可以参考官方文档:4.1 Filtergraph syntax


音频相关参数

acodec:

音频编码器,通常使用 libfdk_aac 。

abitrate:

音频码率,单位kbps,[16, 72] for libfdk_aac

asample_rate:

音频采样率,对于 flv和rtmp,只能设置为 44100,22050,11025,5512。

achannels:

声道,1表示单声道,2表示双声道。

aparams:

可以在里面填一些与音频相关的转码参数,同样的,这些参数会接在achannels(-ac)的后面。

  • profile:a 与h264编码中的profile类似,这里支持 aac_low, aac_he, aac_he_v2 这几种参数的设置。
  • bsf:a 表示Bitstream Filters,详细参考18 Bitstream Filters

转码配置举例


改变视频分辨率

方法一:

使用vwidthvheight参数。

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
vhost www.transcode.com {
transcode {
enabled on;
ffmpeg ./objs/ffmpeg/bin/ffmpeg;
engine 1080_60 {
enabled on;
vfilter {
}
vcodec libx264;
vbitrate 5000;
vfps 60;
vwidth 1920;
vheight 1080;
vthreads 12;
vprofile main;
vpreset fast;
vparams {
}
acodec libfdk_aac;
abitrate 50;
asample_rate 44100;
achannels 2;
aparams {
}
output rtmp://192.168.90.229:2019/persist?vhost=lrm.test.com/long_1080_60;
}
engine 720_60 {
enabled on;
vfilter {
}
vcodec libx264;
vbitrate 3500;
vfps 60;
vwidth 1280;
vheight 720;
vthreads 12;
vprofile main;
vpreset fast;
vparams {
}
acodec libfdk_aac;
abitrate 50;
asample_rate 44100;
achannels 2;
aparams {
}
output rtmp://192.168.90.229:2019/persist?vhost=lrm.test.com/long_720_60;
}
}
}

上面转码将视频转码成1080P+60fps720P+60fps

方法二:

使用filter_complex,在其中使用scale滤镜来缩减分辨率,并将vwidthvheight设置为0。

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
vhost www.transcode.com {
transcode {
enabled on;
ffmpeg ./objs/ffmpeg/bin/ffmpeg;
engine 720_60 {
enabled on;
vfilter {
filter_complex 'scale=1280:-1';
}
vcodec libx264;
vbitrate 3500;
vfps 60;
vwidth 0;
vheight 0;
vthreads 12;
vprofile main;
vpreset fast;
vparams {
}
acodec libfdk_aac;
abitrate 50;
asample_rate 44100;
achannels 2;
aparams {
}
output rtmp://192.168.90.229:2019/persist?vhost=lrm.test.com/long_720_60;
}
}
}

结论:推荐使用方法二。

如果输入视频不是16:9分辨率时,使用方法一转码视频造成画面拉伸,因为它的vwidthvheight参数反映到FFmpeg中是-s vwidth x vheight, 使用-1时则会报错。而使用方法二则可以使用-1来维持原画面比例。


添加水印

官方例子:

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
vhost __defaultVhost__ {
transcode {
enabled on;
ffmpeg ./objs/ffmpeg/bin/ffmpeg;
engine ff {
enabled on;
vfilter {
i ./doc/ffmpeg-logo.png;
filter_complex 'overlay=10:10';
}
vcodec libx264;
vbitrate 300;
vfps 20;
vwidth 768;
vheight 320;
vthreads 2;
vprofile baseline;
vpreset superfast;
vparams {
}
acodec libfdk_aac;
abitrate 45;
asample_rate 44100;
achannels 2;
aparams {
}
output rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_[engine];
}
}
}

实现方法,使用vfilter,输入图片并使用滤镜overlay来完成。


CBR恒定编码实现

先来看一下使用bitrate来控制码率的情况:

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
vhost lrm.test.com {
transcode {
enabled on;
ffmpeg ./objs/ffmpeg/bin/ffmpeg;
engine ff {
enabled on;
vfilter {
i ./doc/ffmpeg-logo.png;
filter_complex '[1][0]scale2ref=w=iw/8:h=ow/mdar[scaled][0-out]; [0-out][scaled]overlay=x=main_w/10-w/2:y=main_h/10-h/2[over]; [over]scale=1280:-1';
}
vcodec libx264;
vbitrate 3500;
vfps 60;
vwidth 0;
vheight 0;
vthreads 12;
vprofile main;
vpreset fast;
vparams {
}
acodec libfdk_aac;
abitrate 50;
asample_rate 44100;
achannels 2;
aparams {
}
output rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_[engine];
}
}
}

这里将vbitrate设置在3500,也就是我们希望码率可以控制在3500kbps,使用VLC播放器可以看到大概的统计效果:

image

参数中虽然可以使用bitrate来指定码率,但是实际上这只是平均码率,但是实际码率还是会上下波动,并且范围也不小

可以使用下面方法来实现CBR,也是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
vhost lrm.test.com {
transcode {
enabled on;
ffmpeg ./objs/ffmpeg/bin/ffmpeg;
engine ff {
enabled on;
vfilter {
i ./doc/ffmpeg-logo.png;
filter_complex '[1][0]scale2ref=w=iw/8:h=ow/mdar[scaled][0-out]; [0-out][scaled]overlay=x=main_w/10-w/2:y=main_h/10-h/2[over]; [over]scale=1280:-1';
}
vcodec libx264;
vbitrate 3500;
vfps 60;
vwidth 0;
vheight 0;
vthreads 12;
vprofile main;
vpreset fast;
vparams {
maxrate:v 3500k;
minrate:v 3500k;
bufsize:v 3500k;
}
acodec libfdk_aac;
abitrate 50;
asample_rate 44100;
achannels 2;
aparams {
}
output rtmp://127.0.0.1:[port]/[app]?vhost=[vhost]/[stream]_[engine];
}
}
}

可以看到上面配置中,增加设置maxrateminratebufsize来配合控制视频的码率。

image

实际效果并不能固定码率,码率仍然在上下波动,但很明显能达到更小的波动范围。