Monday, February 01, 2010

It's very easy to make complex systems using pipes and other simple UNIX shell techniques, if you know how to fully exploit the properties of what you're using.

This is a great example - a two-line streaming system suitable for both recording DV and streaming it to an overflow room @ a conference:

record side:
dvgrab - | ffmpeg -f dv -i - -vcodec mpeg1video -deinterlace -bframes 0 -b 2048k -ab 128k -f mpeg - >> test.mpg

This runs fine on a P4/2.53B (Northwood) system with 512MB ram - easily obtainable for <$100 if you have a good local source.

MPEG-1 video and mp2 (not mp3) audio with no B frames are no longer patent-encumbered and are much faster than Ogg Theora, and with far wider software support. Almost all MPEG-2 decoding systems can deal with MPEG-1 files, and all properly coded mp3 players can play mp2, which can sound really good at 224Kb/sec (VCD bitrate)

mplayer is able to jump into MPEG-1 PS files mid-stream and handle new encoding sessions. So using >> for ffmpeg's output is quite reasonable and provides robustness if the DV capture fails, since dvgrab|ffmpeg can be put into a loop.

You can use other capture systems as well - anything ffmpeg supports, which is quite a lot. You just need to adjust the input-side -f parameter to tell it what to expect.

viewer side:
ssh 192.168.1.40 "tail -f -c 1024 test.mpg" | mplayer -cache 512 -

This is also very simple. It makes an ssh connection to the recording host. tail (at least with -c) is 8-bit clean so you can stream MPEG-1 data through it to mplayer, which can jump in at any point.

There are many different ways to handle the viewer. netcat would work, and anything that can deal with MPEG-1 from standard output, starting mid-stream can be used as a player. mplayer is just the most common one on Linux.