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 | ## 17 Nov 05 2.1 Fix a bug with the path for --all
|
---|
13 | ##
|
---|
14 | ## This is a very nasty hack of myth.rebuilddatabase.pl which was nicely
|
---|
15 | ## done by Greg Froese and instructions by Robert Kulagowski.
|
---|
16 | ##
|
---|
17 | ## Those fine gentlemens information may be found below, however, please
|
---|
18 | ## do not confuse them with the author of this hack...they do nice work.
|
---|
19 | ##
|
---|
20 | ## written by greg froese (g_froese@yahoo.com)
|
---|
21 | ## install instructions by Robert Kulagowski (rkulagow@rocketmail.com)
|
---|
22 | ## Much hacking by Behan Webster <behanw@websterwood.com>
|
---|
23 | ## Who also added some code from epair_optimize.pl by xris
|
---|
24 | ##
|
---|
25 | ## use at your own risk, i am not responsible for anything this program may
|
---|
26 | ## or may not do.
|
---|
27 |
|
---|
28 | ##use strict;
|
---|
29 | use DBI;
|
---|
30 | use Getopt::Long;
|
---|
31 | use File::Basename;
|
---|
32 | use File::stat qw(:FIELDS);;
|
---|
33 |
|
---|
34 | ## get command line args
|
---|
35 |
|
---|
36 | sub usage() {
|
---|
37 | print <<END;
|
---|
38 | Usage: $0 [options] files
|
---|
39 |
|
---|
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;
|
---|
62 | }
|
---|
63 |
|
---|
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/';
|
---|
69 |
|
---|
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);
|
---|
89 |
|
---|
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);
|
---|
106 |
|
---|
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;
|
---|
115 |
|
---|
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;
|
---|
125 |
|
---|
126 | my $dbh = DBI->connect("dbi:mysql:database=$database:host=$host","$user","$pass")
|
---|
127 | || die "Cannot connect to database ($!)\n";
|
---|
128 |
|
---|
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');
|
---|
135 |
|
---|
136 | $sth->execute("$show$suffix") || die "Could not execute ($sql)\n";
|
---|
137 | my ($title, $subtitle, $chanid, $start, $end, $description) = $sth->fetchrow_array;
|
---|
138 | $sthchan->execute($chanid) || die "Could not execute ($chansql)\n";
|
---|
139 | my ($channum, $callsign) = $sthchan->fetchrow_array;
|
---|
140 |
|
---|
141 | unless ($title) {
|
---|
142 | print "$file is not in the database\n";
|
---|
143 | next;
|
---|
144 | }
|
---|
145 |
|
---|
146 | if ($rep) {
|
---|
147 | $subtitle=~s/\ /$rep/gio;
|
---|
148 | $title=~s/\ /$rep/gio;
|
---|
149 | }
|
---|
150 |
|
---|
151 | if ($legal) {
|
---|
152 | my $good = " ";
|
---|
153 | if ($rep) {
|
---|
154 | $good = $rep;
|
---|
155 | }
|
---|
156 | $title =~ s/[\\\/'"()]/$good/gio;
|
---|
157 | $subtitle =~ s/[\\\/'"()]/$good/gio;
|
---|
158 | }
|
---|
159 |
|
---|
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 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | if ($printtotal) {
|
---|
211 | printf "%.3f GB for %.1f hours in %d files\n", $tgb, $tt/60, $tn;
|
---|
212 | }
|
---|
213 |
|
---|
214 | exit(0);
|
---|
215 |
|
---|
216 | # vim: sw=4 ts=4
|
---|