HOWTO: Convert, transcode and stream a video feed.

There was this Foosball competition at my workplace and they ask me to do some nice! This was a side project at work that I complete in, roughly, 3 hours. It was fun because I was able to play with anything to make it work.

First lets explain what I had to do:

  • Use an old SMC TV-IP200 camera
  • Record it
  • Stream this to ~40 people
  • Archive it

The camera itself have a web server where you can see the live feed (a MJPEG stream). It’s nice and all BUT the port on the camera is 10 Mbps. After setting up the camera and adjusting the image settings the feed is 200 KB/s. More than 4 people watching it and the stream will lag.


I did something similar in the past using unrelated piece of software so they work together. I have planned a dedicated server for LAN gaming at work (after-hour mind you) so I decided to reuse it while it was doing nothing.

Specification aren’t great:

  • Intel(R) Core(TM)2 CPU 4300 @ 1.80GHz
  • 2GB of ram
  • 2x 80GB disk in raid1 (using LVM).
  • Ubuntu 9.10

Modest.

After some tweaking, tryout, recompiling I ended up with the following command:

#!/bin/bash
wget http://192.168.*.*/video.cgi –http-user=*** –http-password=*** -q -O – | ffmpeg -f mjpeg -i – -vcodec libtheora -an -b 512k -f ogg – | tee ~/foosball_archive/foosball_`date +%x_%H.%M.%S`.ogv | oggfwd -n “FoosBall Live” -d “Live stream from the Foosball table” 192.168.*.* 8000 *** /foosball.ogv

Quite a mess isn’t it? Let’s break it down:

wget http://192.168.*.*/video.cgi –http-user=*** –http-password=*** -q -O – |

first we download the stream and output it to the stdout [ -O - ] and we pipe it to ffmpeg:

ffmpeg -f mjpeg -i – -vcodec libtheora -an -b 512k -f ogg – |

Again, we read the incoming stdin [ -i - ] specifying its a MJPEG stream, encode it and send it back to stdout using the OGG format [ -f ogg - ] so we can save it:

tee ~/foosball_archive/foosball_`date +%x_%H.%M.%S`.ogv |

tee is a command I found while doing this. It will take the incoming stdin, write it to a file and send it back to stdout for oggfwd:

oggfwd -n “FoosBall Live” -d “Live stream from the Foosball table” 192.168.*.* 8000 *** /foosball.ogv

The last and final bit is oggfwd. This send the stream to an icecast2 server hosted locally.

The good news is the CPU usage is fairly low (around 30-40%), considering we are doing live transcoding.

The resulting stream works in VLC, MPlayer, Firefox, and most probably others. Chromium (5.0.315.0 (37939) Ubuntu) was not able to stream it but could load the first frame.

Then I had to start it at 8 h in the morning before I get to work. the at command came into play for this ;)

The stream also play on a TV in the lobby for everyone to enjoy!

The stream can event be embedded in a web page using the HTML5 video tag.

Hope this is useful to someone at one point.

Tags: , , , ,
| February 17th, 2010 | Posted in HowTo |

Comments are closed.