PVR Update

In anticipation of TV shows coming back on, I’ve been tweaking my PVR scripts. These record shows from the digital airwaves right onto my computer. I’ve separated the script into two parts: recording and encoding. Each is a simple one line shell script that can be called easily from the command line or from a cron. The record.sh script is:

/usr/bin/mencoder dvb://$1 -ovc copy -oac copy -o "$2.mpeg" -endpos $3

All this does is copy the video and audio streams directly to the disk. The three parameters are  the station name, as stated in ~/.mplayer/channels.conf, the filename and the end position. This end position can be the number or seconds to record, or a time format of hh:mm:ss. This makes recording fairly simple within the cron.

The encode.sh script contains:

/usr/bin/mencoder -vf scale=640:-2 -ovc xvid -xvidencopts bitrate=1700:threads=2 \
 -oac copy "$1" -o "$1.avi"

This, slightly more complex use of mencoder, will scale the video to 640 wide, the height calculated to keep the aspect ratio. The video is encoded into xvid at 1700kbps using 2 threads. To speed up the encoding process, the audio is not re-encoded, but copied as is.

You will need to play with the number of xvid threads to find the best fps. I have a dual-core and it encodes the fastest with 2 threads. It does need to be an integer, and most dual-cores will work best at 2.

Then, two lines are required in the cron to record and encode the program:

59 19 * * 1 /dvr/record.sh WAGTNBC /dvr/shows/heroes 01:03:00
05 20 * * 1 /dvr/encode.sh /dvr/shows/heroes.mpeg

Mencoder Cheat Sheet

Mencoder is a video encoder for linux. It is part of the mplayer package which also includes a video player. This tool is quite versatile and can do pretty much any kind of video processing all from the console.

As with any linux tool, there a tons of bells and whistles. This makes the learning curve kind of steep, but with a bit of determination anyone can make sense of it. Without further ado, here’s the cheat sheet:

mencoder clip1.avi clip2.avi -ovc copy -oac copy -o new-clip.avi Copies clip1 and clip2 into new-clip.avi
mencoder clip.avi -ovc xvid -xvidencopts bitrate=800  -oac lamemp3 -lameopts cb:br=128 -o new-clip.avi Encodes a clip with the xvid codec at 800Kbps and mp3 audio at 128Kbps.
mencoder clip.avi -ovc xvid -xvidencopts bitrate=800:threads=2  -oac lamemp3 -lameopts cbr:br=128 -o new-clip.avi Same as above, but uses 2 threads for the xvid encoding.
mencoder clip.avi -endpos 00:05:00 -ovc copy -oac copy -o new-clip.avi Copies the first 5 minutes from clip.avi into new-clip.avi
mencoder clip.avi -endpos 00:05:00 -ss 00:01:00 -ovc copy -oac copy -o new-clip.avi Copies 5 minutes from clip.avi into new-clip.avi, skipping the first minute.
mencoder dvb://WJBF-DT -endpos 01:0000 -ovc copy -oac copy -o ugly.betty.mpeg Tunes to WJBF-DT, records 1 hour of that station to ugly.betty.mpeg
mencoder clip.avi -vf scale=640:-2 -ovc xvid -xvidencopts bitrate=1200:threads=2 -oac copy -o new-clip.avi Resizes the video to 640 wide, keeping the aspect ration, then encoding video using xvid at 1200Kbps and copying the audio directly
mencoder tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0 -ovc lavc -o >(tee webcam-`date +%Y-%m-%d-%H.%M.%S`.avi | mplayer -cache 128) Records the webcam to “webcam-yyyy-dd-mm-hh.mm.ss.avi”, where that is the date, and display it to the screen while recording.
mencoder -idx clip.avi -ovc copy -oac copy new-clip.avi Fixes the AVI index of clip.avi, the output being new-clip.avi
mencoder clip.avi -vf cropdetect -o /dev/null Detects what cropping is needed
mencoder clip.avi -of mpeg -mpegopts format=dvd -lavcopts vcodec=mpeg2video:vbitrate=5000:acodec=ac3 -o dvd-clip.mpeg Encodes clip.avi into a dvd-compatible mpeg at 5000Kbps
mencoder -dvd 2 -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o dvd.avi Rips a DVD to dvd.avi