IPTV Record NG

Nahravání IPTV (DVB-T Getstream server) pomocí mencoderu.

Níže následuje náhled scriptu a pro úplnost, ještě jeho kopie ke stažení v zipu.

Náhled obrazovky:
dvb record ng


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

Petr Jakubec © 2010