天天看點

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

指令符:

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、帶緩沖

配置檔案如下

MaxBandwidth10000

<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

要點:

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
mp4
mpg rtp
libx264
.asf asf
.mjpg mjpg
.jpg jpeg

配置例子:

Multipart JPEG

<Stream test.mjpg>

Feed feed1.ffm

Format mpjpeg

VideoFrameRate 2

VideoIntraOnly

Strict -1

Single JPEG

<Stream test.jpg>

Format jpeg

VideoSize 352x240

Flash

<Stream test.swf>

Format swf

ASF compatible

<Stream test.asf>

Format asf

VideoFrameRate 15

VideoBitRate 256

VideoBufferSize 40

VideoGopSize 30

AudioBitRate 64

MP3 audio

<Stream test.mp3>

Format mp2

AudioCodec mp3

AudioChannels 1

AudioSampleRate 44100

NoVideo

Ogg Vorbis audio

<Stream test.ogg>

Metadata title "Stream title"

AudioChannels 2

Real with audio only at 32 kbits

<Stream test.ra>

Format rm

AudioBitRate 32

Real with audio and video at 64 kbits

<Stream test.rm>

VideoBitRate 128

VideoFrameRate 25

VideoGopSize 25

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"

<Stream file.asf>

File "/usr/local/httpd/htdocs/test.asf"

Metadata author "Me"

Metadata copyright "Super MegaCorp"

Metadata title "Test stream from disk"

Metadata comment "Test comment"

實測可行的例子

配置:

<Streammy.mp4>

Formatrtp

File"/home/my.mp4"

用戶端指令 rtsp://192.168.1.230:9990/my.mp4

端口就是rtp的端口。使用http協定不能通路。

<Streamtest.mp4>

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>

VideoCodeclibx264

VideoFrameRate24

VideoBitRate100

VideoSize480x272

AVPresetVideodefault

AVPresetVideobaseline

AVOptionVideoflags +global_header

AudioCodeclibfaac

AudioBitRate32

AudioChannels2

AudioSampleRate22050

AVOptionAudioflags +global_header

使用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協定的位址格式為:

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>