Menu Home

基于ffmpeg的流媒体服务器

OS:ubuntu 12.04
ffmpeg:N-47141-g4063bb2
x264:0.133.2334 a3ac64b
目标:
使用ffserver建立流媒体服务器
使用ffmpeg对本地文件流化(x264编码器)
使用ffplay播放

ffserver.conf配置

Port 8090  #访问端口
RTSPPort 554 #rtsp端口
BindAddress 0.0.0.0 #服务器地址绑定
MaxHTTPConnections 2000 #允许的最大连接数
MaxClients 1000 #最大的客户数
MaxBandwidth 10000
CustomLog -

<Feed feed1.ffm>
File /tmp/feed1.ffm
FileMaxSize 1G
ACL allow 127.0.0.1 #只允许本地连接
</Feed>

################ asf ################

<Stream test.asf>
Feed feed1.ffm
Format asf #流媒体格式
VideoCodec libx264 #编码格式
VideoFrameRate 25 #桢率
VideoSize 1024x768 #播放大小
VideoBitRate 2M #码率
VideoBufferSize 10000000
VideoGopSize 24
StartSendOnKey
Preroll 5
#使用x264编码时必须有下面的4个参数
VideoQMin 3
VideoQMax 31
AVOptionVideo flags +global_header
AVOptionAudio flags +global_header
NOAudio
</Stream>

################ flv ################

<Stream test.flv>

Feed feed1.ffm
Format flv 

NoAudio
#AudioBitRate  32  
#AudioChannels 2
#AudioSampleRate  44100
#AVOptionAudio flags +global_header
#AudioCodec libmp3lame 

#NoVideo
VideoBitRate  1024 
VideoFrameRate  40  
VideoSize 1280x720 
VideoBufferSize 1000000
VideoCodec libx264 

VideoQMin 3 
VideoQMax 31  

VideoBitRateTolerance 100 
VideoGopSize 12  
StartSendOnKey

AVOptionVideo qmin 3
AVOptionVideo qmax 31
#AVOptionVideo quality good
#AVOptionVideo cpu-used 0
AVOptionVideo flags +global_header
#AVOptionVideo keyint_min 25
#AVOptionVideo qcomp 0.6
AVOptionVideo qdiff 4
#AVOptionVideo me_range 16
#Preroll 15
</Stream>

################ rtsp ################

<Stream live.h264>
Format rtp
Feed feed1.ffm
VideoCodec libx264
#VideoFrameRate 7
VideoBitRate 300
#VideoSize 480x272
VideoSize 352x288
#AVPresetVideo default
AVPresetVideo superfast
AVOptionVideo flags +global_header

PixelFormat yuv420p
AVOptionVideo me_range 4
AVOptionVideo qdiff 4
AVOptionVideo qmin 2
AVOptionVideo qmax 51

#MulticastAddress 224.124.0.1
#MulticastPort 5000
#MulticastTTL 1

NoAudio
#AudioCodec libfaac
#AudioBitRate 32
#AudioChannels 2
#AudioSampleRate 22050
#AVOptionAudio flags +global_header
</Stream>

################ html ################

<Stream stat.html>
Format status
# Only allow local people to get the status
ACL allow localhost
ACL allow 192.168.0.0 192.168.255.255
#FaviconURL http://pond1.gladstonefamily.net:8080/favicon.ico
</Stream>

# Redirect index.html to the appropriate site
<Redirect index.html>
URL http://www.ffmpeg.org/
</Redirect>

启动ffserver服务器

ffserver -d -f ./ffserver.conf

使用ffmpeg对本地文件流化

./ffmpeg -i  ./1.mov -vcodec libx264  -qmin 3 -qmax 31 -qdiff 4 -me_range 16 -keyint_min 25 -qcomp 0.6 -b 9000K    http://localhost:8090/feed1.ffm

捕捉本地摄像头数据

./ffmpeg -f video4linux2 -i /dev/video0 http://localhost:8090/feed1.ffm 

为流媒体增加声音

在ffmserver.conf中打开声音:

#NoAudio
AudioBitRate  128kb 
AudioChannels 2 
AudioSampleRate 44100 
AVOptionAudio flags +global_header

在输入中加入声音捕获:

ffmpeg  -f v4l2 -i  /dev/video0  -f alsa  -i hw:0,0 http://localhost:8090/feed_tv.ffm

使用ffplay播放

./ffplay  mmsh://localhost:8090/test.asf
./ffplay  http://localhost:8090/test.flv
./ffplay  rtsp://localhost:554/live.h264

git: https://github.com/lnmcc/multi_stream_server.git

1 reply

Leave a Reply

Your email address will not be published.