Example script to schedule recordings

From IVTV

Jump to: navigation, search

Contents

Shell Script

This script will wait one hour, record from /dev/video0 for thirty minutes, then exit.

Create a file containing the text below and save it as script.sh, then set it to executable using chmod a+x script.sh

#!/bin/bash
sleep 1h # Instead of using sleep here, you can start the script by using
# the at command, see 'man at'.

cat /dev/video0 > /home/mythtv/video/myvideo.mpg &

CAT_PID=$!
# $! is PID of last job running in background.

sleep 30m
# You should add some check here to make sure cat is still running,
# otherwise you might accidentally kill some other process.
kill $CAT_PID

Another Shell Script

This script is a little more sophisticated than the one above as it lets you actually schedule a recording in the future. It requires you have the 'at' command .

#!/bin/bash

function usage () {
    echo "Usage: `basename $0` -c channel -n \"showname\" -s start_time [-t] [-e end_time |-d duration]"
}

RECTIME="?"
ENDTIME="?"
TIMEFORMAT=""

while getopts c:d:n:s:e:h:t o
do 	case "$o" in
	c) CHANNEL="$OPTARG";;
	d) RECTIME="$OPTARG";;
	n) SHOWNAME="$OPTARG";;
	s) STARTTIME="$OPTARG";;
	e) ENDTIME="$OPTARG";;
	t) TIMEFORMAT="-t";;
	h) usage
	exit 1;;
	esac
done

if [ $# == 0 ] ; then
	usage
	exit 1
fi

mkdir /var/run/tvrecord 2>/dev/null

if [ $ENDTIME != "" ] && [ $ENDTIME != "?" ] ; then

at $TIMEFORMAT $STARTTIME << EoF
ivtv-tune -x $CHANNEL
cat /dev/video >"$SHOWNAME".mpg & 
echo \$! >/var/run/tvrecord/$$.pid
EoF

at $TIMEFORMAT $ENDTIME << EOF
kill \`cat /var/run/tvrecord/$$.pid\`
rm /var/run/tvrecord/$$.pid
EOF

else

at $TIMEFORMAT $STARTTIME << EoF
ivtv-tune -x $CHANNEL
cat /dev/video >"$SHOWNAME".mpg & 
sleep $RECTIME 
kill \$!
EoF

fi

Save this somewhere in your path as 'tvrecord' and make it executable with a 'chmod 755 tvrecord'. Now you can schedule a recording by doing something like:

tvrecord -c MTV -d 60 -n myShow.mpg -s "7:00 pm"

This schedules a recording of channel MTV for 60 minutes starting at 7:00 pm, saved as myShow.mpg. You can get fancy with what you put into start_time_specification, for example "10am Jul 31", "1pm tomorrow" or "now + 5 minutes". Check the manual page for 'at' for more details.

The script is not bulletproof of course, it's just something quick for those of us who haven't yet had the time to set up a proper MythTV installation.



modified version by Ingo on 2007-03-27 ------------

> The script is not bulletproof

That's why I made a few minor adjustments to overcome some potential problems:

a) 'mkdir /var/run/tvrecord' and storing PID there failed (and thus the script), because on my openSUSE 10.2 a user has no write access to /var/run. So either create the subdir tvrecord manually as root and change owner:group to match your setup. Other solution is to use /var/temp/tvrecord.

b) 'at' not just accepts time like hh:mm, but also more complex arguments including white spaces. This requires to add some more double-quotes in the script - and also on the command line: example "19:33 tomorrow"

c) output from tuning (signal detected) will cause a mail to you in /var/spool/mail/<username>. To avoid this, I directed the output to /dev/null

d) hardcoded the target directory, so you do not have to enter it every time (by default the mpg goes to the directory from where it was called. You do nut have to give a suffix .mpg, it is added by the script.

e) please note the script relies on a proper channel list as ~/.xawtv (syntax is as with old 'xawtvrc')

Here my modified version (please consider that I am quite new with Linux, for sure it can be done better:-)

#!/bin/bash

function usage () {
    echo "Usage: `basename $0` -c channel -n \"showname\" -s \"start_time\" [-t] [-e \"end_time\" |-d duration]"
    echo "Time format  (at): \"hh:mm [dd.mm[.yy]]\" (quote if string contains spaces)"
}

RECTIME="?"
ENDTIME="?"
TIMEFORMAT=""

while getopts c:d:n:s:e:h:t o
do 	case "$o" in
	c) CHANNEL="$OPTARG";;
	d) RECTIME="$OPTARG";;
	n) SHOWNAME="$OPTARG";;
	s) STARTTIME="$OPTARG";;
	e) ENDTIME="$OPTARG";;
	t) TIMEFORMAT="-t";;
	h) usage
	exit 1;;
	esac
done

if [ $# == 0 ] ; then
	usage
	exit 1
fi

# mkdir /var/run/tvrecord 2>/dev/null

if [ "$ENDTIME" != "" ] && [ "$ENDTIME" != "?" ] ; then

at $TIMEFORMAT "$STARTTIME" << EoF
ivtv-tune -x $CHANNEL >/dev/null
cat /dev/video0 >/home/ingo/video/tv/"$SHOWNAME".mpg & 
echo \$! >/var/run/tvrecord/$$.pid
EoF

at $TIMEFORMAT "$ENDTIME" << EoF
kill \`cat /var/run/tvrecord/$$.pid\`
rm /var/run/tvrecord/$$.pid
EoF

else

at $TIMEFORMAT "$STARTTIME" << EoF
ivtv-tune -x $CHANNEL >/dev/null
cat /dev/video >/home/ingo/video/tv/"$SHOWNAME".mpg &
sleep $RECTIME
kill \$!
EoF

fi

If you search for documentation, look at the man pages for 'at' and 'sleep'. It would be great, if someone would be able to make a script or small application, which just directs the output from /dev/video into a fifo-buffer with fixed filename on a harddisk with reasonable/adjustable size (50 MB/min). Output may be go into /dev/null. So even while recording from the TV-card, one could watch with i.e. Kaffeine in limited time shift. Works fine here, but the mpg file only grows and grows, while a buffer for some 10 minutes (500 MB) would do.

I am definitely not going to install MythTV, just to watch some TV on my box, or for the few recordings my wife requests from time to time. So a quite basic tool or even script will do.

Please enjoy,

A runit service

Scripts by Andras Korn. GPL.

#!/bin/zsh
#
# Uses a list of shows to record of the format:
#
# YYYY-MM-DD HH:SS;name-of-channel;name-of-show;YYYY-MM-DD HH:SS;destinationdir
#
# The fields are separated by semicolons and mean, respectively:
# 1. start date and time
# 2. name of channel to record from (uses a separate script to switch channels)
# 3. name of the show to record (used to construct the filename of the .mpg)
# 4. end date and time
# 5. destination directory where the mpg file should be created
#
# E.g.:
# 2006-04-11 20:55;rtlklub;Veszhelyzet20060411;2006-04-11 22:05;/tmp
#
# The script works by checking periodically whether one of the shows that need
# to be recorded starts within 30 seconds; if so, it waits until the specified time
# and records it. If not, it goes back to sleep.

exec 2>&1

export HOME=/home/tvrecord
export USERNAME=tvrecord
CONFIG=/home/tvrecord/shows.txt

OLDIFS="$IFS"
IFS=';'

setopt nobgnice

while true; do

        DATE="$(date '+%Y-%m-%d %H:%M')"

        sort $CONFIG | while read begintime channel show endtime dir; do
                if [[ "$begintime" > "$DATE" ]]; then
                        beginsecs=$(date --date "$begintime" '+%s')
                        endsecs=$(date --date "$endtime" '+%s')
                        if [[ "$beginsecs" -lt "$endsecs" ]]; then
                                secsnow=$(date '+%s')
                                diff=$[beginsecs-secsnow]
                                if [[ $diff -lt 30 ]]; then
                                        echo "Info: Will start recording "$show" at $begintime in $diff seconds."
                                        IFS="$OLDIFS" tvchannel "$channel"
                                        length=$[endsecs-beginsecs]
                                        sleep $diff
                                        if ! cd "$dir"; then
                                                echo "Warning: Could not change to ${dir}. Recording to /tmp."
                                                cd /tmp
                                                dir=/tmp
                                        fi
                                        rude -f tryto -t $length -k $[length+5] -n 1 -v -P \
                                                sudo -u tvrecord \
                                                dd if=/dev/video0 of="$show".mpg &
                                        ddpid=$!
                                        echo "Info: recording $show from $begintime to $endtime to $dir/${show}.mpg in progress."
                                        wait $ddpid
                                        echo "Info: Done recording $show".
                                else
                                        echo "Info: we still have $diff seconds until $show begins at $begintime, so we don't need to record just yet."
                                fi
                        else
                                echo "Error: $endtime seems to be before $begintime."
                        fi
                else
                        echo "Info: $begintime is past, so we don't need to record $show anymore."
                fi
        done

        sleep 30

done

The script depends on tryto from the socklog package and on zsh (although it could probably be easily converted into a bash script). The idea is to have it run constantly, supervised by runit, but you could get away with daemon, or just starting it in a terminal. Using rude to get realtime priority for the dd process is optional. rude comes with rtutils; I guess schedutils also provides something similar.

You need to create a tvrecord user that will own the recorded shows; or you could modify the script to use your own user, if yours is a single-user desktop system.

The tvchannel script is as simple as:

#!/bin/sh
exec ivtv-tune -f $(grep -i "^$@," /etc/ivtv/channels | cut -d, -f2)

It's a separate script because I use it to switch channels from the command line.

The /etc/ivtv/channels file lists "channelname,frequency" pairs like this:

BBC World,623.25
BBC,623.25
MTV Europe,695.25
MTV,695.25
Hallmark,703.25
Travel,687.25
Travel Channel,687.25
TravelChannel,687.25

A channel can have many different names.


Combine a script to Maxemumtvguide to record your favorites

Here is a simple way to record tv programs without installing MythTV. Maxemumtvguide is quite simple and easy to install on major distributions.

1. Install Maxemumtvguide : [1]

2. Once Maxemumtvguide is installed (don't forget to get your tvguide.xml), go to "Preferences" and "Updates". Indicate your tvguide.xml file location (example: /home/your_user_name/.xmltv/tvguide.xml), and your Update command (in my case, for Japan: tv_grab_jp --output /home/your_folder_for_tv_guide/tvguide.xml ; just adapt it to your country), then activate "Auto Update".

3. Still in Maxemumtvguide, add the desired tv programs to your favorites. (In the above script, I give an example with 2 tv programs, to make things simple).

4. Create a folder named "recordings".

5. Now, the script. Create a file named "recordings.sh" and make it executable. In this file, we put the essential stuff for recording.

Here is an example, with 2 recordings. You just have to adapt it to your environment.

#!/bin/bash
prog1=1600
canalprog1=1
prog2=0730
canalprog2=12
hp=`date +%H%M`
if [ $hp == $prog1 ]
then
ivtv-tune -t japan-bcast -c $canalprog1 
cat /dev/video0 > /home/your_user_name/recordings/the_name_of_the_first_favorite1.$hp &
CAT_PID=$!
sleep 45m
kill -15 $CAT_PID
fi
if [ $hp == $prog2 ]
then
ivtv-tune -t japan-bcast -c $canalprog2
cat /dev/video0 > /home/your_user_name/recordings/the_name_of_the_second_favorite.$hp &
CAT_PID=$!
sleep 58m
kill -15 $CAT_PID
fi

Explanation: In Maxemumtvguide, I selected as favorites

- a 45 minutes tv program that starts at 16:00 on channel 1 - a 58 minutes tv program that starts at 7:30 on channel 12

Of course, you can add as many programs as you wish.

6. Go back to Maxemumtvguide, in Preferences - Reminders. Set it to "Execute 0 minute(s) before. And finally, select "Execute command" and specify the path to your "recordings.sh" file.

That's it ! Your recordings.sh script will be executed by Maxemumtvguide when one of your favorite programs starts if there's a corresponding entry in your recordings.sh script. After that you just have to adapt your script (delete/modify/add entries) to match your favorites.

Don't forget to quit and reopen Maxemumtvguide after you make any change (favorites or preferences), otherwise the changes will be ignored when you reboot or start a new session.

Alain Note: I'm afraid I won't be able to answer technical questions since this script was created by a friend (I'm a newbie on Linux).

A Simple Timed Shell Script

This simple shell script allows you to set a time for recording in the future using the at command:

#!/bin/bash
cat /dev/video0 > TVRecording.mpg &
CATPID = $!
sleep 3600
kill $(pgrep cat)
echo "Recording complete, process $CATPID terminated"

Let's call this script tvrec60m.sh. You can change the channel using ivtv-tune in a terminal window:

ivtv-tune -c 51 -d /dev/video0

Change 51 to the number of the channel you want to record. Then, set the time you want recording to start by using the at command:

at -f tvrec60m.sh 10:00 pm

This will begin the recording Channel 51 at 10:00 pm, record for an hour, and stop. The recording length can be changed in the script (line 4). The number following "sleep" is the recording length in seconds.

External Links

timed-copy 
Perl script, accepts argument to specify how long to run.
tvpvrd 
Unix/Linux TV Video Recorder daemon/server to manage list of recordings and automatic transcoding using 'ffmpeg'.
Personal tools
development