00001 #!/usr/bin/perl
00002 ##
00003 ## Script to extract show name and subtitle from DB given the filename.
00004 ##
00005 ## Hack and Slash done by Rob Snow (rsnow@dympna.com)
00006 ##
00007 ## 28 Mar 03 1.0 Hack
00008 ## 29 Mar 03 1.1 Added --legal to fix filenames for / and \
00009 ## Should probably be fixed for other chars
00010 ## but I'm too lazy.
00011 ## 14 Nov 05 2.0 Make it work with 0.18 and up
00012 ## 17 Nov 05 2.1 Fix a bug with the path for --all
00013 ##
00014 ## This is a very nasty hack of myth.rebuilddatabase.pl which was nicely
00015 ## done by Greg Froese and instructions by Robert Kulagowski.
00016 ##
00017 ## Those fine gentlemens information may be found below, however, please
00018 ## do not confuse them with the author of this hack...they do nice work.
00019 ##
00020 ## written by greg froese (g_froese@yahoo.com)
00021 ## install instructions by Robert Kulagowski (rkulagow@rocketmail.com)
00022 ## Much hacking by Behan Webster <behanw@websterwood.com>
00023 ## Who also added some code from epair_optimize.pl by xris
00024 ##
00025 ## use at your own risk, i am not responsible for anything this program may
00026 ## or may not do.
00027
00028 ##use strict;
00029 use DBI;
00030 use Getopt::Long;
00031 use File::Basename;
00032 use File::stat qw(:FIELDS);;
00033
00034 ## get command line args
00035
00036 sub usage() {
00037 print <<END;
00038 Usage: $0 [options] files
00039
00040 Where [options] are:
00041 --host - hostname of the mysql server (default: \"127.0.0.1\")
00042 --user - DBUSERNAME (default: \"mythtv\")
00043 --pass - DBPASSWORD (default: \"mythtv\")
00044 --database - DATABASENAME (default: \"mythconverg\")
00045 --replace - Replace spaces with this string (--rep=. will return Daily.Show)
00046 --subtitle - Add subtitle to string after a ':'
00047 --sublen - Maximum subtitle length (only useful with -subtitle)
00048 --legal - Make sure the filename is legal (no '/', '\', etc.)
00049 --all - Consider all files
00050 --channel - Print channel
00051 --codec - Print codec
00052 --description - Print description
00053 --extension - Extension to add to title (defaults to same as filename)
00054 --file - Print filename
00055 --grep - Search title:subtitle
00056 --mpeg {2,4} - Only show files of mpeg2 or mpeg4 encoding
00057 --quiet - Don't print title
00058 --size - Show size of show (mins, size, size/h)
00059 --total - Print total size of listed files
00060 END
00061 exit 0;
00062 }
00063
00064 my $host="127.0.0.1";
00065 my $database="mythconverg";
00066 my $user="mythtv";
00067 my $pass="mythtv";
00068 my $mythdir = '/var/lib/mythtv/';
00069
00070 # Read the mysql.txt file in use by MythTV.
00071 # could be in a couple places, so try the usual suspects
00072 my $found = 0;
00073 my @mysql = ('/usr/local/share/mythtv/mysql.txt',
00074 '/usr/share/mythtv/mysql.txt',
00075 '/etc/mythtv/mysql.txt',
00076 '/usr/local/etc/mythtv/mysql.txt',
00077 "$ENV{HOME}/.mythtv/mysql.txt",
00078 'mysql.txt'
00079 );
00080 foreach my $file (@mysql) {
00081 next unless (-e $file);
00082 $found = 1;
00083 open(CONF, $file) or die "Unable to open $file: $!\n\n";
00084 while (my $line = <CONF>) {
00085 # Cleanup
00086 next if ($line =~ /^\s*#/);
00087 $line =~ s/^str //;
00088 chomp($line);
00089
00090 # Split off the var=val pairs
00091 my ($var, $val) = split(/\=/, $line, 2);
00092 next unless ($var && $var =~ /\w/);
00093 if ($var eq 'DBHostName') {
00094 $host = $val;
00095 } elsif ($var eq 'DBUserName') {
00096 $user = $val;
00097 } elsif ($var eq 'DBName') {
00098 $database = $val;
00099 } elsif ($var eq 'DBPassword') {
00100 $pass = $val;
00101 }
00102 }
00103 close CONF;
00104 }
00105 #die "Unable to locate mysql.txt: $!\n\n" unless ($found && $dbhost);
00106
00107 usage if !GetOptions('verbose+'=>\$verbose, 'help'=>\$help, 'quiet'=>\$quiet,
00108 'database=s'=>\$database, 'host=s'=>\$host, 'user=s'=>\$user, 'pass=s'=>\$pass,
00109 'subtitle'=>\$sub, 'sublen=s'=>\$sublen, 'replace=s'=>\$rep, 'legal'=>\$legal,
00110 'file'=>\$printfile, 'codec'=>\$printcodec, 'channel'=>\$printchan,
00111 'size'=>\$printsize, 'description'=>\$printdesc, 'mpeg=i'=>\$mpeg,
00112 'all'=>\$all, 'extension:s'=>\$extension, 'grep=s'=>\$grep, 'total'=>\$printtotal,
00113 );
00114 usage if $help;
00115
00116 my @files;
00117 if ($all) {
00118 opendir(DIR, $mythdir) || die "$mythdir: $!";
00119 @files = grep {/(mpg|nuv)$/} readdir DIR;
00120 closedir DIR;
00121 } else {
00122 @files = @ARGV;
00123 }
00124 usage unless @files;
00125
00126 my $dbh = DBI->connect("dbi:mysql:database=$database:host=$host","$user","$pass")
00127 || die "Cannot connect to database ($!)\n";
00128
00129 my $sql = 'SELECT title, subtitle, chanid, starttime, endtime, description FROM recorded WHERE basename=?';
00130 my $sth = $dbh->prepare($sql);
00131 my $chansql = 'SELECT channum, callsign FROM channel WHERE chanid=?';
00132 my $sthchan = $dbh->prepare($chansql);
00133 for my $file (@files) {
00134 ($show, $path, $suffix) = fileparse($file , '.nuv');
00135
00136 $sth->execute("$show$suffix") || die "Could not execute ($sql)\n";
00137 my ($title, $subtitle, $chanid, $start, $end, $description) = $sth->fetchrow_array;
00138 $sthchan->execute($chanid) || die "Could not execute ($chansql)\n";
00139 my ($channum, $callsign) = $sthchan->fetchrow_array;
00140
00141 unless ($title) {
00142 print "$file is not in the database\n";
00143 next;
00144 }
00145
00146 if ($rep) {
00147 $subtitle=~s/\ /$rep/gio;
00148 $title=~s/\ /$rep/gio;
00149 }
00150
00151 if ($legal) {
00152 my $good = " ";
00153 if ($rep) {
00154 $good = $rep;
00155 }
00156 $title =~ s/[\\\/'"()]/$good/gio;
00157 $subtitle =~ s/[\\\/'"()]/$good/gio;
00158 }
00159
00160 if ($sublen) {
00161 $subtitle=substr($subtitle,0,$sublen);
00162 }
00163
00164 my $print = 1;
00165 if ($mpeg || $printcodec || $printsize || $printtotal) {
00166 require Time::Piece;
00167 my $before = Time::Piece->strptime($start, '%Y-%m-%d %H:%M:%S');
00168 my $after = Time::Piece->strptime($end, '%Y-%m-%d %H:%M:%S');
00169 my $diff = $after - $before;
00170 stat($file) || stat("$mythdir/$file") || die "Can't find $file: $!";
00171 $mins = $diff->minutes;
00172 $gb = $st_size/1024/1024/1024;
00173 $gbh = $gb * 60 / $mins;
00174
00175 # This is a bit of a hack, but recording from an ivtv card, and then transcoding it works.
00176 $codec = $gbh >= 1.1 ? 2 : 4;
00177
00178 $print = 0 if $mpeg && $mpeg ne $codec;
00179 #print "MPEG:$mpeg $gbh $print\n";
00180 }
00181
00182 if ($grep) {
00183 $print = 0 unless $title =~ /$grep/ || $subtitle && $subtitle =~ /$grep/;
00184 #print "Grep: $print $grep $title\n";
00185 }
00186
00187 my $ext;
00188 if (defined $extension) {
00189 $file =~ /\.(.*?)$/;
00190 $ext = $extension || $1;
00191 }
00192
00193 if ($print) {
00194 print "$file " if $printfile;
00195 print "mpeg$codec " if $printcodec;
00196 print $title unless $quiet;
00197 print ":$subtitle" if $sub && $subtitle;
00198 print ".$ext" if $ext;
00199 print " on $callsign($chanid)" if $printchan;
00200 printf " (%d mins in %.3f GB %.3f GB/h)", $mins, $gb, $gbh if $printsize;
00201 print "\n";
00202 print " $description\n" if $printdesc;
00203
00204 $tgb += $gb;
00205 $tt += $mins;
00206 $tn += 1;
00207 }
00208 }
00209
00210 if ($printtotal) {
00211 printf "%.3f GB for %.1f hours in %d files\n", $tgb, $tt/60, $tn;
00212 }
00213
00214 exit(0);
00215
00216 # vim: sw=4 ts=4