#!/bin/bash

#debug="yes"

## Precommand, for example setting mencoder priority.
## Supernice is my own version of the 'nice' tool.
## Can be left blank.
precmd="supernice --6"

## This is where the magic is, the mencoder options.
mencopts="-tv device=/dev/v4l/video0:driver=v4l2:width=768:height=576:alsa \
tv:// \
-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1000 \
-oac mp3lame -lameopts cbr:br=64 \
-vop pp=lb,crop=720:544:24:16"

## Output file name
output=/tmp/movie/clip.`date +%F-%H-%M`.$1.avi   
#output=/tmp/movie/testi.avi

if [ -z $2 ]; then
	echo "Usage: $0 station length"
	echo "station is retrieved from Xawtv config"
	echo "length is in mencoder format:"
	echo "  '1:05:00'   1 hour 5 min"
	echo "  '4:12'      4 min 12 sec"
	echo "  '30'        30 seconds"
	echo "Example: $0 subtv 30:00"
	exit
fi

## Get the cmdline parameters.
station="$1"
length="$2"

echo "Setting station: $station"

## This little tool belongs to Xawtv.
## You can use the station names defined in the Xawtv config.
v4lctl -c /dev/v4l/video0 setstation $station 2>&1 | grep -v "dlopen"

cmd="$precmd mencoder $mencopts -o $output -endpos $length"

echo "Executing: $cmd"

if [ -z $debug ]; then
	exec $cmd > /dev/null
	exit
fi

exec $cmd

