采用ffmpeg和ffserver实现音视频直播和点播

一、嵌入式开发板采集音视频(远程)

        音频采集程序:./audioc -i 222.222.222.222 -p 8888;

        视频采集程序:./videoc -i 222.222.222.222 -p 9999;

        音频采集程序利用alsa库,采集原始音频帧,发送至服务器进程(222.222.222.222,  8888);

        视频采集程序利用v4l2库,采集原始视频帧,h.264硬编码后,发送至服务器进程(222.222.222.222, 9999);


二、Linux服务器接收音视频

        音频接收程序:./audios -rp 8888 -sp 8899 -i 239.255.1.2;

        视频接收程序:./videos -rp 9999 -sp 9988 -i 239.255.1.2;

        音频接收程序,接收原始音频帧,并发送至组播(239.255.1.2, 8899);

        视频接收程序,接收264视频帧,并发送至组播(239.255.1.2, 9988);


三、ffserver流媒体服务器搭建

       1. ffserver的配置文件:ffserver.conf

配置文件如:

####################################################################
# Port on which the server is listening. You must select a different
# port from your standard HTTP web server if it is running on the same
# computer.
HTTPPort 8090

# Address on which the server is bound. Only useful if you have
# several network interfaces.
HTTPBindAddress 0.0.0.0

# Number of simultaneous HTTP connections that can be handled. It has
# to be defined *before* the MaxClients parameter, since it defines the
# MaxClients maximum limit.
MaxHTTPConnections 2000

# Number of simultaneous requests that can be handled. Since FFServer
# is very fast, it is more likely that you will want to leave this high
# and use MaxBandwidth, below.
MaxClients 1000

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

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





######################################################################
# 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>

	# You must use 'ffmpeg' to send a live feed to ffserver. In this
	# example, you can type:
	#
	# ffmpeg http://localhost:8090/feed1.ffm

	# ffserver can also do time shifting. It means that it can stream any
	# previously recorded live stream. The request should contain:
	# "http://xxxx?date=[YYYY-MM-DDT][[HH:]MM:]SS[.m...]".You must specify
	# a path where the feed is stored on disk. You also specify the
	# maximum size of the feed, where zero means unlimited. Default:
	# File=/tmp/feed_name.ffm FileMaxSize=5M
	File feed1.ffm
	FileMaxSize 5M

	# You could specify
	# ReadOnlyFile /saved/specialvideo.ffm
	# This marks the file as readonly and it will not be deleted or updated.

	# Specify launch in order to start ffmpeg automatically.
	# First ffmpeg must be defined with an appropriate path if needed,
	# after that options can follow, but avoid adding the http:// field
	#Launch ffmpeg

	# Only allow connections from localhost to the feed.
	ACL allow 127.0.0.1
</Feed>

<Feed feed2.ffm>
	File feed2.ffm
	FileMaxSize 5M

	ACL allow 127.0.0.1
</Feed>
#########################################################################




#########################################################################
# live streams

# flv video + audio stream
 <Stream demo.flv>               	#Stream:每一个广播(转换后的视频流)的转码设置项目
	Format flv	 	    	#视频流格式
	Feed feed1.ffm                  #视频流的种子来源
	VideoFrameRate 25              	#视频帧频
	VideoBitRate 200                #视频比特率
	VideoGopSize 28
	VideoSize 480x272                #视频帧大小
	AVOptionVideo flags +global_header
	AudioBitRate 64
	AudioChannels 2
	AudioSampleRate 44100
	AVOptionAudio flags +global_header
	StartSendOnKey
</Stream>

# mp3 audio stream
<Stream demoAudio.mp3>
	Format mp3
	Feed feed2.ffm
	AudioBitRate 64
	AudioChannels 2
	AudioSampleRate 44100
	AVOptionAudio flags +global_header
	NoVideo
</Stream>

# swf video stream
# <Stream demoVideo.swf>               	#Stream:每一个广播(转换后的视频流)的转码设置项目
#	Format swf	 	    	#视频流格式
#	Feed feed3.ffm                  	#视频流的种子来源
#	VideoFrameRate 25              	#视频帧频
#	VideoBitRate 200                	#视频比特率
#	VideoGopSize 28
#	VideoSize 480x272                	#视频帧大小
#	AVOptionVideo flags +global_header
#	NoAudio
#</Stream>

# webm video + audio stream
# <Stream demoSystem.webm>               	#Stream:每一个广播(转换后的视频流)的转码设置项目
#	Format webm	 	    	#视频流格式
#	Feed feed1.ffm                  	#视频流的种子来源
#	AudioCodec vorbis
#	AudioBitRate 64
#	VideoCodec libvpx
#	VideoFrameRate 25              	#视频帧频
#	VideoBitRate 200                	#视频比特率
#	VideoGopSize 28
#	VideoSize 480x272                	#视频帧大小
#	AVOptionVideo flags +global_header
#	AVOptionVideo cpu-used 0
#	AVOptionVideo qmin 10
#	AVOptionVideo qmax 42
#	AVOptionAudio flags +global_header
#	PreRoll 15
#	StartSendOnKey
# </Stream>

# Ogg Vorbis audio
# <Stream test.ogg>
#	Format ogg
#	Feed feed2.ffm
#	Metadata title "Stream title"
#	AudioBitRate 64
#	AudioChannels 2
#	AudioSampleRate 44100
#	NoVideo
# </Stream>
########################################################################




########################################################################
# local streams

<Stream dengZiQi>
	File "dengZiQi.flv"
	Format flv
</Stream>

<Stream Maroon5_Sugar>
	File "Sugar.flv"
	Format flv
</Stream>
#########################################################################




#########################################################################
# Server status
<Stream stat.html>
	Format status

	# Only allow local people to get the status
	ACL allow 127.0.0.1
	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>
#########################################################################

            其中,<Stream demo.flv>是音视频直播流,从feed1.ffm中获取数据, <Stream demoAudio.mp3>为单独的音频流,从

feed2.ffm中获取数据。<Stream demoVideo.swf>为单独的视频流,由于ffserver在出现音频流后不能再出现单独的视频流,所以

此处注释掉了<Stream demoVideo.swf>。

        local streams下的都是本地文件,ffserver支持本地文件的点播。

        关于播放,swf是flash支持的格式,几乎所有的浏览器都支持播放此格式视频;flv是最为流行的网络视频格式,但一般情况

下,浏览器不支持该格式视频直接播放,需安装插件,这里采用vlc插件:http://www.videolan.org/;webm是最新的html5支持的

视频格式,此格式视频可以在chrome浏览器和firefox浏览器中直接播放;mp3、wav等音频格式可以在浏览器中直接播放。

        2. ffserver启动命令:

#! /bin/sh
./ffserver -f ./ffserver.conf


四、ffserver实现视频点播

       在ffserver.conf中加入如下命令:

<Stream dengZiQi>
	File "dengZiQi.flv"
	Format flv
</Stream>


五、向ffserver接入音视频流,实现视频直播

       1. 从组播中接入实时音视频流并转码封装

       ./ffmpeg -f s16le -ar 44100 -ac 1 -i udp://239.255.1.2:8899 -i udp://239.255.1.2:9988 http://222.222.222.222:8090/feed1.ffm

       前面udp流是音频流(udp地址为上述组播地址);中间udp流是视频流(udp地址为上述组播地址);后面http流是根据ffserver.conf

进行转码并封装后的特定格式音视频流(http地址为远程ffserver服务器地址),它存储在ffserver的feed1.ffm缓冲文件中。这里,由于

接收的是原始音频流,所以采用pcm_s16le压缩编码,采样率为44.1k,单声道;而视频流本身已经采用H.264压缩编码了,不再采

用其他压缩编码。

       2. 从组播中接入实时音频流

       ./ffmpeg -f s16le -ar 44100 -ac 1 -i udp://239.255.1.2:8899 http://222.222.222.222:8090/feed2.ffm

       3. 从组播中接入实时视频流

       ./ffmpeg -i udp://239.255.1.2:9988 http://222.222.222.222:8090/feed3.ffm

       4. 从本地文件中接入音视频流

       ./ffmpeg -i video.mp4 -i audio.mp3 http://222.222.222.222:8090/feed4.ffm

       5. 录制屏幕接入音视频流

       ./ffmpeg -f x11grab -r 25 -s 640x512 -i :0.0 -f alsa -i pulse http://222.222.222.222:8090/feed5.ffm

       6. 从摄像头获取视频麦克风获取音频接入音视频流

       ./ffmpeg -f video4linux2 -s 640x480 -r 25 -i /dev/video0 -f alsa -i pulse http://222.222.222.222:8090/feed6.ffm

命令如:

#! /bin/sh
exec ./audios -rp 8888 -sp 8899 -i 239.255.1.2 1>/dev/null 2>&1 &
exec ./videos -rp 9999 -sp 9988 -i 239.255.1.2 1>/dev/null 2>&1 &
exec ./ffmpeg -f s16le -ar 44100 -ac 1 -i udp://239.255.1.2:8899 -i udp://239.255.1.2:9988 http://222.222.222.222:8090/feed1.ffm 1>/dev/null 2>&1 &
exec ./ffmpeg -f s16le -ar 44100 -ac 1 -i udp://239.255.1.2:8899 http://222.222.222.222:8090/feed2.ffm 1>/dev/null 2>&1 &
exec ./ffmpeg -i udp://239.255.1.2:9988 http://222.222.222.222:8090/feed3.ffm 1>/dev/null 2>&1 &




文章来自:http://blog.csdn.net/zuijinhaoma8/article/details/45826001
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3