root/trunk/install/perl/tvrecording.pl

リビジョン 83, 10.2 kB (コミッタ: sorshi, コミット時期: 16 年 前)

foltiaHD最初のリリース。
デジタル録画はFriio/Friio BS/CSのみに対応。
ivtvは1.0系使用になりました。

  • svn:executable 属性の設定値:
Line 
1 #!/usr/bin/perl
2 #
3 # Anime recording system foltia
4 # http://www.dcc-jpl.com/soft/foltia/
5 #
6 #tvrecording.pl
7 # record-v4l2.plを呼びだす録画モジュール。
8 #
9 #usage tvrecording.pl ch length(sec) [clip No(000-)] [filename] [bitrate(5)] [TID] [NO] [/dev/video0]
10 #引数
11 #ch :録画チャンネル 0だとS入力、-1だとコンポジット入力 [必須項目]
12 #length(sec) :録画秒数 [必須項目]
13 #[sleeptype] :0かN Nならスリープなしで録画
14 #[filename] :出力ファイル名
15 #[bitrate(5)] :ビットレート Mbps単位で指定
16 #[TID] :しょぼかるタイトルID
17 #[NO] :その番組の放送話数
18 #[/dev/video0]:キャプチャデバイス
19 #
20 #
21 # DCC-JPL Japan/foltia project
22 #
23 #
24
25
26 #use Time::HiRes qw(usleep);
27
28
29 $path = $0;
30 $path =~ s/tvrecording.pl$//i;
31 if ($pwd  ne "./"){
32 push( @INC, "$path");
33 }
34
35
36 ##
37 #キャプチャカード枚数検出
38 #cat /proc/interrupts  | grep ivtv |wc -l
39 # 11:    1054118          XT-PIC  uhci_hcd, eth0, ivtv0, ivtv1, ivtv2
40 #失敗
41
42 #tvConfig.pl -------------------------------
43 $extendrecendsec = 10;                                                  #recording end second.
44 #$startupsleeptime = 52;                                        #process wait(MAX60sec)
45 $startupsleeptime = 37;                                 #process wait(MAX60sec)
46
47
48 #-------------------------------
49
50 require 'foltialib.pl';
51
52  &writelog("tvrecording:  DEBUG $ARGV[0] $ARGV[1] $ARGV[2] $ARGV[3] $ARGV[4] $ARGV[5] $ARGV[6]  $ARGV[7] ");
53
54 sub getChCallsign {
55 if ($ARGV[5]  ne ""){
56         $recchname = $ARGV[5] ;
57         }else{
58         $recchname = $recch."ch";
59 }
60
61 }#endsub getChCallsign
62
63 sub getRecPath{ #capture directory define
64         $recfolderpath = '/home/foltia/php/tv';         
65 }#end sub getRecPath
66 #
67 # -- ここからメイン ----------------------------
68 #準備
69 &prepare;
70 #もし録画が走ってたら、止める
71 $reclengthsec = &chkrecprocess();
72 &setbitrate;
73 &chkextinput;
74
75 $reclengthsec = $reclengthsec + $extendrecendsec ;
76
77 &callrecordv4l;
78
79 &writelog("tvrecording:$recch:$reclengthsec:$outputfile:$recdevice:$capturedeviceinputnum:$ivtvrecch:$stdbitrate:$peakbitrate");
80
81 # -- これ以下サブルーチン ----------------------------
82 sub chkextinput{
83
84 if ($recch == 0){
85                 if ($svideoinputnum > -1 && $svideoinputnum < 30){
86                 $capturedeviceinputnum = $svideoinputnum ;
87                 }else{
88                 $capturedeviceinputnum = 7 ;
89                 }
90         $capturedeviceinputName = "S-Video 1";
91         $ivtvrecch = '';
92 }elsif($recch == -1){
93                 if ($comvideoinputnum > -1 && $comvideoinputnum < 30){
94                 $capturedeviceinputnum = $comvideoinputnum;
95                 }else{
96                 $capturedeviceinputnum = 8;
97                 }
98         $capturedeviceinputName = "Composite 1";
99         $ivtvrecch = '';
100 }else{
101                 if ($tunerinputnum > -1 && $tunerinputnum < 30){
102                 $capturedeviceinputnum = $tunerinputnum ;
103                 }else{
104                 $capturedeviceinputnum = 6 ;
105                 }
106         $capturedeviceinputName = "Tuner 1";
107         $ivtvrecch = $recch;
108 }
109 # 1-12chはntsc-bcast-jp
110 if($recch > 12){
111         if ($uhfbandtype == 1){
112         $frequencyTable = "ntsc-cable-jp";
113         }else{
114         $frequencyTable = "ntsc-bcast-jp";
115         }
116 }else{
117         $frequencyTable = "ntsc-bcast-jp";
118 }#if
119         &writelog ("tvrecording DEBUG $frequencyTable $recch");
120
121 }#chkextinput
122
123
124
125 sub chkrecprocessOLD{
126 #もし録画が走ってたら、止める
127 my $mencoderkillcmd = "";
128
129 $mencoderkillcmd =`/usr/sbin/lsof -Fp $recdevice`;
130 $mencoderkillcmd =~ s/p//;
131
132 if ($mencoderkillcmd != ""){
133         #kill process
134         $mencoderkillcmd  = "kill ".$mencoderkillcmd;
135         system ($mencoderkillcmd);
136         chomp($mencoderkillcmd);
137         &writelog ("tvrecording Killed current recording process. process:$mencoderkillcmd");
138                 sleep(1);
139                  my $videodevice =`/usr/sbin/lsof $recdevice`;
140
141                 while ($videodevice =~ /tvrecording/){
142
143                 $videodevice =`/usr/sbin/lsof $recdevice`;
144                 sleep(1);
145                 $sleepcounter++;
146                 $reclengthsec = $reclengthsec - $sleepcounter;
147                 &writelog ("tvrecording videodevice wait:$sleepcounter");
148                 }
149                 $sleepcounter = 0;             
150 }#if ($mencoderkillcmd != "")
151
152 return $reclengthsec;
153
154 }#end chkrecprocess
155
156 sub chkrecprocess{
157 my $mencoderkillcmd = "";
158 my $j = $recunits -1;
159 my $i = 0;
160 my $testrecdevice = "";
161 my @usedevices  ;
162 my @unusedevices;
163 my $n = 0;
164 $recdevice = "";
165 if ($ARGV[7]  ne ""){
166         $recdevice =  $ARGV[7] ;
167 }
168
169 #for ($i = $j ;$i >= 0 ; $i--){
170 for ($i = 0 ;$i <= $j ; $i++){
171 #print "$i,$j\n";
172 $testrecdevice = "/dev/video$i";
173 $mencoderkillcmd =`/usr/sbin/lsof -Fp $testrecdevice`;
174 $mencoderkillcmd =~ s/p//;
175 if ($mencoderkillcmd != ""){
176         push (@usedevices ,  $testrecdevice);
177         &writelog ("tvrecording now using:$testrecdevice");
178 }else{
179         push (@unusedevices ,  $testrecdevice);
180         &writelog ("tvrecording unused:$testrecdevice");
181 }#if
182 }#for
183
184 $i = 0; #初期化
185 $n = @unusedevices;
186 #デバイス指定があるか?
187 if ($recdevice  ne ""){ #指定があったら
188 #そこが使われているかチェック
189 $mencoderkillcmd =`/usr/sbin/lsof -Fp $recdevice`;
190 $mencoderkillcmd =~ s/p//;
191         if ($mencoderkillcmd != ""){ #使われてたら無条件に落とす
192         $mencoderkillcmd  = "kill ".$mencoderkillcmd;
193         system ($mencoderkillcmd);
194         chomp($mencoderkillcmd);
195         &writelog ("tvrecording Killed current recording process. $recdevice:$mencoderkillcmd");
196                 sleep(1);
197         }
198 }else{
199 #地上波or 指定なしなら
200         if (($n == 0) and ($recch > 0)) {#空きデバイスがなくて、地上波なら     
201         $mencoderkillcmd =`/usr/sbin/lsof -Fp /dev/video$i`;#→$i
202         $mencoderkillcmd =~ s/p//;
203                 if ($mencoderkillcmd != ""){ #使われてたら最高位/dev/video$j を無条件に落とす →最低位$i
204                 $mencoderkillcmd  = "kill ".$mencoderkillcmd;
205                 system ($mencoderkillcmd);
206                 chomp($mencoderkillcmd);
207                 &writelog ("tvrecording Killed current recording process. /dev/video$i:$mencoderkillcmd");
208                         sleep(1);
209                 }
210         $recdevice = "/dev/video$i"; #→最低位$i
211                 &writelog ("tvrecording select device:$recdevice");
212
213 }elsif ($recch <= 0) { # 外部入力なら
214         #外部入力だけどデバイス指定されていないときも
215         #落とす
216         $mencoderkillcmd =`/usr/sbin/lsof -Fp /dev/video$j`;#
217         $mencoderkillcmd =~ s/p//;
218                 if ($mencoderkillcmd != ""){ #使われてたら最高位/dev/video$j を無条件に落とす
219                 $mencoderkillcmd  = "kill ".$mencoderkillcmd;
220                 system ($mencoderkillcmd);
221                 chomp($mencoderkillcmd);
222                 &writelog ("tvrecording Killed current recording process. /dev/video$j:$mencoderkillcmd");
223                         sleep(1);
224                 }
225         $recdevice = "/dev/video$j"; # 外部入力は最高位デバイス
226         }else{
227         #空きを使う
228         $recdevice = shift(@unusedevices );
229         }#endif 空きデバイスなければ
230
231 }#end if 指定あるか
232
233 #ここには落ちてこないはずなのに?
234 if ($recdevice eq ""){
235         $recdevice = "/dev/video0";
236         &writelog ( "Rec Device un defined. / $recch ");
237 }
238 return $reclengthsec;
239
240 }#end chkrecprocessNew
241
242
243
244 sub prepare{
245
246 #引数エラー処理
247 $recch = $ARGV[0] ;
248 $reclengthsec = $ARGV[1];
249 if (($recch eq "" )|| ($reclengthsec eq "")){
250         print "usage tvrecording.pl ch length(sec) [clip No(000-)] [filename] [bitrate(5)] [TID] [NO] [/dev/video0]\n";
251         exit;
252 }
253 #1分前にプロセス起動するから指定時間スリープ
254 #srand(time ^ ($$ + ($$ << 15)));
255 #my $useconds  = int(rand(12000000));
256 #my $intval = int ($useconds  / 1000000);
257 #my $startupsleeptimemicro = ($startupsleeptime * 1000000) - $useconds;
258 #$reclengthsec = $reclengthsec + $intval + 1;
259 #&writelog("tvrecording:  DEBUG SLEEP $startupsleeptime:$useconds:$intval:$startupsleeptimemicro");
260 #       usleep ( $startupsleeptimemicro );
261
262 # $recch でウェイト調整入れましょう
263 #52
264 #my $intval = $recch % 50; # 0〜49
265 #my $startupsleep = $startupsleeptime - $intval; #  3〜52 (VHF 40-51)
266 #37
267 my $intval = $recch % 35; # 0〜34
268 my $startupsleep = $startupsleeptime - $intval; #  3-37 (VHF 25-36,tvk 30)
269 $reclengthsec = $reclengthsec + (60 - $startupsleep) + 1; #
270
271 if ( $ARGV[2] ne "N"){
272         &writelog("tvrecording: DEBUG SLEEP $startupsleeptime:$intval:$startupsleep:$reclengthsec");
273         sleep ( $startupsleep);
274 }else{
275         &writelog("tvrecording: DEBUG RAPID START");
276
277 }
278 if ($recunits > 1){
279 my $deviceno = $recunits - 1;#3枚差しのとき/dev/video2から使う
280         $recdevice = "/dev/video$deviceno";
281         $recch = $ARGV[0] ;
282 }else{
283 #1枚差し
284         $recdevice = "/dev/video0";
285         $recch = $ARGV[0] ;
286 }
287
288 &getChCallsign();
289 #&getRecPath;
290
291 $outputpath = "$recfolderpath"."/";
292
293 if ($ARGV[6] eq "0"){
294         $outputfile = $outputpath.$ARGV[5]."--";
295 }else{
296         $outputfile = $outputpath.$ARGV[5]."-".$ARGV[6]."-";
297 }
298 #2番目以降のクリップでファイル名指定があったら
299         if ($ARGV[3]  ne ""){
300 #               if ($ARGV[3] =~ /[0-9]{8}-[0-9]{4}/){
301 #               $outputfile .= "$ARGV[3]";
302 #               }else{
303 #               $outputfile .= `date  +%Y%m%d-%H%M --date "1 min "`;
304 #               }
305                 $outputfile = $ARGV[3];
306                 $outputfile = &filenameinjectioncheck($outputfile);
307                 $outputfilewithoutpath = $outputfile ;
308                 $outputfile = $outputpath.$outputfile ;
309 #               $outputfile .= "$ARGV[3]";             
310 #               $outputfile .= `date  +%Y%m%d-%H%M --date "1 min "`;
311                 &writelog("tvrecording:  DEBUG ARGV[2] ne null  \$outputfile $outputfile ");
312         }else{
313                 $outputfile .= `date  +%Y%m%d-%H%M --date "1 min "`;
314                 chomp($outputfile);
315                 $outputfile .= ".m2p";
316                 $outputfilewithoutpath = $outputfile ;
317                 &writelog("tvrecording:  DEBUG ARGV[2] is null  \$outputfile $outputfile ");
318         }
319
320
321 @wday_name = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
322 $sleepcounter = 0;
323 $cmd="";
324
325 #二重録りなど既に同名ファイルがあったら中断
326 if ( -e "$outputfile" ){
327         if ( -s "$outputfile" ){
328         &writelog("tvrecording :ABORT :recfile $outputfile exist.");
329         exit 1;
330         }
331 }
332
333 }#end prepare
334
335 sub setbitrate{
336 $bitrate = $ARGV[4] ;
337 $bitrate = $bitrate * 1024*1024;#Mbps -> bps
338 $peakbitrate = $bitrate + 350000;
339 $recordbitrate = "  --bitrate $bitrate --peakbitrate $peakbitrate ";
340         $stdbitrate = "$bitrate";
341         $peakbitrate = "$peakbitrate";
342 }#end setbitrate
343
344
345 sub callrecordv4l{
346
347 #$frequency = `ivtv-tune -d $recdevice -t $frequencyTable -c $ivtvrecch | awk '{print $2}'|tr -d .`;
348 my $ivtvtuneftype = '';
349 if ($frequencyTable eq "ntsc-cable-jp"){
350         $ivtvtuneftype = 'japan-cable';
351 }else{
352         $ivtvtuneftype = 'japan-bcast';
353 }
354 #print "ivtv-tune -d $recdevice -t $ivtvtuneftype -c $ivtvrecch\n";
355 &writelog("tvrecording DEBUG ivtv-tune -d $recdevice -t $ivtvtuneftype -c $ivtvrecch");
356 &writelog("tvrecording DEBUG $ENV{PATH}");
357
358 $frequency = `env PATH=PATH=/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/bin:/usr/bin:/home/foltia/bin ivtv-tune -d $recdevice -t $ivtvtuneftype -c $ivtvrecch`;
359 &writelog("tvrecording DEBUG frequency:$frequency");
360 @frequency = split(/\s/,$frequency);
361 $frequency[1] =~ s/\.//gi;
362 $frequency = $frequency[1] ;
363 &writelog("tvrecording DEBUG frequency:$frequency");
364
365 my $recordv4lcallstring = "$toolpath/perl/record-v4l2.pl --frequency $frequency --duration $reclengthsec --input $recdevice --directory $recfolderpath --inputnum $capturedeviceinputnum --inputname '$capturedeviceinputName' --freqtable $frequencyTable --bitrate $stdbitrate --peakbitrate $peakbitrate --output $outputfilewithoutpath ";
366
367 &writelog("tvrecording $recordv4lcallstring");
368 &writelog("tvrecording DEBUG $ENV{HOME}/.ivtvrc");
369 $oserr = `env HOME=$toolpath $recordv4lcallstring`;
370 &writelog("tvrecording DEBUG $oserr");
371
372 }#end callrecordv4l
373
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。
track feed