#!/usr/bin/perl use DBI; use Getopt::Long; use File::Basename; use DirHandle; $DIR='/var/video'; # Where does myth store its videos $TODIR='/tv'; # The parent directory to copy the files to $CPFLAGS='-m'; # Special cp flags, probaly none! (-m is a modified cp flag) # DB information $DBHOST = "127.0.0.1"; $DBUSER = "mythtv"; $DBPASS = "mythtv"; $DBNAME = "mythconverg"; sub getname{ my $file=shift; my ($filename,$subtitle); my $dbh = DBI->connect("dbi:mysql:database=$DBNAME:host=$DBHOST","$DBUSER","$DBPASS") or die "Cannot connect to database ($!)\n"; ($show, $path, $suffix) = fileparse($file,"\.nuv"); $channel = substr($show,0,4); $syear = substr($show, 5,4); $smonth = substr($show, 9,2); $sday = substr($show,11,2); $shour = substr($show,13,2); $sminute = substr($show,15,2); $ssecond = substr($show,17,2); $eyear = substr($show,20,4); $emonth = substr($show,24,2); $eday = substr($show,26,2); $ehour = substr($show,28,2); $eminute = substr($show,30,2); $esecond = substr($show,32,2); $q = "select title, subtitle, chanid, starttime, endtime, cutlist from recorded where chanid=$channel and starttime='$syear$smonth$sday$shour$sminute$ssecond' and endtime='$eyear$emonth$eday$ehour$eminute$esecond'"; $sth = $dbh->prepare($q); $sth->execute or die "Could not execute ($q)\n"; @row=$sth->fetchrow_array; $title = $row[0]; $subtitle = $row[1]; $cutlist = $row[5]; $nogood = " "; $title =~ s/\\/$nogood/gio; $subtitle =~ s/\\/$nogood/gio; $title =~ s/\//$nogood/gio; $subtitle =~ s/\//$nogood/gio; $title =~ s/\:/$nogood/gio; $subtitle =~ s/\:/\ /gio; $filename = "$title"; if (-e "$filename.avi"){ $cnt = 1; while (-e "$filename-[$cnt].avi") { $cnt=$cnt+1; } $filename = "$filename-[$cnt]"; } $filename =~ s/\s/_/g; $filename =~ s/\W//g; $subtitle =~ s/\s/_/g; $subtitle =~ s/\W//g; $subtitle = $subtitle .'.mpg'; return($filename,$subtitle); } sub docopy{ my $file=shift; my ($title,$filename)=&getname($file); if ( ! -d "$TODIR/$title" ) { mkdir("$TODIR/$title"); } if ( -f "$TODIR/$title/$filename" ) { @ostat=stat("$file"); @nstat=stat("$TODIR/$title/$filename"); if ( $ostat[7] <= $nstat[7] ) { print "ERROR: $TODIR/$title/$filename exists, skipping!\n"; return; } } system("cp $CPFLAGS $file $TODIR/$title/$filename"); } $d= new DirHandle "$DIR"; if (defined $d) { while (defined($_ = $d->read)) { if ( $_ !~ /\.nuv$/ || $_ =~ /ringbuf/ ) { next; } $do="$DIR/$_"; ($title,$filename)=&getname($do); @t=localtime(time); $etime=sprintf("%04d%02d%02d%02d%02d%02d",$eyear,$emonth,$eday,$ehour,$eminute,$esecond); $atime=sprintf("%04d%02d%02d%02d%02d%02d",$t[5]+1900,$t[4]+1,$t[3],$t[2],$t[1],$t[0]); if ( $atime > $etime ) { print "Copy $title $filename\n"; &docopy($do); } } }