ffmpeg与ffserver的协同工作
ffmpeg和ffserver配合使用可以實現實時的流媒體服務,可以實時傳輸來自攝像頭的數據,客戶端可以采用HTTP、RTSP、RTP協議等播放視頻流。
?
一、概念和流程
ffmpeg和ffserver配合使用涉及到四個概念:
1.??????ffmpeg,負責媒體文件的轉碼工作,把你服務器上的源媒體文件轉換成要發送出去的流媒體文件。
2.??????ffserver,負責響應客戶端的流媒體請求,把流媒體數據發送給客戶端。
3.??????ffserver.conf,ffserver啟動時的配置文件,在這個文件中主要是對網絡協議,緩存文件feed1.ffm和要發送的流媒體文件的格式參數做具體的設定。
4.??????feed1.ffm,可以看成是一個流媒體數據的緩存文件,ffmpeg把轉碼好的數據發送給ffserver,如果沒有客戶端連接請求,ffserver把數據緩存到該文件中。
?
工作流程如下:
1、 啟動ffserver,配置參數
ffserver先于ffmpeg啟動,它在啟動的時候需要加參數-f指定其配置文件,配置文件里包含端口信息、緩沖文件配置、傳送流配置(如編碼方式,幀率,采樣率……)。
?
2、 啟動ffmpeg,輸入流
??? 啟動ffmpeg,向緩沖文件輸入數據流,數據流可以來自攝像頭,也可以來自本來就存在的文件。
feed1.ffm是一個緩沖文件,fserver啟動后,feed1.ffm就會自動被創建,feed1.ffm開始的部分已經寫入向客戶端傳送流的配置信息,在feed1.ffm做緩沖用的時候,這些信息是不會被覆蓋掉。
ffmpeg啟動的一個關鍵參數就是“http://ip:port/feed1.ffm”,其中ip是運行ffserver主機的ip,如果ffmpeg和ffserver都在同一系統中運行的話,用localhost或者127.0.0.1也行。ffmpeg啟動后會與ffserver建立一個連接(短暫的連接),通過這第一次的連接,ffmpeg從ffserver那里獲取了向客戶端輸出流的配置,并把這些配置作為自己編碼輸出的配置,然后ffmpeg斷開了這次連接,再次與ffserver建立連接(長久的連接),利用這個連接ffmpeg會把編碼后的數據發送給ffserver。如果你觀察ffserver端的輸出就會發現這段時間會出現兩次HTTP的200,這就是兩次連接的過程。
3、連接過程
ffmpeg從攝像頭獲取數據后,按照輸出流的編碼方式編碼,然后發送給ffserver,ffserver收到ffmpeg的數據后,如果網絡上沒有播放的請求,就把數據寫入feed1.ffm中緩存,寫入時把數據加上些頭信息然后分塊,每塊4096B(每塊也有結構),當feed1.ffm的大小到了ffserver.conf中規定的大小后,就會從文件開始(跳過頭)寫入,覆蓋舊的數據。直到網絡上有播放的請求,ffserver從feed1.ffm中讀取數據,發送給客戶端。
?
?
二、配置與應用
?
ffserver可以配置為帶緩沖或者不帶緩沖,其中不帶緩沖的只需要配置stream的位置,不需要feed和ffmpeg。
ffserver配置文件可以參考ffmpeg源碼中的doc/ffserver.conf,里邊有詳細的注釋。文件的結構可以分為頭部信息、實時流信息、格式信息。
?
1、不帶緩沖
最簡單的配置文件如下
Port 9999
RTSPPort9990
BindAddress0.0.0.0
MaxClients1000
MaxBandwidth100000
CustomLog–
#只需要指定待播放的文件的路徑以及格式信息即可
<Streamtest.flv>
?? File "/home/test.flv"
?? Format flv
</Stream>
#rtsp應用
<Streamtest.mpg>
File"myfile/testvideo/test.mpg"
Format?rtp
</Stream>
?
命令符:
1. 在終端里輸入ffserver -f /etc/ffserver.conf
2. 在瀏覽器里或者相關播放器地址里輸入 http://ipAddr:port/test.flv
備注:1、Port為配置里面的9999,文件名直接輸入流的文件名即可。
?????2、實際測試flv等格式都可以播放。
3、測試需要的test.flv的可以使用ffmpeg錄制,命令是
ffmpeg -f v4l2 -s 320*240 -r 10 -i /dev/video2-vcodec flv? /test.flv
?
?
2、帶緩沖
配置文件如下
Port 9999
RTSPPort9990
BindAddress0.0.0.0
MaxClients1000
MaxBandwidth10000
CustomLog–
?
<Feed?feed1.ffm>
File/tmp/feed1.ffm
FileMaxSize40k
ACL allow127.0.0.1
</Feed>
?
<Stream?test.flv>
Feedfeed1.ffm
Formatflv
BitExact
DctFastint
IdctSimple
VideoFrameRate10
VideoSize320x240
VideoBitRate64
VideoGopSize10
NoAudio
?
PreRoll10
StartSendOnKey
MaxTime100
?
</Stream>
?
?
?
?
?
要點:
1、實時流數據配置,其中注意文件的位置,可以放到tmp文件夾下面,這樣會被自動清理掉。
2、每個不同的流都來自feed1.ffm,因此配置越多的流,當執行的時候,會逐個轉換,影響速度,一般不建議多配置。
3、ACL allow表示ip的地址范圍,比如ACL allow 192.168.0.0 192.168.255.255
?
命令符:
1. 在終端里輸入
ffserver -f/etc/ffserver.conf
2. a.若是文件方式則輸入
ffmpeg -i/home/test.flv http://127.0.0.1:9999/test.flv
b.若是實時視頻則輸入
ffmpeg -fv4l2 -framerate 30?-i?/dev/video2http://127.0.0.1:9999/feed1.ffm
?
3、運行客戶端命令
http://192.168.1.230:9999/test.flv
rtsp://ip:port/rtsp.mpg
?
?
三、流的格式
文件的拓展名對應一定的格式,常用的有:
| 拓展名 | 格式 |
| flv | flv |
| mp4 | mp4 |
| mpg | rtp |
| ? | libx264 |
| .asf | asf |
| .mjpg | mjpg |
| .jpg | jpeg |
?
配置例子:
?
Multipart JPEG
| ? | <Stream test.mjpg> Feed feed1.ffm Format mpjpeg VideoFrameRate 2 VideoIntraOnly NoAudio Strict -1 </Stream> |
Single JPEG
| ? | <Stream test.jpg> Feed feed1.ffm Format jpeg VideoFrameRate 2 VideoIntraOnly VideoSize 352x240 NoAudio Strict -1 </Stream> |
Flash
| ? | <Stream test.swf> Feed feed1.ffm Format swf VideoFrameRate 2 VideoIntraOnly NoAudio </Stream> |
ASF compatible
| ? | <Stream test.asf> Feed feed1.ffm Format asf VideoFrameRate 15 VideoSize 352x240 VideoBitRate 256 VideoBufferSize 40 VideoGopSize 30 AudioBitRate 64 StartSendOnKey </Stream> |
MP3 audio
| ? | <Stream test.mp3> Feed feed1.ffm Format mp2 AudioCodec mp3 AudioBitRate 64 AudioChannels 1 AudioSampleRate 44100 NoVideo </Stream> |
Ogg Vorbis audio
| ? | <Stream test.ogg> Feed feed1.ffm Metadata title "Stream title" AudioBitRate 64 AudioChannels 2 AudioSampleRate 44100 NoVideo </Stream> |
Real with audio only at 32 kbits
| ? | <Stream test.ra> Feed feed1.ffm Format rm AudioBitRate 32 NoVideo </Stream> |
Real with audio and video at 64 kbits
| ? | <Stream test.rm> Feed feed1.ffm Format rm AudioBitRate 32 VideoBitRate 128 VideoFrameRate 25 VideoGopSize 25 </Stream> |
For stream coming from a file: you onlyneed to set the input filename and optionally a new format.
| ? | <Stream file.rm> File "/usr/local/httpd/htdocs/tlive.rm" NoAudio </Stream> |
- ?
| ? | <Stream file.asf> File "/usr/local/httpd/htdocs/test.asf" NoAudio Metadata author "Me" Metadata copyright "Super MegaCorp" Metadata title "Test stream from disk" Metadata comment "Test comment" </Stream> |
?
?
?
?
?
實測可行的例子
配置:
<Streammy.mp4>
Formatrtp
File"/home/my.mp4"
</Stream>
?
客戶端命令 rtsp://192.168.1.230:9990/my.mp4
端口就是rtp的端口。使用http協議不能訪問。
?
?
?
<Streamtest.mp4>
Feedfeed1.ffm
Formatrtp
BitExact
DctFastint
IdctSimple
VideoFrameRate10
VideoSize320x240
VideoBitRate64
VideoGopSize10
NoAudio
?
PreRoll10
StartSendOnKey
MaxTime100
?
</Stream>
?
ffmpeg -fv4l2 -r 10 -I /dev/video2?http://127.0.0.1:9999/feed1.ffm
客戶端命令 rtsp://192.168.1.230:9990/test.mp4
?
<Streamlive.h264>
Formatrtp
Feedfeed1.ffm
VideoCodeclibx264
VideoFrameRate24
VideoBitRate100
VideoSize480x272
AVPresetVideodefault
AVPresetVideobaseline
AVOptionVideoflags +global_header
?
AudioCodeclibfaac
AudioBitRate32
AudioChannels2
AudioSampleRate22050
AVOptionAudioflags +global_header
</Stream>
?
使用H.264編碼時,使用命令
?
ffmpeg -f v4l2 -s 176*144 -r 2 -i /dev/video0-vcodec libx264 http://192.168.1.6:8090/feed1.ffm
ffmpeg-f v4l2 -s 176*144 -r 2?-vpre libx264-hq.ffpreset?-i /dev/video0 -vcodeclibx264http://localhost:8090/feed1.ffm
ffmpeg-f v4l2 -s 176*144 -r 10?-vpre libx264-hq.ffpreset-i /dev/video0 -vcodec libx264?-f rtprtp://192.168.1.105:6060 > /tmp/x264.sdp
?
四、協議
HTTP協議的地址格式為:
http://ffserver_ip_address:http_port/stream_name[options]
RTSP協議的地址格式為:
http://ffserver_ip_address:rtsp_port/stream_name[options]
?
?
Sampleffserver configuration file
Port 8090 BindAddress 0.0.0.0 MaxHTTPConnections 2000 MaxClients 1000 MaxBandwidth 1000 CustomLog - ################################################################## <Feed feed1.ffm> # ffmpeg http://localhost:8090/feed1.ffm File /tmp/feed1.ffm FileMaxSize 200K ACL allow 127.0.0.1 </Feed> ################################################################## <Stream test1.mpg> Feed feed1.ffm # 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. # asf??????? : ASF compatible streaming (Windows Media Player format). # swf??????? : Macromedia Flash compatible stream # avi??????? : AVI format (MPEG-4 video, MPEG audio sound) Format mpeg # Bitrate for the audio stream. Codecs usually support only a few # different bitrates. 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 # Bitrate for the video stream VideoBitRate 64 # Ratecontrol buffer size VideoBufferSize 40 # Number of frames per second VideoFrameRate 3 # Size of the video frame: WxH (default: 160x128) # The following abbreviations are defined: sqcif, qcif, cif, 4cif, qqvga, # qvga, vga, svga, xga, uxga, qxga, sxga, qsxga, hsxga, wvga, wxga, wsxga, # wuxga, woxga, wqsxga, wquxga, whsxga, whuxga, cga, ega, hd480, hd720, # hd1080 VideoSize 160x128 # Transmit only intra frames (useful for low bitrates, but kills frame rate). #VideoIntraOnly # If non-intra only, an intra frame is transmitted every VideoGopSize # frames. Video synchronization can only begin at an intra frame. VideoGopSize 12 # More MPEG-4 parameters # VideoHighQuality # Video4MotionVector # Choose your codecs: #AudioCodec mp2 #VideoCodec mpeg1video # Suppress audio #NoAudio # Suppress video #NoVideo #VideoQMin 3 #VideoQMax 31 # Set this to the number of seconds backwards in time to start. Note that # most players will buffer 5-10 seconds of video, and also you need to allow # for a keyframe to appear in the data stream. #Preroll 15 # ACL: # You can allow ranges of addresses (or single addresses) #ACL ALLOW <first address> # You can deny ranges of addresses (or single addresses) #ACL DENY <first address> # You can repeat the ACL allow/deny as often as you like. It is on a per # stream basis. The first match defines the action. If there are no matches, # then the default is the inverse of the last ACL statement. # # Thus 'ACL allow localhost' only allows access from localhost. # 'ACL deny 1.0.0.0 1.255.255.255' would deny the whole of network 1 and # allow everybody else. </Stream> ################################################################## # Example streams # Multipart JPEG #<Stream test.mjpg> #Feed feed1.ffm #Format mpjpeg #VideoFrameRate 2 #VideoIntraOnly #NoAudio #Strict -1 #</Stream> # Single JPEG #<Stream test.jpg> #Feed feed1.ffm #Format jpeg #VideoFrameRate 2 #VideoIntraOnly ##VideoSize 352x240 #NoAudio #Strict -1 #</Stream> # Flash #<Stream test.swf> #Feed feed1.ffm #Format swf #VideoFrameRate 2 #VideoIntraOnly #NoAudio #</Stream> # ASF compatible <Stream test.asf> Feed feed1.ffm Format asf VideoFrameRate 15 VideoSize 352x240 VideoBitRate 256 VideoBufferSize 40 VideoGopSize 30 AudioBitRate 64 StartSendOnKey </Stream> # MP3 audio #<Stream test.mp3> #Feed feed1.ffm #Format mp2 #AudioCodec mp3 #AudioBitRate 64 #AudioChannels 1 #AudioSampleRate 44100 #NoVideo #</Stream> # Ogg Vorbis audio #<Stream test.ogg> #Feed feed1.ffm #Title "Stream title" #AudioBitRate 64 #AudioChannels 2 #AudioSampleRate 44100 #NoVideo #</Stream> # Real with audio only at 32 kbits #<Stream test.ra> #Feed feed1.ffm #Format rm #AudioBitRate 32 #NoVideo #NoAudio #</Stream> # Real with audio and video at 64 kbits #<Stream test.rm> #Feed feed1.ffm #Format rm #AudioBitRate 32 #VideoBitRate 128 #VideoFrameRate 25 #VideoGopSize 25 #NoAudio #</Stream> ################################################################## # A stream coming from a file: you only need to set the input # filename and optionally a new format. Supported conversions: #??? AVI -> ASF #<Stream file.rm> #File "/usr/local/httpd/htdocs/tlive.rm" #NoAudio #</Stream> #<Stream file.asf> #File "/usr/local/httpd/htdocs/test.asf" #NoAudio #Author "Me" #Copyright "Super MegaCorp" #Title "Test stream from disk" #Comment "Test comment" #</Stream> ################################################################## # RTSP examples # # You can access this stream with the RTSP URL: #?? rtsp://localhost:5454/test1-rtsp.mpg # # A non-standard RTSP redirector is also created. Its URL is: #?? http://localhost:8090/test1-rtsp.rtsp #<Stream test1-rtsp.mpg> #Format rtp #File "/usr/local/httpd/htdocs/test1.mpg" #</Stream> # Transcode an incoming live feed to another live feed, # using libx264 and video presets #<Stream live.h264> #Format rtp #Feed feed1.ffm #VideoCodec libx264 #VideoFrameRate 24 #VideoBitRate 100 #VideoSize 480x272 #AVPresetVideo default #AVPresetVideo baseline #AVOptionVideo flags +global_header # #AudioCodec libfaac #AudioBitRate 32 #AudioChannels 2 #AudioSampleRate 22050 #AVOptionAudio flags +global_header #</Stream> ################################################################## # SDP/multicast examples # # If you want to send your stream in multicast, you must set the # multicast address with MulticastAddress. The port and the TTL can # also be set. # # An SDP file is automatically generated by ffserver by adding the # 'sdp' extension to the stream name (here # http://localhost:8090/test1-sdp.sdp). You should usually give this # file to your player to play the stream. # # The 'NoLoop' option can be used to avoid looping when the stream is # terminated. #<Stream test1-sdp.mpg> #Format rtp #File "/usr/local/httpd/htdocs/test1.mpg" #MulticastAddress 224.124.0.1 #MulticastPort 5000 #MulticastTTL 16 #NoLoop #</Stream> ################################################################## # Special streams # Server status <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>總結
以上是生活随笔為你收集整理的ffmpeg与ffserver的协同工作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VMware虚拟机网络模式详解 NAT模
- 下一篇: 分布式转码初步方案(hadoop+ffm