20 September, 2009

Streaming with FFMpeg

For work I've been playing with VLC to support some streaming experiments for the program I'm working on. In the process of installing VLC it became obvious the vast number of package dependencies it's reliant on.

So, for fun, I played with streaming via FFMpeg at home. Below are my findings.

Assuming you have FFMpeg already installed on your machine, there are 3 steps:

1) start the ffserver application with appropriate configuration file.
2) start ffmpeg to redirect the input to the stream
3) view with video client

FFServer Setup
The ffserver utility is installed as part of the FFMpeg package, no need to install it explicitly. You do however need to author an appropriate configuration file. Below is the configuration file I've used as a starting point.

$ cat ffserver.conf
Port 8090
# bind to all IPs aliased or not
BindAddress 0.0.0.0
# max number of simultaneous clients
MaxClients 10
# max bandwidth per-client (kb/s)
MaxBandwidth 10000
# Suppress that if you want to launch ffserver as a daemon.
NoDaemon


File /tmp/feed1.ffm
FileMaxSize 5M


# FLV output - good for streaming

# the source feed
Feed feed1.ffm
# the output stream format - FLV = FLash Video
Format flv
VideoCodec flv
# this must match the ffmpeg -r argument
VideoFrameRate 10
# generally leave this is a large number
VideoBufferSize 80000
# another quality tweak
VideoBitRate 200
# quality ranges - 1-31 (1 = best, 31 = worst)
VideoQMin 30
VideoQMax 31
VideoSize 352x288
# this sets how many seconds in past to start
PreRoll 0
# wecams don't have audio
Noaudio


# ASF output - for windows media player

# the source feed
Feed feed1.ffm
# the output stream format - ASF
Format asf
VideoCodec msmpeg4
# this must match the ffmpeg -r argument
VideoFrameRate 10
# generally leave this is a large number
VideoBufferSize 80000
# another quality tweak
VideoBitRate 200
# quality ranges - 1-31 (1 = best, 31 = worst)
VideoQMin 30
VideoQMax 31
VideoSize 352x288
# this sets how many seconds in past to start
PreRoll 0
# wecams don't have audio
Noaudio

$


Next, you need to run ffserver as follows:

$ ffserver -f ffserver.conf


FFMpeg Setup
The ffmpeg utility feeds the server. The following example uses a quick cam as the source, redirecting through the video server to be viewed by the clients. Worth noting, the ffserver configuration file specifies a frame rate, in this case 10fps, which must match the command line specification of ffmpeg.


$ ffmpeg -r 10 -s 352x288 -f video4linux -i /dev/video0 http://localhost:8090/feed1.ffm



Video Client Setup
Lastly, test the feed using a video client. For our example, we'll use mplayer as follows:

$ mplayer http://localhost:8090/test.asf

Play nice.

No comments: