#!/bin/bash ##################################################################################### # /usr/local/bin/save_dvb 20080608rm revised to check that duration of save is is set # either as minutes or hours. # # An interactive script for saving DVB broadcasts using dvbstream. Option is # provided to save immediately or schedule to save at a later time/date using atd. # # It requires an accessable channels.conf as generated by DVB-Utils' scan which # it will parse for station names, frequencies, PID's, etc, # # This script will ignore any lines in channels.conf which are commented out. # # Modify the variable "chnl_conf" below to point towards the channels.conf you want # to use. Modify variable "save_dir" to point to a designated partition/directory you # will be saving into. # # Option is provided to save (record) now or to schedule (to record) sometime in # the future. Modify the variable "sched_show" to point to where the temporary # scheduling script will go # # Scheduling is via atd so see manual for correct time and date usage. Duration of # the save uses sleep so you need to suffix with "m" for minutes, "h" for hours, etc. # # Be sure to set the variables in lines 36-39 to suit or this script won't work. # # This script is GPL. Use as you wish but don't blame me for your problems. # Send comments or constructive criticism to: rick[dot]miles[at]turtlespond[dot]net ###################################################################################### # The command "date" will execute if sent to the script ~/.dvb_at_file with "echo". # So "awk" is used on the following line to create variable $timestamp in the script # $sched_show. Edit the timestamp command below to suit but do not un-comment it. # Timestamp uses: `date +%d-%b-%R` # Declare variables; modify to suit. chnl_conf=/etc/channels.conf # Where channels.conf with comments (#) is kept. #save_dir=/videos # Default save direcory. save_dir=/multimedia sched_show=~/.sched_show # Nominate the directory and temporary script name run by at. I use ~/.sched_show script_path=/usr/local/bin/save-dvb #This script needs to know where it is clear # Make some room get_channel() # Function used for channels selection { echo -e "\033[1m \n\tEnter the number of the station you want to record:\n" # "\033[1m" is used to make stdout bold. # sed filters out comments, awk adds line number and prints first column seperated with ":" sed '/#/d' $chnl_conf | awk -F ":" '{print("\t\t\t", NR, "\t" $1)}' echo -e "\n\t\t\t \c" # get answer, assign it to response_0 read response_0 # assign line number $response_0 to variable $channel # sed filters out comments, nl numbers line , grep prints only line number $response_0 to stdout channel=`sed '/#/d' $chnl_conf | nl -b a -n ln | grep "^[$response_0]"` #echo "TEST -- $channel" # Comment out when I know it works } parse_variables() # Assign the variables required by dvbstream from a selected line in channels.conf. { # There is probably more than one way to parse any of these variables, I make no excuses I used the first thing that worked. qam=`echo "$channel" | awk -F ":" '{ print $7 }' | tr -d QAM_` coder=`echo "$channel" | awk -F ":" '{ print $5 }' | sed -s 's/FEC_//'` guardi=`echo "$channel" | awk -F ":" '{ print $9 }' | sed -e 's/GUARD_INTERVAL_1_//'` bandw=`echo "$channel" | awk -F ":" '{ print $4 }' | sed -e 's/BANDWIDTH_//' | sed -e 's/_MHZ//'` tranmod=`echo "$channel" | awk -F ":" '{ print $8 }' | sed -e 's/TRANSMISSION_MODE_//' | sed -e 's/K//'` freq=`echo "$channel" | awk -F ":" '{ print $2 }' | cut -c1-6` pid=`echo "$channel" | awk -F ":" '{ print $11, $12 }'` } get_duration() # Function used to get how long to record dvb broacast. { while [ TRUE ]; do clear echo -e "\n\n\tEnter how long to record for as minutes (m) or" echo -e "\thours (h), e.g 90m or 1.5h:" echo -e "\n\t\t\t\c" read response_1 echo "$response_1" | grep m > /dev/null 2>&1 test_result_1=$? if [ $test_result_1 -eq 0 ]; then duration=$response_1 break fi echo "$response_1" | grep h > /dev/null 2>&1 test_result_2=$? if [ $test_result_2 -eq 0 ]; then duration=$response_1 break fi if [ $test_result_1 -o $test_result_2 -eq 1 ]; then echo -e "\n\tYou must use \"m\" for minutes or \"h\" for hours after the number." echo -e "\n\t\t\t\c" sleep 4 fi done } get_name () # Get a name for the recorded DVB broadcast { echo -e "\n\n\tEnter a name for your recording. The day and" echo -e "\ttime will be appended to your name:" echo -e "\n\t\t\t\c" read response_2 name=$response_2 } get_dir() # Confirm default save directory and give option to save to a different directory. { echo -e "\n\n\tThe default save directory is $save_dir Hit" echo -e "\t\"Enter\" to save to $save_dir or enter the" echo -e "\tpath to a different directory." echo -e "\n\t\t\t\c" read response_3 if [ $response_3 ]; then path=$response_3 else path="$save_dir" fi } display_select() # Display recording variables and select to save now or later sed -n "\$start"p { while true; do clear echo -e "\n\tHere are your selections:\n" echo -e "\tSaving from:\t`echo "$channel" | awk -F ":" '{ print $1 }'`" echo -e "\tRecording to:\t$path/$name-[timestamp].mpg" echo -e "\tRun time:\t$duration" sleep 2 echo -e "\n\n\tTo schedule saving at a later time hit \"s\". \c" sleep 1 echo -e "\n\tTo begin recording immediately hit \"n\". \c" sleep 1 echo -e "\n\tTo quit this script and start again hit \"q\"." echo -e "\n\t\t\t\c" read response_4 echo -e "\n" case $response_4 in S)when="later" break;; s)when="later" break;; N)when"now" break;; n)when="now" break;; Q)when="quit" break;; q)when="quit" break;; esac done } save_now() # Use this function when saving immediately { tput sgr0 #revert from bold to normal console display settings echo -e "#!/bin/bash" > $sched_show echo -e "##########################################################" >> $sched_show echo -e "# Created on: $(date)" >> $sched_show echo -e "# This temporary script created when saving dvb shows" >> $sched_show echo -e "# with the script dvb-save. This script is overwritten" >> $sched_show echo -e "# every time dvb-save is run and another dvb-t show is" >> $sched_show echo -e "# scheduled to be saved" >> $sched_show echo -e "##########################################################n" >> $sched_show echo -e "\n# Declare variables" >> $sched_show echo -e "path=$path" >> $sched_show echo -e "name=$name" >> $sched_show echo -e "duration=$duration" >> $sched_show grep -m 1 '# Timestamp*' $script_path | awk '{print "timestamp=" $4 " " $5 }' >> $sched_show echo -e "\n# Begin giving commands" >> $sched_show echo "fuser -k /dev/dvb/adapter0/frontend0" >> $sched_show echo -e "dvbstream -qam $qam -cr $coder -gi $guardi -bw $bandw -tm $tranmod -f $freq $pid -ps -o > \$path/\$name-\$timestamp.mpg &" >> $sched_show echo -e "dvbstream_pid=\$!" >> $sched_show echo -e "sleep \$duration" >> $sched_show echo -e "kill \$dvbstream_pid" >> $sched_show chmod +x $sched_show # Make sure the script is executable. $sched_show & # Run the script now. #sleep 2 # This creates a delay. Otherewise the script using this function will not end on a command prompt } save_later() # Use this function when scheduling to save. { clear echo -e "\n\n\tIt is now `date +%A-%B-%R`.\n" echo -e "\tEnter when you want to save the DVB show. within" echo -e "\tthe next 24 hours as hour:minute, e.g 23:30. See" echo -e "\t\"man at\" for other time and format options." echo -e "\n\t\t\t\c" read response_5 save_time=$response_5 echo -e "\n\n\n" tput sgr0 # Revert from bold to normal display settings # The following will make up a temporary script to be run by at. echo -e "#!/bin/bash" > $sched_show echo -e "##########################################################" >> $sched_show echo -e "# Created on: $(date)" >> $sched_show echo -e "# This temporary script created when saving dvb shows" >> $sched_show echo -e "# with the script dvb-save. This script is overwritten" >> $sched_show echo -e "# every time dvb-save is run and another dvb-t show is" >> $sched_show echo -e "# scheduled to be saved" >> $sched_show echo -e "##########################################################n" >> $sched_show echo -e "\n# Declare variables" >> $sched_show echo -e "path=$path" >> $sched_show echo -e "name=$name" >> $sched_show echo -e "duration=$duration" >> $sched_show grep -m 1 '# Timestamp*' $script_path | awk '{print "timestamp=" $4 " " $5 }' >> $sched_show echo -e "\n# Begin giving commands" >> $sched_show echo "fuser -k /dev/dvb/adapter0/frontend0" >> $sched_show echo -e "dvbstream -qam $qam -cr $coder -gi $guardi -bw $bandw -tm $tranmod -f $freq $pid -ps -o > \$path/\$name-\$timestamp.mpg &" >> $sched_show echo -e "dvbstream_pid=\$!" >> $sched_show echo -e "sleep \$duration" >> $sched_show echo -e "kill \$dvbstream_pid" >> $sched_show chmod +x $sched_show # Make sure the script is executable. at $save_time -f $sched_show & # Use at to run the script at the nominated time. sleep 2 # This creates a delay. Otherewise the script using this function will not end on a command prompt } # Begin main get_channel parse_variables get_duration get_name get_dir display_select case $when in now) save_now;; later) save_later;; esac #End of Script