Building a Linux-based time-shifting box

by Russell Pavlicek


FINAL NOTE:

I built this box around 2002 before there were many canned options for a Linux-based TV recording system. It was a great experience and it served me well for over 2 years. But more recently, I have been using a new box built on MythTV (which was a non-functional work-in-progress when I built my original system). MythTV is far more interesting technology than the stuff I built years ago. Still, I know history has value, so I am leaving this page intact so people can understand that it wasn't always as easy to build a time-shifting box as it is today. Enjoy... -- Russell Pavlicek, June 2006


2004 UPDATE: See Jamie Davis's instructions regarding using mencoder to increase quality while decreasing file size. Look here. I've found that on my limited box, there isn't enough horsepower to pull down 30 fps using Jamie's syntax. However, by dropping the load on the encoder (and VCD compatibility) I can get around 90% of the maximum capture rate. Details are below.

Introduction:

We don't watch tons of TV, but there are a few programs every week that we do like to see. I could buy a TiVo, but then I'd have to pay US$13 each month for the required service or a couple hundred bucks for a lifetime subscription. It may be worth it for many folks, but that's overkill for me. And I don't like the concept of having my destiny linked with the fortunes of the supplying company. I don't need a US$300 doorstop if the TiVo company should someday fail. Hey, if Enron and Worldcom can end up in the toilet, you have to allow for the fact that no one company will be around forever.

Materials:


HP/Unisys 400 Mhz Celeron "mini" PC, using Sahara-II motherboard (paid about US$100, including shipping; CPU upgrade for US$18)
Brooktree 878 based capture card (about US$40)
On board Mach64 3D Rage IIC
On board Ethernet Pro 100
Logitec USB mouse (US$7)
96 MB of memory (added 64 MB for US$12)
Western Digital 60 GB 7200 RPM Hard drive (under US$100)
TVator Pro (takes VGA signals and creates TV signals) (about US$45)
X10 Video Transmitter and Receiver (Optional; I had this already)
Added Packard Bell IR remote (under US$10; removes need for USB mouse)
USB keyboard and VGA monitor needed during system build; removable afterwards

Goals:

To create a TV timeshifting box that will run unattended to capture the weekly programs of value to our household. Capturing individual programs is less of an issue, but it should be possible. Playback should be easy to achieve, but I don't care if the record settings are particularly user-friendly, since I intend the box to capture basically the same programs every week.

There is no need to pause a live TV signal like some PVRs do. Neither is there a need to lookup shows by name or use VCR+ time codes.

The machine should be networked. It should be controllable from a laptop or my Zaurus.

It should also be capable of creating images that can be burned on Video CDs (VCD). It should be able to run without a keyboard. A mouse is acceptable.

Process:

Before I was serious about building this box, I decided to see how much of a task it would be to implement it. I picked up a very cheap, no-name video capture card at a local computer show. The box said it used the Brooktree 878 chipset -- one I remembered was well supported by the Video4Linux project. And the price was right for experimentation at US$40. I took it home and installed it on a crappy Pentium 166 I had lying around. I installed the card and Mandrake 8.2 and started playing with the card using XawTV. The I/O and speed limitations of that system kept it from being overly useful as a recorder, but it proved to me that the project was viable and that the equivalent of a second generation Pentium processor could probably do the job adequately.

As someone who has spent more time on eBay than I care to remember, I naturally started looking for viable hardware there. I found this strange little Hewlett Packard "built for Unisys" PC which is about 1 foot square and about 3.5 inches tall. It features a Sahara-II motherboard and had most of the hardware I needed built into the standard box. I added a mouse and keyboard and the TV capture card and I was ready to start building a solution. It started out as a 300 MHz Celeron with 32 MB of memory, but I decided to fork over another US$30 or so and make it 400 Mhz and 96 MB. It actually worked before the upgrade, but the CPU seemed to be at its edge and I didn't want to use KDE with so little memory.

I set up the system to autologin to a passwordless user called (appropriately) "tv." I capture the programs into a subdirectory called (strangely enough) "shows."

The recording rate of the TV capture card appears to max out at 15 frames per second. If I were intending to archive these programs forever, I'd probably invest in a better card. But for timeshifting shows like the evening news from France (for my wife) and The Red Green Show (for me), 15 fps is adequate.

UPDATE: The card can do more than 15 fps, even on this old machine. As one reader discovered, the key was to add an explicit "-n ntsc" parameter on the command line. Without it, it would never go beyond 15 fps, regardless of what I set it to.

The on-board Rage SVGA card did not have video out. I had two choices: try to add another video card with composite output or buy an adapter to convert VGA output to composite output. Since I only have one PCI slot free in the box which I might want to use someday, and since I really like playing with strange hardware, I decided to purchase a VGA to composite adapter. Back to eBay I go, and for another 45 bucks I get a TVator.

Now I can watch TV on the TV. But, being the geek that I am, I could not resist digging out my old X10 Video Sender to send the signal to another TV as well. Very geeky, but pretty cool.

UPDATE:

A few people have asked questions which indicate that they believe that I watch all my TV "through" the box (like a TiVo). I could, but I don't. I watch normal programs on the TV. When I want to watch a recorded program, I set the TV to the channel which receives the output of the recorder, activate XINE using the AUX2 button on the Packard-Bell remote, and watch a recorded program. The box records programs without getting in the way of the TV, and I don't interact with it until I summon a playback.

Software:

For recording, I tried a few different pieces of software and then settled on streamer, which is a part of the XawTV package. The rather ugly set of parameters involved in recording with streamer (see Files section below) have been selected so that the resulting AVI file can be converted to VCD format. I haven't done this beyond experimentation at this point, but I figure that I may want to burn some VCDs at some point in time. The parameters I've selected should allow that. I also had to play with the parameters to get the most bang for the buck -- or in this case, the best picture quality without costing too much disk space. The parameters in the "rec" script yield programs that take up a little over 2 GB per hour on average. Adjusting the compression parameter can modify both picture quality and disk space used. For playback, I quickly narrowed the field to Xine and Mplayer. I've been back and forth a couple of times between them, but I have decided to use Xine for the moment. Most players seem to have problem that the picture and sound can slowly go out of sync over time. Xine seems to sync about the best, with the sound and video falling a second or so out of sync over an hour. When I decided to go with the TVator Pro for TV output, I had to change the resolution of the screen back to 640x480, which is the only resolution this unit can use. For normal desktop usage, that would be a disaster, but for watching TV it's a non-problem. Even a 640x480 VGA display is too fine for a television to reproduce.

Problems encountered:

Files:


Here are a couple interesting lines from modules.conf:

pre-install videodev modprobe -k tuner type=2
options bttv gbuffers=2 card=52 radio=1 fieldnr=1

The Mandrake installation properly loads the drivers, but you need to figure out which TV and radio tuner are implemented by the particular card. So I experimented for a little while and found that TV card 52 and radio 1 seemed to work best. It just took a little trial and error, loading and unloading the drivers with various parameters until the tuner seemed to work correctly under XawTV when changing stations.


I am currently using a record script called "rec" located in /home/tv which looks like the following:

#!/bin/bash 
if test -z "$2"; then echo "usage: channel lengthoftime"; exit; fi 
f=shows/`date +%y%m%d-%a-%H_%M` 
echo $f >> /tmp/TV_rec.err 
echo "  Recording channel $1 ($2) at `date`" >> /tmp/TV_rec.status 
killall -9 xine 2>> /tmp/TV_rec.err 
killall -9 mplayer 2>> /tmp/TV_rec.err
killall -9 xawtv 2>> /tmp/TV_rec.err 
v4lctl setchannel $1 2>> /tmp/TV_rec.err
v4lctl volume 100 2>> /tmp/TV_rec.err 
#streamer -q -t $2 -s 320x240 -r 15 -o $f.avi -f jpeg -j 86 -F stereo 2>> /tmp/TV_rec.err
streamer -n ntsc -q -t $2 -s 320x240 -r 20 -o $f.avi -f jpeg -j 86 -F stereo 2>> tmp/TV_rec.err
echo "      Ended recording channel $1 at `date`" >> /tmp/TV_rec.status

UPDATE: By using mencoder and by sacrificing the VCD compatibility, I can record around 27 fps in less space than I did before. Here's the new line that replaces streamer:

mencoder -tv  on:driver=v4l:device=/dev/video0:norm=NTSC:width=320:height=240 -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=800 -vop pp=lb 
 -endpos $2 -o $f.avi 2> /dev/null > /dev/null
Given the channel and a number of minutes and seconds to record, the script creates a file in "YYMMDD-day-HH_MM.avi" format (e.g., "021008-Tue-19_00.avi") in the "shows/" subdirectory.

If you are not used to shell scripts, don't panic. This script actually does a lot of things that aren't really crucial. All the output redirected to a log file is for my benefit. I also write to a status file before and after the recording, since I have a little window open on the box which shows me the log as it is written. The icon which displays the log is described as:

xterm -title "XVCR Recording Log" -geometry 80x8+20+440 -e tail -s 20 -f /tmp/TV_rec.status


The playback logic is handled by "play," also located in the home directory of the "tv" user.

#!/bin/bash
xine --auto-play=fh $1* > /tmp/TV_xine_play

For recording, I mainly use good old "cron." Of course the "at" command can be used to record individual programs, but most everything is controlled by the crontab file. Here's an example:

SHELL=/bin/bash
# Daily cleanup of anything over a week old
45 04 * * * $HOME/weekly_cleanup >> /tmp/TV_cleanup
# French News
00 19 * * * $HOME/rec 29 29:00
# Red Green
00 23 * * 6 $HOME/rec 3 28:00
It's ugly, but it works just fine. Eventually, a neato GUI should allow the user to specify the shows to record and then automatically build and submit the crontab file.


The cleanup script gets rid of week old programs. It runs nightly, as seen in the crontab file:

#!/bin/bash
date
del_it="`find shows/ -mtime +6`"
if test -n "$del_it"; then echo "Deleting " $del_it; rm -f $del_it; fi


I have an ugly bastardized piece of Tcl/Tk code that allows me to select a file from the "shows" subdirectory and then executes the "play" script above. It's crappy and largely swiped from other, nicer scripts. I would love to hear from someone who can do the task justice.


Final thoughts:

I don't consider this project "done," although I am in no rush to finish it. It works fine for my needs as is. As time allows, I intend to explore some of the PVR user interfaces which can be found at Freshmeat.net

I intended to replace the mouse with a wireless mouse-remote unit. Unfortunately, the one I got off eBay is DOA. We'll get there eventually, though.

I have also recently begun playing with xine-remote, which is a program to control Xine from another machine or another process on the same machine. I think this could be useful for better control from laptops or my Zaurus. All it really needs is a nice user-friendly shell around it, maybe written in Tcl/Tk or Python or some other nice visual scripting language.


UPDATE (16 October 2002):

I am now using LIRC to power an el-cheapo Packard Bell infrared remote I picked up from eBay for under US$10. This remote connects to the serial port. You need to load the LIRC serial port driver instead of the standard Linux driver. LIRC is a bit of a pain to set up because it is only now being integrated in Mandrake (the cooker for V9.0 shows RPMs for it). I expect that once it is part of most standard distributions, it will be much easier to use. I found that the website has all the information you need, but the information is almost impossible to find unless you use Google. Unfortunately, that's the price of working with a reasonably complex system of drivers which is still in very active development. I doubt this will be as much of an issue a few months from now.

Note that /etc/lircd.conf defines a series of codes corresponding to button names. Part of the flexibility of LIRC is that the button names are totally definable. Use whatever names make sense to you given the remote in question. But make certain that you use the exact same button names when you refer to them in /etc/lircmd.conf and ~/.lircd.conf. I have configured .lircd.conf to use xawtv by pressing Aux1 and xine by pressing Aux2. Using the mode structures (also not documented exceptionally well), the remote buttons are reprogrammed to provide appropriate input for each program. It's really quite cool.

I use the following autostart script in .kde/Autostart/ :

#!/bin/bash
irexec 2> /tmp/irexec.err > /tmp/irexec.out &
irxevent 2> /tmp/irxevent.err > /tmp/irxevent.out &

I added the following line to /etc/modules.conf to load the LIRC driver for the serial port:

alias char-major-61 lirc_serial

Here are some of the files used to configure LIRC for the Packard Bell remote:

Make sure you modify the XFree86 server configuration according to the instructions in the LIRC documentation.

By the way, this also gives me a solution for my ugly Tcl/Tk script. When I summon xine, I load the playlist with all the AVI files in the shows/ directory. I then use the menu within xine as the playback menu. It's not perfect, but it is pretty good.

But the biggest gain in this solution is the potential loss of keyboard and mouse. Neither the keyboard nor the mouse is now required to use the system. The properly configured remote makes these devices unnecessary -- just what you'd hope for in a box that might reside in your entertainment center.


Links:

Here is a link to the archives of The Linux Show webcast where I describe the project. My segment begins about 45 minutes into the program and lasts about 30 minutes:

Here are some links regarding Video for Linux support:

Here are a few other PVR-related links suggested by other folks who visited this website. Note that none of them are directly related to my little project:


Last updated 5 June 2006