33 | | my $argc=@ARGV; |
34 | | if ($argc == 0) { |
35 | | print "usage: mythname.pl [options] |
36 | | /path/to/store/1001_20030401190000_20030401200000.nuv |
37 | | |
38 | | Where [options] is: |
39 | | --host - hostname or IP address of the mysql server (default: |
40 | | \"127.0.0.1\") |
41 | | --user - DBUSERNAME (default: \"mythtv\") |
42 | | --pass - DBPASSWORD (default: \"mythtv\") |
43 | | --database - DATABASENAME (default: \"mythconverg\") |
44 | | --replace - Replace spaces with this string (--rep=. will return Daily.Show) |
45 | | --sublen - Maximum subtitle length (only useful with -s) |
46 | | -s - Add subtitle to string after a ':' |
47 | | --legal - Make sure the filename is legal (no '/', '\', etc.) |
48 | | "; |
49 | | exit(0); |
| 40 | Where [options] are: |
| 41 | --host - hostname of the mysql server (default: \"127.0.0.1\") |
| 42 | --user - DBUSERNAME (default: \"mythtv\") |
| 43 | --pass - DBPASSWORD (default: \"mythtv\") |
| 44 | --database - DATABASENAME (default: \"mythconverg\") |
| 45 | --replace - Replace spaces with this string (--rep=. will return Daily.Show) |
| 46 | --subtitle - Add subtitle to string after a ':' |
| 47 | --sublen - Maximum subtitle length (only useful with -subtitle) |
| 48 | --legal - Make sure the filename is legal (no '/', '\', etc.) |
| 49 | --all - Consider all files |
| 50 | --channel - Print channel |
| 51 | --codec - Print codec |
| 52 | --description - Print description |
| 53 | --extension - Extension to add to title (defaults to same as filename) |
| 54 | --file - Print filename |
| 55 | --grep - Search title:subtitle |
| 56 | --mpeg {2,4} - Only show files of mpeg2 or mpeg4 encoding |
| 57 | --quiet - Don't print title |
| 58 | --size - Show size of show (mins, size, size/h) |
| 59 | --total - Print total size of listed files |
| 60 | END |
| 61 | exit 0; |
52 | | GetOptions('verbose+'=>\$verbose, 'database=s'=>\$database, 'host=s'=>\$host, |
53 | | 'user=s'=>\$user, 'pass=s'=>\$pass, 's+'=>\$sub, 'replace=s'=>\$rep, |
54 | | 'sublen=s'=>\$sublen, 'legal+'=>\$legal); |
| 64 | my $host="127.0.0.1"; |
| 65 | my $database="mythconverg"; |
| 66 | my $user="mythtv"; |
| 67 | my $pass="mythtv"; |
| 68 | my $mythdir = '/var/lib/mythtv/'; |
56 | | if (!$host) { $host="127.0.0.1"; } |
57 | | if (!$database) { $database="mythconverg"; } |
58 | | if (!$user) { $user="mythtv"; } |
59 | | if (!$pass) { $pass="mythtv"; } |
| 70 | # Read the mysql.txt file in use by MythTV. |
| 71 | # could be in a couple places, so try the usual suspects |
| 72 | my $found = 0; |
| 73 | my @mysql = ('/usr/local/share/mythtv/mysql.txt', |
| 74 | '/usr/share/mythtv/mysql.txt', |
| 75 | '/etc/mythtv/mysql.txt', |
| 76 | '/usr/local/etc/mythtv/mysql.txt', |
| 77 | "$ENV{HOME}/.mythtv/mysql.txt", |
| 78 | 'mysql.txt' |
| 79 | ); |
| 80 | foreach my $file (@mysql) { |
| 81 | next unless (-e $file); |
| 82 | $found = 1; |
| 83 | open(CONF, $file) or die "Unable to open $file: $!\n\n"; |
| 84 | while (my $line = <CONF>) { |
| 85 | # Cleanup |
| 86 | next if ($line =~ /^\s*#/); |
| 87 | $line =~ s/^str //; |
| 88 | chomp($line); |
61 | | my $dbh = |
62 | | DBI->connect("dbi:mysql:database=$database:host=$host","$user","$pass") or |
63 | | die "Cannot connect to database ($!)\n"; |
| 90 | # Split off the var=val pairs |
| 91 | my ($var, $val) = split(/\=/, $line, 2); |
| 92 | next unless ($var && $var =~ /\w/); |
| 93 | if ($var eq 'DBHostName') { |
| 94 | $host = $val; |
| 95 | } elsif ($var eq 'DBUserName') { |
| 96 | $user = $val; |
| 97 | } elsif ($var eq 'DBName') { |
| 98 | $database = $val; |
| 99 | } elsif ($var eq 'DBPassword') { |
| 100 | $pass = $val; |
| 101 | } |
| 102 | } |
| 103 | close CONF; |
| 104 | } |
| 105 | #die "Unable to locate mysql.txt: $!\n\n" unless ($found && $dbhost); |
65 | | ($show, $path, $suffix) = fileparse(@ARGV[0],"\.nuv"); |
66 | | $channel = substr($show,0,4); |
67 | | $syear = substr($show, 5,4); |
68 | | $smonth = substr($show, 9,2); |
69 | | $sday = substr($show,11,2); |
70 | | $shour = substr($show,13,2); |
71 | | $sminute = substr($show,15,2); |
72 | | $ssecond = substr($show,17,2); |
73 | | $eyear = substr($show,20,4); |
74 | | $emonth = substr($show,24,2); |
75 | | $eday = substr($show,26,2); |
76 | | $ehour = substr($show,28,2); |
77 | | $eminute = substr($show,30,2); |
78 | | $esecond = substr($show,32,2); |
| 107 | usage if !GetOptions('verbose+'=>\$verbose, 'help'=>\$help, 'quiet'=>\$quiet, |
| 108 | 'database=s'=>\$database, 'host=s'=>\$host, 'user=s'=>\$user, 'pass=s'=>\$pass, |
| 109 | 'subtitle'=>\$sub, 'sublen=s'=>\$sublen, 'replace=s'=>\$rep, 'legal'=>\$legal, |
| 110 | 'file'=>\$printfile, 'codec'=>\$printcodec, 'channel'=>\$printchan, |
| 111 | 'size'=>\$printsize, 'description'=>\$printdesc, 'mpeg=i'=>\$mpeg, |
| 112 | 'all'=>\$all, 'extension:s'=>\$extension, 'grep=s'=>\$grep, 'total'=>\$printtotal, |
| 113 | ); |
| 114 | usage if $help; |
80 | | $q = "select title, subtitle, chanid, starttime, endtime from recorded where |
81 | | chanid=$channel and starttime='$syear$smonth$sday$shour$sminute$ssecond' and |
82 | | endtime='$eyear$emonth$eday$ehour$eminute$esecond'"; |
83 | | $sth = $dbh->prepare($q); |
84 | | $sth->execute or die "Could not execute ($q)\n"; |
| 116 | my @files; |
| 117 | if ($all) { |
| 118 | opendir(DIR, $mythdir) || die "$mythdir: $!"; |
| 119 | @files = grep {/(mpg|nuv)$/} readdir DIR; |
| 120 | closedir DIR; |
| 121 | } else { |
| 122 | @files = @ARGV; |
| 123 | } |
| 124 | usage unless @files; |
90 | | if ($rep) { |
91 | | $subtitle=~s/\ /$rep/gio; |
92 | | $title=~s/\ /$rep/gio; |
93 | | } |
94 | | if ($sublen) { |
95 | | $subtitle=substr($subtitle,0,$sublen); |
96 | | } |
| 129 | my $sql = 'SELECT title, subtitle, chanid, starttime, endtime, description FROM recorded WHERE basename=?'; |
| 130 | my $sth = $dbh->prepare($sql); |
| 131 | my $chansql = 'SELECT channum, callsign FROM channel WHERE chanid=?'; |
| 132 | my $sthchan = $dbh->prepare($chansql); |
| 133 | for my $file (@files) { |
| 134 | ($show, $path, $suffix) = fileparse($file , '.nuv'); |
110 | | print "$title"; |
111 | | if ($sub) { |
112 | | print ":$subtitle"; |
| 160 | if ($sublen) { |
| 161 | $subtitle=substr($subtitle,0,$sublen); |
| 162 | } |
| 163 | |
| 164 | my $print = 1; |
| 165 | if ($mpeg || $printcodec || $printsize || $printtotal) { |
| 166 | require Time::Piece; |
| 167 | my $before = Time::Piece->strptime($start, '%Y-%m-%d %H:%M:%S'); |
| 168 | my $after = Time::Piece->strptime($end, '%Y-%m-%d %H:%M:%S'); |
| 169 | my $diff = $after - $before; |
| 170 | stat($file) || stat("$mythdir/$file") || die "Can't find $file: $!"; |
| 171 | $mins = $diff->minutes; |
| 172 | $gb = $st_size/1024/1024/1024; |
| 173 | $gbh = $gb * 60 / $mins; |
| 174 | |
| 175 | # This is a bit of a hack, but recording from an ivtv card, and then transcoding it works. |
| 176 | $codec = $gbh >= 1.1 ? 2 : 4; |
| 177 | |
| 178 | $print = 0 if $mpeg && $mpeg ne $codec; |
| 179 | #print "MPEG:$mpeg $gbh $print\n"; |
| 180 | } |
| 181 | |
| 182 | if ($grep) { |
| 183 | $print = 0 unless $title =~ /$grep/ || $subtitle && $subtitle =~ /$grep/; |
| 184 | #print "Grep: $print $grep $title\n"; |
| 185 | } |
| 186 | |
| 187 | my $ext; |
| 188 | if (defined $extension) { |
| 189 | $file =~ /\.(.*?)$/; |
| 190 | $ext = $extension || $1; |
| 191 | } |
| 192 | |
| 193 | if ($print) { |
| 194 | print "$file " if $printfile; |
| 195 | print "mpeg$codec " if $printcodec; |
| 196 | print $title unless $quiet; |
| 197 | print ":$subtitle" if $sub && $subtitle; |
| 198 | print ".$ext" if $ext; |
| 199 | print " on $callsign($chanid)" if $printchan; |
| 200 | printf " (%d mins in %.3f GB %.3f GB/h)", $mins, $gb, $gbh if $printsize; |
| 201 | print "\n"; |
| 202 | print " $description\n" if $printdesc; |
| 203 | |
| 204 | $tgb += $gb; |
| 205 | $tt += $mins; |
| 206 | $tn += 1; |
| 207 | } |