LinuxでFriio録画 簡易延長対応(その4)サーバ & UNIX
最近忙しくて、ろくにエントリも上げられないのだが、つづき
EPG変更対応も問題なく動作することを確認できたのだが、複数回変更がかかると
2009-12-20 0340 NTV 音龍門【変更】【変更】【変更】【変更】
なんて、情けないファイル名になるので少し変更。
【変更2】
【変更3】
…
と変更回数を入れるようにした。
それにしても、ホントに冗長なスクリプトになってしまった。
キレイに書き直せば、2/3ぐらいにはなると思うし、後々の修正も楽になるだろう…とは思うのだが、このスクリプト自体の負荷は全然たいしたことないし、動いてるのでこのまま放置になるだろう。orz
cron_rec.pl
#!/usr/bin/perl
#-------------------------------------------------------------------#
# 変数宣言
$cmd_rec = '/usr/local/bin/recfriio --b25 --strip';
$cmd_tss = '/usr/local/bin/tsselect';
$cmd_tsp = '/usr/local/bin/tssp.py';
$cmd_b25 = '/usr/local/bin/b25 -v 0';
$cmd_ffm = '/usr/local/bin/enc_ffmpeg.pl';
$cmd_epg = '/usr/local/bin/epgdump -l - -';
$workdir = '/work';
$tmpdir = '/tmp';
$outdir = '/CURRENT';
$libdir = '/LIBRARY';
$rec_log = '/work/cron_rec.log';
$enc_log = '/work/cron_enc.log';
$epg_log = '/work/cron_epg.log';
$chk_sec = 20;
use Time::HiRes;
&now_date;
$start_time = $now_time;
$mondir = `nice -5 date --date "1 min" "+%Y-%m"`;
chomp $mondir;
if ($ARGV[0] eq '') {print "usage: $0 [rec_ch] [rec_sec] [title] [prog_id]\n"; exit;}
$rec_ch = $ARGV[0];
$rec_sec = $ARGV[1];
$title = $ARGV[2];
$prog_id = $ARGV[3];
if ($rec_ch eq "B101") {
$rec_ch = "B10";
$flg_bs = 1;
$rem_pid = "0x02f0";
} # if
elsif ($rec_ch eq "B102") {
$rec_ch = "B10";
$flg_bs = 2;
$rem_pid = "0x01f0";
} # elsif
($book_date, $book_time, $ttl_other) = split (/ /, $title, 3);
$book_date =~ s/-/\//g;
$book_time =~ s/([0-9]{2})([0-9]{2})/$1:$2:00/;
$epg_chk = `nice -5 $cmd_rec $rec_ch $chk_sec - 2> /dev/null | nice -5 $cmd_epg 2> /dev/null | grep "SEVT," | cut -d , -f 3- | sort | uniq`;
if ($epg_chk ne '' && $epg_chk !~ /$prog_id,[^,]*,$book_date,$book_time,[^,]*,[^,]*,$rec_sec,.*/ && $epg_chk =~ /$prog_id/) {
($SHH, $SMM, $btm_other) = split (/:/, $book_time, 3);
$epg_chk =~ /$prog_id,[^,]*,([^,]*),([^,]*),[^,]*,[^,]*,([^,]*),.*/;
$book_date = $1;
($EHH, $EMM, $epg_other) = split (/:/, $2, 3);
$rec_sec = $3;
$book_date =~ s/\//-/g;
if ($EHH >= $SHH) {$wait_sec = ($EHH - $SHH)*3600 + ($EMM - $SMM)*60 - $chk_sec;}
elsif ($EHH < $SHH) {$wait_sec = ($EHH + 24 - $SHH)*3600 + ($EMM - $SMM)*60 - $chk_sec;}
if ($ttl_other =~ /^(.*)【変更([0-9]+)】$/) {
$h_num = $2; $h_num ++; $ttl_other = "$1【変更$h_num】";
} # if
elsif ($ttl_other =~ /^(.*)【変更】$/){ $ttl_other = "$1【変更2】";}
else{ $ttl_other = "$ttl_other【変更】";}
open LOG, ">>$epg_log";
print LOG "$epg_chk\n";
print LOG "$prog_id:$title\n";
print LOG "\`$0 $rec_ch$flg_bs $rec_sec \\\"$book_date $EHH$EMM $ttl_other\\\" $prog_id\`\n";
close LOG;
sleep $wait_sec;
`$0 $rec_ch$flg_bs $rec_sec \"$book_date $EHH$EMM $ttl_other\" $prog_id`;
exit;
} # if
elsif ($epg_chk ne '' && $epg_chk !~ /$prog_id,[^,]*,$book_date,$book_time,[^,]*,[^,]*,$rec_sec,.*/) {
$rec_sec = $rec_sec + 1800;
$title = "$title【延長】";
} # elsif
open LOG, ">>$epg_log";
print LOG "$epg_chk\n";
print LOG "$prog_id:$title\n";
close LOG;
#-------------------------------------------------------------------#
# 現在時刻取得
sub now_date {
$now = Time::HiRes::time;
($wtime, $msec) = split(/\./, $now);
($sec,$min,$hour,$mday,$mon,$year,$wno) = localtime($wtime);
@wdays = ('SUN','MON','TUE','WED','THU','FRI','SAT');
$now_time = sprintf("%04d/%02d/%02d(%s) %02d:%02d:%02d.%06d",$year+1900,$mon+1,$mday,$wdays[$wno],$hour,$min,$sec,$msec);
} # now_date
#-------------------------------------------------------------------#
# TS録画 & ARIB STD-B25
until ($sec == 59){
$sec = (localtime)[0];
if ($sec < 58) {sleep (58 - $sec);}
&now_date;
$rec_time = $now_time;
} # until
&recording;
sub recording {
$ret = `$cmd_rec $rec_ch $rec_sec "$workdir/$title.ts"`;
&now_date;
$ret1 = "$ret1\n$ret$now_time\n";
} # recording
if ($ret =~ /no friio can be used./) {
$rec_sec --;
# $cmd_rec = "$cmd_rec --hdp";
&recording;
} # if
elsif ($ret =~ /Continue recording without B25./) {
$ret = `nice -19 $cmd_b25 "$workdir/$title.ts" "$workdir/$title.b25"`;
$ret1 = "$ret1\n$ret\n";
$ret = `nice -19 rm "$workdir/$title.ts"`;
$ret1 = "$ret1\n$ret\n";
$ret = `nice -19 mv "$workdir/$title.b25" "$workdir/$title.ts"`;
$ret1 = "$ret1\n$ret\n";
&now_date;
$ret1 = "$ret1\n$now_time\n";
} # elsif
elsif ($ret =~ /Error/) {
$rec_sec --;
&recording;
} # elsif
#-------------------------------------------------------------------#
# without 1segTS
`nice -19 chmod 664 \"$workdir/$title.ts\"`;
if ($rec_ch eq "B10") {
$pid = `nice -19 $cmd_tss \"$workdir/$title.ts\"`;
$ret2 = $pid;
$pid =~ s/pid=//g;
$pid =~ s/, .*//g;
$pid =~ s/$rem_pid//g;
$pid =~ s/\n+/ /g;
$ret3 = `nice -19 $cmd_tss \"$workdir/$title.ts\" \"$outdir/$title.m2ts\" $pid`;
`nice -19 rm \"$tmpdir/$title.ts\"`;
} # if
elsif ($rec_ch eq "26") {
$pid = `nice -19 $cmd_tss \"$workdir/$title.ts\"`;
$ret2 = $pid;
$pid =~ s/pid=//g;
$pid =~ s/, .*//g;
# NHK教育テレビ 3rdSD除去
$pid =~ s/0x04f0//g;
$pid =~ s/\n+/ /g;
$ret3 = `nice -19 $cmd_tss \"$workdir/$title.ts\" \"$outdir/$title.m2ts\" $pid`;
`nice -19 rm \"$tmpdir/$title.ts\"`;
} # elsif
elsif ($rec_ch !~ /B/){
$pid = `nice -19 $cmd_tss \"$workdir/$title.ts\"`;
$ret2 = $pid;
$pid =~ s/pid=//g;
$pid =~ s/, .*//g;
$pid =~ s/0x1fc[89]{1}//g;
$pid =~ s/\n+/ /g;
$ret3 = `nice -19 $cmd_tss \"$workdir/$title.ts\" \"$outdir/$title.m2ts\" $pid`;
`nice -19 rm \"$tmpdir/$title.ts\"`;
} # elsif
else {$ret3 = `nice -19 cp \"$workdir/$title.ts\" \"$outdir/$title.m2ts\"`;}
`nice -19 chmod 664 \"$outdir/$title.m2ts\"`;
#-------------------------------------------------------------------#
# Encode for DVD
#if ($title =~ /\[二\]/ && $title =~ /\[S\]/) {$opt_flg = 2;}
if ($title =~ /\[二\]/) {$opt_flg = 2;}
`nice -19 $cmd_ffm \"$outdir/$title.m2ts\" $opt_flg`;
#-------------------------------------------------------------------#
# write rec_log
&log_write;
sub log_write {
open LOG, ">>$rec_log";
print LOG "##################################################\n";
print LOG "$start_time\n";
print LOG "$rec_time\n";
print LOG "$ARGV[0] $ARGV[1] $ARGV[2]\n";
if ($ret1 ne '') {print LOG "### RECORD ###$ret1\n"};
if ($ret2 ne '') {print LOG "### TSLIST ###\n$ret2"};
if ($ret3 ne '') {print LOG "### TS-SEL ###\n$ret3\n"};
print LOG "$now_time\n";
print LOG "### ffmpeg Encode complete ###\n";
&now_date;
print LOG "$now_time\n";
close LOG;
} # log_write
exit;
