ffmpeg+ffserver搭建rtsp服务器

1、ffserver

   ffserver在ffmpeg3.4版本以后的版本被移除,能用做http/rtsp流媒体服务器。其架构如下图所示。
在这里插入图片描述
上图中I、F、S、P概念:

  • 《1》、输入源
       能将音/视频传给ffserver的外部应用,通常是ffmpeg应用。输入源会和ffserver连接然后将自己绑定到一个或多个feed上。(同一时刻一个feed只能绑定一个输入源)。一个输入源能绑到多个feed的前提条件是改输入源能为其绑定的每个feed输出不同的流。
  • 《2》feeds
      feed元素是ffserver的一部分,用于将一个输入源与一个或多个stream关联。当你想将一个输入源输出为多种输出格式时推荐将一个feed与多个stream关联。(例如,同时输出full HD分辨率和为手机输出小分辨率)
  • 《3》streams
      stream是ffserver的一部分,是外部想要获取stream的客户端与ffserver的连接点。每一个stream可以处理多个客户端连接。
  • 《4》、Media players
      外部应用,一般指的是客户端。

2、ffserver.conf配置

HTTPPort 8090
RTSPPort 5454
# Address on which the server is bound. Only useful if you have
# several network interfaces.
HTTPBindAddress 192.168.1.252


MaxHTTPConnections 2000


# Access log file (uses standard Apache log file format)
# '-' is the standard output.
MaxClients 1000


# This the maximum amount of kbit/sec that you are prepared to
# consume when streaming to clients.
MaxBandwidth 30000


# Access log file (uses standard Apache log file format)
# '-' is the standard output.
CustomLog -


NoDaemon
#NoDefaults


##################################################################
# Definition of the live feeds. Each live feed contains one video
# and/or audio sequence coming from an ffmpeg encoder or another
# ffserver. This sequence may be encoded simultaneously with several
# codecs at several resolutions.


<Feed feed1.ffm>
File /tmp/feed1.ffm
FileMaxSize 5M


# Only allow connections from localhost to the feed.
ACL allow 127.0.0.1


</Feed>


##################################################################
# Now you can define each stream which will be generated from the
# original audio and video stream. Each format has a filename (here
# 'test1.mpg'). FFServer will send this stream when answering a
# request containing this filename.


#<Stream test1>
#Format rtp
#VideoCodec libx264
#AudioCodec aac
# coming from live feed 'feed1'
#Feed feed1.ffm


#File btv2.ts


# Format of the stream : you can choose among:
# mpeg       : MPEG-1 multiplexed video and audio
# mpegvideo  : only MPEG-1 video
# mp2        : MPEG-2 audio (use AudioCodec to select layer 2 and 3 codec)
# ogg        : Ogg format (Vorbis audio codec)
# rm         : RealNetworks-compatible stream. Multiplexed audio and video.
# ra         : RealNetworks-compatible stream. Audio only.
# mpjpeg     : Multipart JPEG (works with Netscape without any plugin)
# jpeg       : Generate a single JPEG image.
# mjpeg      : Generate a M-JPEG stream.
# asf        : ASF compatible streaming (Windows Media Player format).
# swf        : Macromedia Flash compatible stream
# avi        : AVI format (MPEG-4 video, MPEG audio sound)
#Format mpegts


#BitExact
#DctFastint
#IdctSimple
#videoFrameRate 25
#VideoSize 1920x1080


# Ratecontrol buffer size
#VideoBufferSize 4096


#VideoBitRate 90000
#VideoGopSize 25
#NoAudio
#AudioBitRate 32
# Number of audio channels: 1 = mono, 2 = stereo
#AudioChannels 1


# Sampling frequency for audio. When using low bitrates, you should
# lower this frequency to 22050 or 11025. The supported frequencies
# depend on the selected audio codec.
#AudioSampleRate 44100


#</Stream>


<Stream test1-rtsp>
#File "./in.264"
Feed feed1.ffm
Format rtp
VideoCodec libx264
VideoGopSize 25
VideoFrameRate 25
VideoBufferSize 80000
VideoSize 1920x1080
#VideoBitRate 90000
#AudioSampleRate 44100
PreRoll 15
NoDefaults
NoAudio
</Stream>

#一个输出声音,有声音时用ffplay是播放不出来,原因待查找
#只要设置为NoAudio就可以一个输入源输出两种分辨率
<Stream test2-rtsp>
#File "./in.264"
Feed feed1.ffm
Format rtp
VideoCodec libx264
VideoGopSize 25
VideoFrameRate 25
VideoBufferSize 80000
VideoSize 352x288
#VideoBitRate 90000
AudioCodec aac
AudioSampleRate 44100
# Number of audio channels: 1 = mono, 2 = stereo
#AudioChannels 2
AudioBitRate 32
PreRoll 15
NoDefaults
#NoAudio
</Stream>

<Stream stat.html>
Format status
ACL allow localhost


# You can allow ranges of addresses (or single addresses)
#ACL ALLOW <first address> <last address>
ACL allow 192.168.0.0 192.168.255.255


</Stream>

运行ffserver,制定配置文件

ffserver -f ffserver.conf

3、ffmpeg推流

ffmeg推流到ffserver遵循以下格式:

ffmpeg <inputs> <feed URL>

其中< feed URL >的格式如下:

http://<ffserver_ip_address_or_host_name>:<ffserver_port>/<feed_name>

该URL格式中的参数意义如下:

  • <ffserver_ip_address_or_host_name> - using the “BindAddress” directive
  • < ffserver_port > - using the “Port” directive
  • < feed_name > - using the “” block

若需要接受外部IP的客户端连接,则将host_name设置为本机IP,若不需要则设置为localhost即可。原文如下。
在这里插入图片描述

  例子:

只在本地主机使用:
ffmpeg -re -i "./test.mp4" -bf 0 -g 25 -r 25 -s 1920x1080 -b:v 4M -maxrate 8M -bufsize 1800k -vcodec libx264 -acodec aac  -ar 44100 http://localhost:8090/feed1.ffm

其他IP主机也可使用:
ffmpeg -re -i "./test.mp4" -bf 0 -g 25 -r 25 -s 1920x1080 -b:v 4M -maxrate 8M -bufsize 1800k -vcodec libx264 -acodec aac  -ar 44100 http://192.168.1.252:8090/feed1.ffm

4、验证

《1》、ffplay接收流

ffplay -i rtsp://localhost:5456/test1-rtsp
ffplay -i rtsp://localhost:5456/test2-rtsp

《2》、浏览器查看状态如下
在这里插入图片描述

5、参考

《1》、 https://trac.ffmpeg.org/wiki/ffserver
《2》、 https://stackoverflow.com/questions/37403282/is-there-anyone-who-can-success-real-time-streaming-with-ffserver
《3》、ffmpeg码率参数设置
《4》、ffserve stat设置使用
《5》、ffmpeg preset参数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值