採用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是最為流行的網路視訊格式,
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 &
六、其他命令
1. 分離音視訊流
./ffmpeg -i demo.mkv -vcodec copy -an demo.mp4 ## 分離出視訊流
./ffmpeg -i demo.mkv -acodec copy -vn demo.mp3 ## 分離出音訊流
2. 視訊解複用
./ffmpeg -i demo.mp4 -vcodec copy -an -f m4v demo.264
3. 音視訊封裝
./ffmpeg -i demo.mp4 -i demo.mp3 -vcodec copy -acodec copy demo.mkv