root/trunk/install/perl/recwrap.pl

リビジョン 97, 8.6 kB (コミッタ: sorshi, コミット時期: 14 年 前)

radiko録音対応

  • svn:executable 属性の設定値:
Line 
1 #!/usr/bin/perl
2 #usage recwrap.pl ch length(sec) [bitrate(5)] [TID] [NO] [PID] [stationid] [digitalflag] [digitalband] [digitalch]
3 #
4 # Anime recording system foltia
5 # http://www.dcc-jpl.com/soft/foltia/
6 #
7 #
8 #レコーディングラッパ
9 #atから呼び出され、tvrecordingを呼び出し録画
10 #そのあとMPEG4トラコンを呼び出す
11 #
12 # DCC-JPL Japan/foltia project
13 #
14
15 use DBI;
16 use DBD::Pg;
17 use DBD::SQLite;
18 use Schedule::At;
19 use Time::Local;
20 use Jcode;
21
22 $path = $0;
23 $path =~ s/recwrap.pl$//i;
24 if ($path ne "./"){
25 push( @INC, "$path");
26 }
27
28 require "foltialib.pl";
29 #引き数がアルか?
30 $recch = $ARGV[0] ;
31 if ($recch eq "" ){
32         #引き数なしで実行されたら、終了
33         print "usage recwrap.pl  ch length(sec) [bitrate(5)] [TID] [NO] [PID]\n";
34         exit;
35 }
36
37 $recch = $ARGV[0] ;
38 $reclength = $ARGV[1] ;
39 $bitrate  = $ARGV[2] ;
40 $tid  = $ARGV[3] ;
41 $countno  = $ARGV[4] ;
42 $pid = $ARGV[5] ;
43 $stationid = $ARGV[6] ;
44 $usedigital = $ARGV[7] ;
45 $digitalstationband = $ARGV[8] ;
46 $digitalch= $ARGV[9] ;
47
48 #DB初期化
49 $dbh = DBI->connect($DSN,$DBUser,$DBPass) ||die $DBI::error;;
50
51
52 if ($usedigital == 1){
53         $extension = ".m2t";#TSの拡張子
54 }else{
55         $extension = ".m2p";#MPEG2の拡張子
56 }
57 if ($recch == -2 ){ #ラジオ局
58         $extension = ".aac";#MPEG2の拡張子
59 }
60
61 $outputfile = strftime("%Y%m%d-%H%M", localtime(time + 60));
62 chomp($outputfile);
63
64 if ($tid == 0){
65                 $outputfilename = "0--".$outputfile."-".$recch.$extension;
66                 $mp4newstylefilename = "-0--".$outputfile."-".$recch;
67 }else{
68         if ($countno == 0){
69                 $outputfilename = $tid ."--".$outputfile.$extension;
70                 $mp4newstylefilename = "-" . $tid ."--".$outputfile;
71         }else{
72                 $outputfilename = $tid ."-".$countno."-".$outputfile.$extension;
73                 $mp4newstylefilename = "-" . $tid ."-".$countno."-".$outputfile;
74         }
75 }
76
77 if ($recch == -2 ){ #ラジオ局
78 # stationIDからradiko識別子を取得
79 $sth = $dbh->prepare($stmt{'recwrap.8'});
80 $sth->execute($stationid);
81  @stationline= $sth->fetchrow_array;
82 $radikostationname = $stationline[3];
83
84 $oserr = system("$toolpath/perl/digitalradiorecording.pl $radikostationname $reclength $outputfilename");
85 $oserr = $oserr / 256;
86 &writelog("recwrap DEBUG radiko rec finished. $oserr");
87
88 # aacファイル名をfoltia_subtitlePIDレコードに書き込み
89 $sth = $dbh->prepare($stmt{'recwrap.1'});
90 $sth->execute($outputfilename, $pid);
91 &writelog("recwrap DEBUG UPDATEDB $stmt{'recwrap.1'}");
92 &changefilestatus($pid,$FILESTATUSTRANSCODEMP4BOX);
93
94 # aacファイル名をfoltia_m2pfilesPIDレコードに書き込み
95 $sth = $dbh->prepare($stmt{'recwrap.2'});
96 $sth->execute($outputfilename);
97 &writelog("recwrap DEBUG UPDATEDB $stmt{'recwrap.2'}");
98
99
100 }else{#非ラジオ局なら
101
102 if ($usedigital == 1){
103 #デジタルなら
104 &writelog("recwrap RECSTART DIGITAL $digitalstationband $digitalch $reclength $stationid 0 $outputfilename $tid $countno friio");
105 #録画
106     $starttime = time();
107 $oserr = system("$toolpath/perl/digitaltvrecording.pl $digitalstationband $digitalch $reclength $stationid 0 $outputfilename $tid $countno friio");
108 $oserr = $oserr / 256;
109
110 if ($oserr == 1){
111         &writelog("recwrap ABORT recfile exist. [$outputfilename] $digitalstationband $digitalch $reclength $stationid 0  $outputfilename $tid $countno");
112         exit;
113 }elsif ($oserr == 2){
114         &writelog("recwrap ERR 2:friio busy;retry.");
115         &continuousrecordingcheck;#もうすぐ終わる番組をkill
116         sleep(2);
117         $oserr = system("$toolpath/perl/digitaltvrecording.pl $digitalstationband $digitalch $reclength $stationid N $outputfilename $tid $countno friio");
118         $oserr = $oserr / 256;
119         if ($oserr == 2){
120         &writelog("recwrap ERR 2:friio busy;Giving up digital recording.");
121         }
122 }elsif ($oserr == 3){
123 &writelog("recwrap ABORT:ERR 3");
124 exit ;
125 }
126 }else{ # NOT $usedigital == 1
127 #リモコン操作
128 # $haveirdaunit = 1;リモコンつないでるかどうか確認
129 if ($haveirdaunit == 1){
130 # 録画チャンネルが0なら
131         if ($recch == 0){
132 # &つけて非同期でchangestbch.pl呼び出し
133         &writelog("recwrap Call Change STB CH :$pid");
134         system ("$toolpath/perl/changestbch.pl $pid &");
135         }#end if
136 }#end if
137
138 if($recch == -10){
139 #非受信局なら
140         &writelog("recwrap Not recordable channel;exit:PID $pid");
141         exit;
142         }#end if
143 # アナログ録画
144 &writelog("recwrap RECSTART $recch $reclength 0 $outputfilename $bitrate $tid $countno $pid $usedigital $digitalstationband $digitalch");
145
146 #録画
147 #system("$toolpath/perl/tvrecording.pl $recch $reclength 0 $outputfile $bitrate $tid $countno");
148     $starttime = time();
149
150 $oserr = system("$toolpath/perl/tvrecording.pl $recch $reclength 0 $outputfilename $bitrate $tid $countno");
151 $oserr = $oserr / 256;
152 if ($oserr == 1){
153         &writelog("recwrap ABORT recfile exist. [$outputfilename] $recch $reclength 0 0 $bitrate $tid $countno $pid");
154         exit;
155 }
156
157 }#endif #デジタル優先フラグ
158
159 #デバイスビジーで即死してないか検出
160 $now = time();
161         if ($now < $starttime + 100){ #録画プロセス起動してから100秒以内に戻ってきてたら
162     $retrycounter = 0;
163                 while($now < $starttime + 100){
164                         if($retrycounter >= 5){
165                                 &writelog("recwrap WARNING  Giving up recording.");
166                                 last;
167                         }
168                 &writelog("recwrap retry recording $now $starttime");
169                 #アナログ録画
170         $starttime = time();
171 if($outputfilename =~ /.m2t$/){
172         $outputfilename =~ s/.m2t$/.m2p/;
173 }
174 $oserr = system("$toolpath/perl/tvrecording.pl $recch $reclength N $outputfilename $bitrate $tid $countno");
175         $now = time();
176 $oserr = $oserr / 256;
177                         if ($oserr == 1){
178                                 &writelog("recwrap ABORT recfile exist. in resume process.[$outputfilename] $recch $reclength 0 0 $bitrate $tid $countno $pid");
179                                 exit;
180                         }# if
181                 $retrycounter++;
182                 }# while
183         } # if
184
185         &writelog("recwrap RECEND [$outputfilename] $recch $reclength 0 0 $bitrate $tid $countno $pid");
186
187
188 # m2pファイル名をPIDレコードに書き込み
189 $sth = $dbh->prepare($stmt{'recwrap.1'});
190 $sth->execute($outputfilename, $pid);
191 &writelog("recwrap DEBUG UPDATEDB $stmt{'recwrap.1'}");
192 &changefilestatus($pid,$FILESTATUSRECEND);
193
194 # m2pファイル名をPIDレコードに書き込み
195 $sth = $dbh->prepare($stmt{'recwrap.2'});
196 $sth->execute($outputfilename);
197 &writelog("recwrap DEBUG UPDATEDB $stmt{'recwrap.2'}");
198
199 # Starlight breaker向けキャプチャ画像作成
200 if (-e "$toolpath/perl/captureimagemaker.pl"){
201         &writelog("recwrap Call captureimagemaker $outputfilename");
202 &changefilestatus($pid,$FILESTATUSCAPTURE);
203         system ("$toolpath/perl/captureimagemaker.pl $outputfilename");
204 &changefilestatus($pid,$FILESTATUSCAPEND);
205 }
206 }#非ラジオ局
207
208 # MPEG4 ------------------------------------------------------
209 #MPEG4トラコン必要かどうか
210 $sth = $dbh->prepare($stmt{'recwrap.3'});
211 $sth->execute($tid);
212  @psptrcn= $sth->fetchrow_array;
213 if ($psptrcn[0]  == 1 ){#トラコン番組
214         &writelog("recwrap Launch ipodtranscode.pl");
215         exec ("$toolpath/perl/ipodtranscode.pl");
216         exit;
217 }#PSPトラコンあり
218
219 sub continuousrecordingcheck(){
220     my $now = time() + 60 * 2;
221 &writelog("recwrap DEBUG continuousrecordingcheck() now $now");
222 my @processes =`ps ax | grep recfriio`;
223
224 my $psline = "";
225 my @processline = "";
226 my $pid = "";
227 my @pid;
228 my $sth;
229 foreach (@processes){
230         if (/friiodetect/) {
231                 if (/^.[0-9]*\s/){
232                         push(@pid, $&);
233                 }#if
234         }#if
235 }#foreach
236
237 if (@pid > 0){
238 my @filenameparts;
239 my $tid = "";
240 my $startdate = "";
241 my $starttime = "";
242 my $startdatetime = "";
243 my @recfile;
244 my $endtime = "";
245 my $endtimeepoch = "";
246 foreach $pid (@pid){
247 #print "DEBUG  PID $pid\n";
248 &writelog("recwrap DEBUG continuousrecordingcheck() PID $pid");
249
250         my @lsofoutput = `/usr/sbin/lsof -p $pid`;
251         my $filename = "";
252         #print "recfolferpath $recfolderpath\n";
253         foreach (@lsofoutput){
254                 if (/m2t/){
255                 @processline = split(/\s+/,$_);
256                 $filename = $processline[8];
257                 $filename =~ s/$recfolderpath\///;
258                 &writelog("recwrap DEBUG continuousrecordingcheck()  FILENAME $filename");
259                 # 1520-9-20081201-0230.m2t
260                 @filenameparts = split(/-/,$filename);
261                 $tid = $filenameparts[0];
262                 $startdate = $filenameparts[2];
263                 $starttime = $filenameparts[3];
264                 @filenameparts = split(/\./,$starttime);
265                 $startdatetime = $startdate.$filenameparts[0];
266                 #DBから録画中番組のデータ探す
267                     &writelog("recwrap DEBUG continuousrecordingcheck() $stmt{'recwrap.7'}");
268                     $sth = $dbh->prepare($stmt{'recwrap.7'});
269         &writelog("recwrap DEBUG continuousrecordingcheck() prepare");
270                     $sth->execute($tid, $startdatetime);
271         &writelog("recwrap DEBUG continuousrecordingcheck() execute");
272         @recfile = $sth->fetchrow_array;
273         &writelog("recwrap DEBUG continuousrecordingcheck() @recfile  $recfile[0] $recfile[1] $recfile[2] $recfile[3] $recfile[4] $recfile[5] $recfile[6] $recfile[7] $recfile[8] $recfile[9] ");
274         #終了時刻
275         $endtime = $recfile[4];
276         $endtimeepoch = &foldate2epoch($endtime);
277         &writelog("recwrap DEBUG continuousrecordingcheck() $recfile[0] $recfile[1] $recfile[2] $recfile[3] $recfile[4] $recfile[5] endtimeepoch $endtimeepoch");
278         if ($endtimeepoch < $now){#まもなく終わる番組なら
279                 #kill
280                 system("kill $pid");
281                 &writelog("recwrap recording process killed $pid/$endtimeepoch/$now");
282         }
283                 }#endif m2t
284         }#foreach lsofoutput
285 }#foreach
286 }else{
287 #print "DEBUG fecfriio NO PID\n";
288 &writelog("recwrap No recording process killed.");
289 }
290 }#endsub
291
292
293
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。
track feed