1 | #!/usr/bin/perl
|
---|
2 | ##
|
---|
3 | ## Script to extract show name and subtitle from DB given the filename.
|
---|
4 | ##
|
---|
5 | ## Hack and Slash done by Rob Snow (rsnow@dympna.com)
|
---|
6 | ##
|
---|
7 | ## 28 Mar 03 1.0 Hack
|
---|
8 | ## 29 Mar 03 1.1 Added --legal to fix filenames for / and \
|
---|
9 | ## Should probably be fixed for other chars
|
---|
10 | ## but I'm too lazy.
|
---|
11 | ## 14 Nov 05 2.0 Make it work with 0.18 and up
|
---|
12 | ##
|
---|
13 | ## This is a very nasty hack of myth.rebuilddatabase.pl which was nicely
|
---|
14 | ## done by Greg Froese and instructions by Robert Kulagowski.
|
---|
15 | ##
|
---|
16 | ## Those fine gentlemens information may be found below, however, please
|
---|
17 | ## do not confuse them with the author of this hack...they do nice work.
|
---|
18 | ##
|
---|
19 | ## written by greg froese (g_froese@yahoo.com)
|
---|
20 | ## install instructions by Robert Kulagowski (rkulagow@rocketmail.com)
|
---|
21 | ## Much hacking by Behan Webster <behanw@websterwood.com>
|
---|
22 | ## Who also added some code from epair_optimize.pl by xris
|
---|
23 | ##
|
---|
24 | ## use at your own risk, i am not responsible for anything this program may
|
---|
25 | ## or may not do.
|
---|
26 |
|
---|
27 | ##use strict;
|
---|
28 | use DBI;
|
---|
29 | use Getopt::Long;
|
---|
30 | use File::Basename;
|
---|
31 | use File::stat qw(:FIELDS);;
|
---|
32 |
|
---|
33 | ## get command line args
|
---|
34 |
|
---|
35 | sub usage() {
|
---|
36 | print <<END;
|
---|
37 | Usage: $0 [options] files
|
---|
38 |
|
---|
39 | Where [options] are:
|
---|
40 | --host - hostname of the mysql server (default: \"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 | --subtitle - Add subtitle to string after a ':'
|
---|
46 | --sublen - Maximum subtitle length (only useful with -subtitle)
|
---|
47 | --legal - Make sure the filename is legal (no '/', '\', etc.)
|
---|
48 | --all - Consider all files
|
---|
49 | --channel - Print channel
|
---|
50 | --codec - Print codec
|
---|
51 | --description - Print description
|
---|
52 | --extension - Extension to add to title (defaults to same as filename)
|
---|
53 | --file - Print filename
|
---|
54 | --grep - Search title:subtitle
|
---|
55 | --mpeg {2,4} - Only show files of mpeg2 or mpeg4 encoding
|
---|
56 | --quiet - Don't print title
|
---|
57 | --size - Show size of show (mins, size, size/h)
|
---|
58 | --total - Print total size of listed files
|
---|
59 | END
|
---|
60 | exit 0;
|
---|
61 | }
|
---|
62 |
|
---|
63 | my $host="127.0.0.1";
|
---|
64 | my $database="mythconverg";
|
---|
65 | my $user="mythtv";
|
---|
66 | my $pass="mythtv";
|
---|
67 |
|
---|
68 | # Read the mysql.txt file in use by MythTV.
|
---|
69 | # could be in a couple places, so try the usual suspects
|
---|
70 | my $found = 0;
|
---|
71 | my @mysql = ('/usr/local/share/mythtv/mysql.txt',
|
---|
72 | '/usr/share/mythtv/mysql.txt',
|
---|
73 | '/etc/mythtv/mysql.txt',
|
---|
74 | '/usr/local/etc/mythtv/mysql.txt',
|
---|
75 | "$ENV{HOME}/.mythtv/mysql.txt",
|
---|
76 | 'mysql.txt'
|
---|
77 | );
|
---|
78 | foreach my $file (@mysql) {
|
---|
79 | next unless (-e $file);
|
---|
80 | $found = 1;
|
---|
81 | open(CONF, $file) or die "Unable to open $file: $!\n\n";
|
---|
82 | while (my $line = <CONF>) {
|
---|
83 | # Cleanup
|
---|
84 | next if ($line =~ /^\s*#/);
|
---|
85 | $line =~ s/^str //;
|
---|
86 | chomp($line);
|
---|
87 |
|
---|
88 | # Split off the var=val pairs
|
---|
89 | my ($var, $val) = split(/\=/, $line, 2);
|
---|
90 | next unless ($var && $var =~ /\w/);
|
---|
91 | if ($var eq 'DBHostName') {
|
---|
92 | $host = $val;
|
---|
93 | } elsif ($var eq 'DBUserName') {
|
---|
94 | $user = $val;
|
---|
95 | } elsif ($var eq 'DBName') {
|
---|
96 | $database = $val;
|
---|
97 | } elsif ($var eq 'DBPassword') {
|
---|
98 | $pass = $val;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | close CONF;
|
---|
102 | }
|
---|
103 | #die "Unable to locate mysql.txt: $!\n\n" unless ($found && $dbhost);
|
---|
104 |
|
---|
105 | usage if !GetOptions('verbose+'=>\$verbose, 'help'=>\$help, 'quiet'=>\$quiet,
|
---|
106 | 'database=s'=>\$database, 'host=s'=>\$host, 'user=s'=>\$user, 'pass=s'=>\$pass,
|
---|
107 | 'subtitle'=>\$sub, 'sublen=s'=>\$sublen, 'replace=s'=>\$rep, 'legal'=>\$legal,
|
---|
108 | 'file'=>\$printfile, 'codec'=>\$printcodec, 'channel'=>\$printchan,
|
---|
109 | 'size'=>\$printsize, 'description'=>\$printdesc, 'mpeg=i'=>\$mpeg,
|
---|
110 | 'all'=>\$all, 'extension:s'=>\$extension, 'grep=s'=>\$grep, 'total'=>\$printtotal,
|
---|
111 | );
|
---|
112 | usage if $help;
|
---|
113 |
|
---|
114 | my @files;
|
---|
115 | if ($all) {
|
---|
116 | my $dir = '/var/lib/mythtv/';
|
---|
117 | opendir(DIR, $dir) || die "$dir: $!";
|
---|
118 | @files = grep {/(mpg|nuv)$/} readdir DIR;
|
---|
119 | closedir DIR;
|
---|
120 | } else {
|
---|
121 | @files = @ARGV;
|
---|
122 | }
|
---|
123 | usage unless @files;
|
---|
124 |
|
---|
125 | my $dbh = DBI->connect("dbi:mysql:database=$database:host=$host","$user","$pass")
|
---|
126 | || die "Cannot connect to database ($!)\n";
|
---|
127 |
|
---|
128 | my $sql = 'SELECT title, subtitle, chanid, starttime, endtime, description FROM recorded WHERE basename=?';
|
---|
129 | my $sth = $dbh->prepare($sql);
|
---|
130 | my $chansql = 'SELECT channum, callsign FROM channel WHERE chanid=?';
|
---|
131 | my $sthchan = $dbh->prepare($chansql);
|
---|
132 | for my $file (@files) {
|
---|
133 | ($show, $path, $suffix) = fileparse($file , '.nuv');
|
---|
134 |
|
---|
135 | $sth->execute("$show$suffix") || die "Could not execute ($sql)\n";
|
---|
136 | my ($title, $subtitle, $chanid, $start, $end, $description) = $sth->fetchrow_array;
|
---|
137 | $sthchan->execute($chanid) || die "Could not execute ($chansql)\n";
|
---|
138 | my ($channum, $callsign) = $sthchan->fetchrow_array;
|
---|
139 |
|
---|
140 | unless ($title) {
|
---|
141 | print "$file is not in the database\n";
|
---|
142 | next;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if ($rep) {
|
---|
146 | $subtitle=~s/\ /$rep/gio;
|
---|
147 | $title=~s/\ /$rep/gio;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if ($legal) {
|
---|
151 | my $good = " ";
|
---|
152 | if ($rep) {
|
---|
153 | $good = $rep;
|
---|
154 | }
|
---|
155 | $title =~ s/[\\\/'"()]/$good/gio;
|
---|
156 | $subtitle =~ s/[\\\/'"()]/$good/gio;
|
---|
157 | }
|
---|
158 |
|
---|
159 | if ($sublen) {
|
---|
160 | $subtitle=substr($subtitle,0,$sublen);
|
---|
161 | }
|
---|
162 |
|
---|
163 | my $print = 1;
|
---|
164 | if ($mpeg || $printcodec || $printsize || $printtotal) {
|
---|
165 | require Time::Piece;
|
---|
166 | my $before = Time::Piece->strptime($start, '%Y-%m-%d %H:%M:%S');
|
---|
167 | my $after = Time::Piece->strptime($end, '%Y-%m-%d %H:%M:%S');
|
---|
168 | my $diff = $after - $before;
|
---|
169 | stat($file) || die "Can't find $file: $!";
|
---|
170 | $mins = $diff->minutes;
|
---|
171 | $gb = $st_size/1024/1024/1024;
|
---|
172 | $gbh = $gb * 60 / $mins;
|
---|
173 |
|
---|
174 | # This is a bit of a hack, but recording from an ivtv card, and then transcoding it works.
|
---|
175 | $codec = $gbh >= 1.1 ? 2 : 4;
|
---|
176 |
|
---|
177 | $print = 0 if $mpeg && $mpeg ne $codec;
|
---|
178 | #print "MPEG:$mpeg $gbh $print\n";
|
---|
179 | }
|
---|
180 |
|
---|
181 | if ($grep) {
|
---|
182 | $print = 0 unless $title =~ /$grep/ || $subtitle && $subtitle =~ /$grep/;
|
---|
183 | #print "Grep: $print $grep $title\n";
|
---|
184 | }
|
---|
185 |
|
---|
186 | my $ext;
|
---|
187 | if (defined $extension) {
|
---|
188 | $file =~ /\.(.*?)$/;
|
---|
189 | $ext = $extension || $1;
|
---|
190 | }
|
---|
191 |
|
---|
192 | if ($print) {
|
---|
193 | print "$file " if $printfile;
|
---|
194 | print "mpeg$codec " if $printcodec;
|
---|
195 | print $title unless $quiet;
|
---|
196 | print ":$subtitle" if $sub && $subtitle;
|
---|
197 | print ".$ext" if $ext;
|
---|
198 | print " on $callsign($chanid)" if $printchan;
|
---|
199 | printf " (%d mins in %.3f GB %.3f GB/h)", $mins, $gb, $gbh if $printsize;
|
---|
200 | print "\n";
|
---|
201 | print " $description\n" if $printdesc;
|
---|
202 |
|
---|
203 | $tgb += $gb;
|
---|
204 | $tt += $mins;
|
---|
205 | $tn += 1;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | if ($printtotal) {
|
---|
210 | printf "%.3f GB for %.1f hours in %d files\n", $tgb, $tt/60, $tn;
|
---|
211 | }
|
---|
212 |
|
---|
213 | exit(0);
|
---|
214 |
|
---|
215 | # vim: sw=4 ts=4
|
---|