Nshrávání z IPTV

Princip:
Skript v bashi, který konvertuje parametry příkazu na zadání nahrávání pomocí mencoderu pro daemon at. Vstupem jsou dva časové údaje, počáteční čas a datum pro daemon at a konečný čas, pak je to číslo kanálu a případný požadavek na vypnutí stroje.

Vysílání je realizováno pomocí getstream-poempel (klon getstreamu), což je velice nenáročné řešení sremingu multimédií (spotřebováná mezi 0,1-2% sys. zdrojů, snadná konfigurace ). Navíc zachytáváním z tohoto serveru se pak neblokuje kanál DVB-T karty a je tedy možné se zachytávat či sledovat (případně obojí) v rámci multiplexu další programy. Mé skripty a poznámky ohledně provozu getstream-poempel jsou součástí balíčku ke stažení.

Realizace zachytávání pomocí mencoderu se osvědčuje pro jeho nenáročnost, konfigurovatelnost a přirozenou snahu stream poopravovat. Jako zcela nevhodné se však ukázalo zachytávání do kontejneru AVI, proto je použit MPEG. Pro jeho další zpracování lze použít Gopchop, DVBCut, ProjectX , případně Avidemux.

Dalšími možnostmi pro zachytávání je použít třeba wget nebo vlc a pro streaming vls.

Pakliže Vás napdane nějaké vylepšení tohoto řešení či naleznete nejakou chybu, přijmám nápady, patche či jakoukoliv jinou pomoc.

Kontakt: dvb_record zavináč jakubcovi tečka info. nebo vizte kontakt

Přímo zde vidíte náhled scriptu a ještě jeho kopie ke stažení v gzipu. (Včetně getstream-poempel)

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