3 Live Streaming Transcode

SRS可以对推送到SRS的RTMP流进行转码,然后输出到RTMP服务器(也可以是SRS自己)。

Use Scenario

FFMPEG的重要应用场景包括:

  • 推送一路高码率,转多路输出。譬如:游戏直播中,推送一路1080p流到SRS,SRS可以转码输出1080p/720p/576p多路,低码率可以给移动设备观看。这样节省了推流带宽(一般源站为BGP带宽,很贵),也减轻了客户端压力(譬如客户端边玩游戏边直播)。
  • 支持多屏输出。譬如:网页推流(主播)编码为vp6/mp3或speex,推流到SRS后无法支持HLS(要求h264+aac),可以转码成h264+aac后切片成HLS或者推送到其他服务器再分发。
  • 加水印。适用于需要对流进行加水印的情况,譬如打上自己的logo。SRS支持文字水印和图片水印,也可以支持视频作为水印,或者将两路流叠加(参考ffmpeg的用法)。
  • 截图:参考使用Transcoder截图
  • 其他滤镜:SRS支持所有ffmpeg的滤镜。

Workflow

SRS转码的主要流程包括:

  1. 编码器推送RTMP流到SRS的vhost。
  2. SRS的vhost若配置了转码,则进行转码。
  3. 转码后,按照配置,推送到SRS本身或者其他RTMP服务器。

Transcode Config

SRS可以对vhost的所有的流转码,或者对某些app的流转码,或者对某些流转码。

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.
# we will build the parameters to fork ffmpeg:
# ffmpeg <perfile>
# -i <iformat>
# <vfilter>
# -vcodec <vcodec> -b:v <vbitrate> -r <vfps> -s <vwidth>x<vheight> -profile:v <vprofile> -preset <vpreset>
# <vparams>
# -acodec <acodec> -b:a <abitrate> -ar <asample_rate> -ac <achannels>
# <aparams>
# -f <oformat>
# -y <output>
engine example {
# whether the engine is enabled
# default: off.
enabled on;
# pre-file options, before "-i"
perfile {
re;
rtsp_transport tcp;
}
# 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.
# png: use png to snapshot thumbnail.
# 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.
# image2, for vcodec png to snapshot thumbnail.
# 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];
}
}
}
复制

该配置对频道的所有流转码。譬如:

  • 推送流:rtmp://dev:1935/live/livestream
  • 观看原始流:rtmp://dev:1935/live/livestream
  • 观看转码流:rtmp://dev:1935/live/livestream_ff

输出配置使用了变量替换,主要的参数是:

  • [vhost] 输入流的vhost。譬如:dev
  • [port] 输入流的端口。譬如:1935
  • [app] 输入流的app。譬如:live
  • [stream] 输入流名称。譬如:livestream
  • [engine] 转码engine名称,engine后面就是名称。譬如:ff 注意:转码会使用自动检测,保证推送到自己的流不会被再次转码。但转码推送到SRS自己的流可以被切片成HLS。譬如,若开启了HLS,上面的live/livestream,和转码出来的流live/livestream_ff都能观看HLS。

对app或流转码时,只要在transcode后面加app和stream就可以。


查看全文:http://ossrs.net/lts/zh-cn/docs/v4/doc/ffmpeg

请先 登录 后评论
  • 0 关注
  • 0 收藏,366 浏览
  • cici 提出于 2023-04-15 08:49

相似问题