1 #!/bin/sh
    2 # Description: Script for grabbing from getstream server (DVB-T Recording)
    3 # Author: Petr (DotaZ) Jakubec (jakupetr_at_gmailcom)
    4 # Date:  17/03/2008 23:58:11
    5 # Name: dvb_record-public.sh
    6   Version="0.11"
    7 # Destination: for all use
    8 # Licence: GPL v2 (http://www.gnu.org/licenses/gpl.txt)
    9 # Thanks: Cyriac BENOIT and him xdtv_record.sh script
   10 # Update:
   11 #		2008/07/08 14:38:55 (CEST)
   12 #		2008/11/08 15:27:21 update for new multiplex allocation, 2 multiplex, 2 cards, new getstream server v2.
   13 
   14 
   15 # Variable:
   16 path_to_snap="/path/to/video/files"
   17 server="hostname_of_stream_server"
   18 
   19 #Stating spacial character styles:
   20 bold_style="\033[1m"
   21 red_style="\E[1;31m"
   22 green_style="\033[042m"
   23 end_style="\033[0m"
   24 
   25 ## Checking for needed binaries
   26 binaries="grep awk sed cat du sleep shutdown at atq mencoder"
   27 for bin in $binaries; do
   28   $(which $bin 1>/dev/null 2>&1)
   29   if [ "$?" -eq 1 ]; then
   30     echo -e $bold_style"$bin" $end_style $red_style" not found in \$PATH. This script may not work..."  $end_style
   31     exit 1
   32   fi
   33 done
   34 
   35 ## DATA MINING ##
   36 # getting info from the user:
   37 #rec_date (date DD.MM.RR)
   38 #start_time (time)
   39 #end_time (time)
   40 #tv_station (tv_station and port)
   41 #file_label (text)
   42 #halted (yes or no)
   43 
   44 
   45 
   46 # Getting day
   47 get_rec_date () {
   48 	error="0"
   49 	while [ "$error" -eq "0" ]; do
   50 		echo -en $bold_style"Enter recording date (DD.MM.YY):" " [$(date +"%d.%m.%y")] "$end_style
   51 		read rec_date
   52 		if [ -z "$rec_date" ]; then
   53 			rec_date=$(date +"%d.%m.%y")
   54 		fi
   55 		rec_date_check=$(echo -n "$rec_date" | wc -c)
   56 		rec_date_check_dot_tmp=$(echo $rec_date | awk -F "." '{print $1}')
   57 		rec_date_check_dot=$(echo -n "$rec_date_check_dot_tmp" | wc -c)
   58 		if [ "$rec_date_check_dot" -eq "2" ] && [ "$rec_date_check" -eq "8" ]; then
   59 			error=1
   60 		else
   61 			echo -e $red_style"	Wrong date format. Must be DD.MM.RR! Now: $rec_date!"$end_style
   62 			error=0
   63 		fi
   64 	done
   65 }
   66 
   67 # Getting start time
   68 get_start_time () {
   69 	error="0"
   70 	while [ "$error" -eq "0" ]; do
   71 		echo -en $bold_style"Enter start time (HH:MM), now is $(date +"%H:%M"): "$end_style
   72 	   read start_time
   73 		start_time_check=$(echo -n "$start_time" | wc -c)
   74 		start_time_check_dot_tmp=$(echo $start_time | awk -F ":" '{print $1}')
   75 		start_time_check_dot=$(echo -n "$start_time_check_dot_tmp" | wc -c)
   76 		if [ "$start_time_check_dot" -eq "2" ] && [ "$start_time_check" -eq "5" ]; then
   77 			error=1
   78 		else
   79 			echo -e $red_style"	Wrong time format. Must be HH:MM! Now: $start_time!"$end_style
   80 			error=0
   81 		fi
   82 	done
   83 }
   84 
   85 # Getting end time
   86 get_end_time () {
   87 	error="0"
   88 	while [ "$error" -eq "0" ]; do
   89 		echo -en $bold_style"Enter end time (HH:MM): "$end_style
   90 	   read end_time
   91 		end_time_check=$(echo -n "$end_time" | wc -c)
   92 		end_time_check_dot_tmp=$(echo $end_time | awk -F ":" '{print $1}')
   93 		end_time_check_dot=$(echo -n "$end_time_check_dot_tmp" | wc -c)
   94 		if [ "$end_time_check_dot" -eq "2" ] && [ "$end_time_check" -eq "5" ]; then
   95 			error=1
   96 		else
   97 			echo -e $red_style"	Wrong time format. Must be HH:MM! Now: $end_time!"$end_style
   98 			error=0
   99 		fi
  100 	done
  101 }
  102 
  103 #Getting tv_station
  104 get_tv_station () {
  105 	error="0"
  106 	while [ "$error" -eq "0" ]; do
  107 		echo "   1) [CT1]"
  108 		echo "   2)  CT2"
  109 		echo "   3)  CT24"
  110 		echo "   4)  CT4"
  111 		echo "   5)  NOVA"
  112 		echo "   6)  PRIMA"
  113 		echo "   7)  BarrandovTV"
  114 		echo "   8)  TEST"
  115 		echo "   9)  PRIMA2"
  116 		echo -en $bold_style"Enter number of TV station: "$end_style
  117 		read tv_station_num
  118 		case $tv_station_num in
  119 			1)	tv_station="CT1";	port="8000/ct1"; error="1"; echo " $tv_station" ;;
  120 			2)	tv_station="CT2";	port="8000/ct2"; error="1"; echo " $tv_station" ;;
  121 			3)	tv_station="CT24";	port="8000/ct24"; error="1"; echo " $tv_station" ;;
  122 			4)	tv_station="CT4";	port="8000/ct4"; error="1"; echo " $tv_station" ;;
  123 			3)	tv_station="NOVA";	port="8000/nova"; error="1"; echo " $tv_station" ;;
  124 			6)	tv_station="PRIMA";	port="8000/prima"; error="1"; echo " $tv_station" ;;
  125 			7)	tv_station="BarrandovTV";	port="8000/barrandovtv"; error="1"; echo " $tv_station" ;;
  126 			8)	tv_station="TEST";	port="8000/test"; error="1"; echo " $tv_station" ;;
  127 			9)	tv_station="PRIMA2";	port="8000/prima2"; error="1"; echo " $tv_station" ;;
  128 			*) tv_station="CT1";	port="8000/ct1"; error="1"; echo " $tv_station" ; echo -e $bold_style"	Dont enter TV Station, default is CT1"$end_style
  129 		esac
  130 	done
  131 }
  132 
  133 #Getting file_label
  134 get_file_label () {
  135 	error="0"
  136 	while [ "$error" -eq "0" ]; do
  137 		echo -en $bold_style"Enter the movie label: "$end_style
  138 		read file_label_raw
  139 		# get out bizarreness or dangerous character, such as:  ) ( / ' ' ; * = : ` $ "
  140 		file_label=$(echo -n "$file_label_raw" | sed 's/\ /_/g' | sed 's/[\"\*\`\=\$\;\:\/\)\(]//g' ) && error="1" || error="0"
  141 	done
  142 }
  143 
  144 # Getting info about halt necessity
  145 get_halted () {
  146 	if [ -z "$halted" ]; then
  147 	   echo -en $bold_style"Do you wanna shut your computer down after recording ? (N/y) [N] "$end_style
  148 	   read halted
  149 		if [ "$halted" = "Y" ] || [ "$halted" = "y" ]; then
  150 			halted="shutdown -h +5"
  151 	   else
  152 	   	halted="# computer do not shutdown"
  153 	   fi
  154 	fi
  155 }
  156 
  157 ### TIME MINING FUNCTION (count record time)
  158 #	input:	start_time, end_time
  159 count_record_time () {
  160 	 # TH == time hour, TM == time minutes.
  161 	 # xx1 == startting time, xx2 == stopping time
  162 		TH1=`echo $start_time | awk -F ":" '{print $1}'`
  163 		TM1=`echo $start_time | awk -F ":" '{print $2}'`
  164 		TH2=`echo $end_time | awk -F ":" '{print $1}'`
  165 		TM2=`echo $end_time | awk -F ":" '{print $2}'`
  166 	 # digits is in pair? -> control this:
  167 		check_TH1=$(echo -n "$TH1" | wc -c)
  168 		check_TM1=$(echo -n "$TM1" | wc -c)
  169 		check_TH2=$(echo -n "$TH2" | wc -c)
  170 		check_TM2=$(echo -n "$TM2" | wc -c)
  171 
  172 		if [ $check_TH1 -ne 2 -o $check_TM1 -ne 2 ]; then
  173 			echo -e $red_style"Wrong format. Must be HH:MM"$end_style && exit 1
  174 		fi
  175 
  176 		if [ $check_TH2 -ne 2 -o $check_TM2 -ne 2 ]; then
  177 			echo -e $red_style"Wrong format. Must be HH:MM"$end_style && exit 1
  178 		fi
  179 
  180 	 # multiplied *8 and *9 in BASH invoke exception:
  181 	 #     value too great for base (error token is "09")
  182 	 # idea+ from xdvt_record.sh
  183 
  184 	function oct_to_dec () {
  185 		if [[ $1 = "08" ]] ; then
  186 			echo 8
  187 		else if [[ $1 = "09" ]] ; then
  188 				echo 9
  189 			else
  190 				echo $1
  191 			fi
  192 		fi
  193 	}
  194 
  195 	TH1=`oct_to_dec $TH1`
  196 	TH2=`oct_to_dec $TH2`
  197 	TM1=`oct_to_dec $TM1`
  198 	TM2=`oct_to_dec $TM2`
  199 
  200 	 # Adding a whole day for special cases (23:30 - 01:15)
  201 		start=$(($TH1 * 60 + $TM1))
  202 		stop=$(($TH2 * 60 + $TM2))
  203 		if [ $stop -lt $start ]; then
  204 			TH2=$(($TH2 + 24))
  205 		fi
  206 
  207 	 # other errs & ending count record time:
  208 	 # Step by Step
  209 	 #Hours=$(($TH2 * 60 - $TH1 * 60))
  210 	 #Minutes=$(($TM2 - $TM1))
  211 	 #record_time=$(($Hours + $Minutes))
  212 
  213 		record_time=$(($(($TH2 * 60 - $TH1 * 60)) + $(($TM2 - $TM1))))
  214 		if [ $record_time -lt 0 ]; then
  215 			echo -e $red_style"record_time = $record_time. Any BUG, must be ... ending!"$end_style && exit 1
  216 		fi
  217 	 # export not need? BASH have local or global variable only:
  218 	 # export Record_time=$Record_time
  219 }
  220 
  221 # Creating script for AT:
  222 create_at_script (){
  223 	# From rec_date & start_time prepare label YYYYMMDD-HHMM (20080411-1030)
  224 	date_label="`echo $rec_date | awk -F "." '{printf("20%-0s%0s%-0s\n", $3, $2, $1)}'`-`echo $start_time | sed -e "s/://g"`"
  225 	file_for_at="/usr/local/TVRecordings/at-$date_label-$tv_station--$$.sh"
  226 	# in Debian dont use /tmp for at_script save! if you want store this. it was remove after reboot. use /var/tmp.
  227 	time_for_at==`date +%Y/%m/%d\ %H:%M:%S\ \(%Z\)`
  228 	hostname_for_at=`hostname --long`
  229 	# Frame count is usually 24,94-7, but 25 is simply and function.
  230 	#-frames in mencoder is in [frame/sec]
  231 	# 60 * seconds *25 = frame_time
  232 	frame_time=$(($record_time*60*25))
  233 
  234 # This is at script content:
  235 cat >> $file_for_at <<_END_
  236 #!/bin/bash
  237 # Description: Script for TV Recording from stream server $server
  238 # Date:  $time_for_at
  239 # Version: $Version
  240 # Destination: from $hostname_for_at; to $server(server dvb-t)
  241 
  242 mencoder http://$server:$port -cache 1024 -ovc copy -oac copy -of mpeg -o $path_to_snap/$tv_station-$date_label-$file_label--$$.mpg -force-avi-aspect 1.33 -frames $frame_time -really-quiet
  243 
  244 echo 'This recording from DVBT server $server accomplished: '
  245 echo ' '
  246 echo 'Info:	$tv_station, $rec_date  $start_time-$end_time ($record_time min)'
  247 du -h $path_to_snap/$tv_station-$date_label-$file_label--$$.mpg
  248 chmod 664 $path_to_snap/$tv_station-$date_label-$file_label--$$.mpg
  249 chown petr:users $path_to_snap/$tv_station-$date_label-$file_label--$$.mpg
  250 
  251 chmod 600 $file_for_at
  252 $halted & exit
  253 _END_
  254 
  255 chmod 700 $file_for_at
  256 # in AT script: or rm -f $file_for_at as do you wish to errase old used script
  257 }
  258 
  259 create_at_task  (){
  260 	error="0"
  261 	echo -en $bold_style"AT report:  "$end_style
  262 	echo $file_for_at | at $start_time $rec_date && error="0" || error="1"
  263 	if [ "$error" -eq "1" ]; then
  264 		echo -e $red_style"Any BUG, must be ... ending! - Format of day, time is ok? It was in quote?"$end_style
  265 		rm -f $file_for_at
  266 	fi;
  267 	# list jobs:
  268 	echo -e $bold_style"List all actual jobs:  "$end_style
  269 	atq
  270 }
  271 
  272 # ---=== BEGIN ===---
  273 
  274 clear
  275 echo " "
  276 get_rec_date
  277 #	echo "rec_date = $rec_date"
  278 #	echo "$rec_date_check $rec_date_check_dot $rec_date_check_dot_tmp"
  279 get_start_time
  280 #	echo "start_time =  $start_time"
  281 #	echo " $start_time_check $start_time_check_dot $start_time_check_dot_tmp"
  282 get_end_time
  283 #	echo "end_time = $end_time"
  284 #	echo " $end_time_check $end_time_check_dot $end_time_check_dot_tmp"
  285 get_tv_station
  286 #	echo "tv_station = $tv_station (port = $port)"
  287 get_file_label
  288 #	echo "file_label = $file_label"
  289 #	echo $file_label $file_label_raw
  290 get_halted
  291 #	echo "halted = $halted"
  292 
  293 
  294 count_record_time
  295 #	echo "TH1,2 TM1,2 = $TH1, $TH2 , $TM1, $TM2"
  296 #	echo "record_time = $record_time"
  297 
  298 # correct entry?
  299 echo -en $bold_style" This entry correct is? (yY/nN): [Y]"$end_style
  300 read ok
  301 if [ "$ok" = "n" ] || [ "$ok" = "N" ]; then
  302 	echo -e $red_style"	OK, see you later (code: CUL8R)!"$end_style
  303 	exit 13
  304 fi
  305 echo " "
  306 create_at_script
  307 #	echo "$date_label, $file_for_at, $hostname_for_at, $frame_time, $sizeapprox"
  308 
  309 create_at_task
  310 
  311 # Report in CZ_cs:
  312 sizeapprox=$(($record_time * 25))
  313 echo " "
  314 echo "Report in CZ_cs: "
  315 echo -e $green_style"Nahravam $record_time min. ($frame_time frames, priblizna velikost souboru bude $sizeapprox MB)"$end_style
  316 echo -e $green_style"Nahravat budu z kanalu $tv_station (port $port) a zacinam $start_time $rec_date"$end_style
  317 if [ "$halted" = "shutdown -h +5" ]; then
  318 	echo -e $green_style"Po skonceni nahravani pocitac vypnu!"$end_style
  319 fi
  320 
  321 echo ""
  322 echo "Waiting for ENTER to restart this script"
  323 
  324 read && exit 0
  325 # ---=== EOF ===---