Ffserver

From OnnoWiki
Jump to navigation Jump to search

Live video streaming from Ubuntu Tux, the Linux mascot.

As part of a new hobby project involving real live mice, a couple of webcams and a laptop, I got to spend some time setting up a more or less robust live streaming service. At this point in time the whole setup is offline due to dust accumulating inside the laptop as well as the mice gnawing through too many USB cables. The setup should work in more forgiving environments, though.


Webcams

This web stream from the inside of a mouse tank needed to be cheap. Mice are very active rodents so they tend to chew on just about anything. Especially at first I didn't want to invest in expensive webcams only to have them chewed to bits in a few hours. My fears were quickly confirmed. As I'm typing this, there's a wireless webcam on the table next to me. Except, this cam actually does need the USB wire so it's broken now thanks to our subjects.

The cheap model webcam I picked is branded "Tecknet" and is available from Amazon. The biggest selling point to me on this specific model is the group of bright white LED lights installed in the front of the cam. This makes a huge difference in a shady environment like a mouse tank. The LED intensity is controlled by a knob in the wire, so it's independent from the PC control.

Computing hardware

While waiting for a friend's soon to be genrously donated half-broken first generation Acer Aspire One netbook, I decided to hook up a laptop from work. It's a Lenovo T60p, which is much too powerful for this purpose. A simple netbook booted off a USB stick will do just fine.

Essentially what I needed was a device that can pull video from USB at 640x480x15fps, MPEG4-encode this in real time and push that over WiFi to a remote multiplexer for use by viewers.

Software: FFMpeg and FFServer

The FFMpeg package that comes with Ubuntu is all you need to get this working. Out of the box Ubuntu doesn't ship with any non-free codecs in FFMpeg, but luckily I didn't need those anyway (yet) so I could get away with a simple:

apt-get install ffmpeg

It is actually important to have the exact same FFMpeg package on both the encoder laptop and on the multiplexing server. The FFMpeg and FFServer compontents are very picky about their communications between particular versions. Assuming you have FFMpeg installed on both your server and the laptop, you should begin with the server. The server is the compontent that will later dictate the encoding parameters to the laptop, so make sure you have that set correctly.

The FFServer component is what will be streaming your goods to visitors from the internet. You don't need big hardware for this, but do make sure you have the necessary traffic allowance. For instance, the Muizenwereld webcam streams 24/7 at 320kbps. Those are indeed kilobits, but they add up to almost 3GB (yes, bytes) per day of incoming traffic just to your multiplexer server alone. This is without anyone even watching your stream to begin with. Every 24 hours of actual viewing adds another rough 3GB per day of outbound traffic to your stats. FFServer configuration

Configuring the FFServer component is actually quite simple. You put the following into /etc/ffserver.conf, start up the ffserver binary and you're all set.

Port 8090
BindAddress 78.47.18.19
MaxHTTPConnections 30
MaxClients 20
MaxBandwidth 2000
CustomLog -

<Feed cam1.ffm>
File /tmp/cam1.ffm
FileMaxSize 10000K
ACL allow 12.34.56.78
</Feed>

#-----
# Windows Media Player
#-----
<Stream cam1.asf>
Feed cam1.ffm
Format asf
VideoCodec msmpeg4v2
VideoFrameRate 15
VideoSize vga
VideoBitRate 320
VideoBufferSize 40
VideoGopSize 15
NoAudio StartSendOnKey
</Stream>

<Stream stat.html>
Format status
ACL allow 12.34.56.78
</Stream>

The first section defines some general settings such as the TCP port to listen on and the network interface. These options are pretty self-explanatory. The interesting bits are the feed definition and the actual stream itself.

The feed is really simple. It designates a file in /tmp as a buffer to store a backlog of your feed. This is useful if you want your visitors to be able to pick a starting point sometime in the past. The bigger you let this file become, the further back in time you'll be able to seek.

The first Stream block defines parameters for bog standard Windows Media streaming. It turned out that this is by far the most reliable way to push video with FFServer and I quickly gave up on Flash video. The most interesting bits here are VideoBitRate, VideoFrameRate and of course make sure you stream from the correct feed.

The Status stream is a special case. It's limited by ACL to allow only my home IP (fake in this exmple). Hitting this stream with a web browser displays a status report on FFServer's workings. Whether your streams are up, whether your feeds are working, how many viewers there are etc.

FFMpeg: feeding the video

The settings in your server's Stream blocks automatically dictate encoding settings on the machine that has your webcam attached to it. Make sure you have FFServer running and reachable from your encoding machine. Then, assuming your webcam is a Video4Linux device at /dev/video0, give the following command

ffmpeg -f video4linux2 -i /dev/video0 78.47.18.19/cam1.ffm

Of course you should substitute your own FFServer in the above example. At this point, FFMpeg will begin capturing video from your camera. It'll do the transcoding to the proper format locally and send that off to your server. Leave it to encode for about half a minute and then hit your server's stream URL using Windows Media using this URL: 78.47.18.19/cam1.asf The player will buffer for a bit and your video will start playing. Congratulations, you're done.


Pranala Menarik

Referensi

Intro ffmpeg

Capture Video


Play Video