As part of my upgrade to R5B7, I copied my old version of mythlink.sh to /usr/local/bin so I could keep using the wonderful script that I got from marc.aronson (
http://mysettopbox.tv/phpBB2/viewtopic.php?t=4865). Unfortunately, the script doesn't create new links for .mpg files. While I'm certainly not a linux shell scripting OR a database expert, I did a little digging and discovered the 'basename' field in the 'recorded' table that the script uses. With just a couple quick changes to mythlink.sh, it now seems to work fine for both the old .nuv's and the new .mpg's in /myth/tv.
Essentially, the changes I made were to 2 lines in the code from the post mentioned above.
Change one: add basename to the mysql query
Original code:
Code:
mysql -uroot mythconverg -B --exec "select chanid,DATE_FORMAT(starttime,'%Y%m%d%H%i%s'),DATE_FORMAT(endtime,'%Y%m%d%H%i%s'),title,subtitle,description from recorded;" >/tmp/mythlink.$$
New code:Code:
mysql -uroot mythconverg -B --exec "select chanid,DATE_FORMAT(starttime,'%Y%m%d%H%i%s'),DATE_FORMAT(endtime,'%Y%m%d%H%i%s'),title,subtitle,description,basename from recorded;" >/tmp/mythlink.$$
Change two: change $ofn to use the 'basename' data from the query
Original code:Code:
my $ofn = "${chanid}_${start}_${end}.nuv";
New code:Code:
my $ofn = "${basename}";
Again, I am certainly not a scripting expert - and this may not be the best way to handle the mix of old .nuv's and new .mpg's, but it works for me - and it keeps my wife happy (she still likes using a Windows box to watch shows through the pretty share).
The complete script is available at
http://www.doute.net/mythlink.sh.txt, and is listed below (be careful about line wrapping). Again, many thanks to all that contributed to the original script that I modified slightly.
Code:
#!/bin/sh
# mythlink.sh - Symlinks mythtv files to more readable version in /tv/pretty
# by Dale Gass
# modified by Dave Misevich for slightly prettier names and doesn't destroy/recreate valid links.
# (I found a windows box watching a recording would terminate if the link was removed)
# Bug fixed caused by programs that don't have a subtitle. Use start time/date to create one.
# modified to add .mpg to end of symlink. Thanks mike at the mysettopbox.tv forum for the code.
# modified to use 'basename' from mythconverg to handle a mix of .nuv & .mpg's in /myth/tv
mysql -uroot mythconverg -B --exec "select chanid,DATE_FORMAT(starttime,'%Y%m%d%H%i%s'),DATE_FORMAT(endtime,'%Y%m%d%H%i%s'),title,subtitle,description,basename from recorded;" >/tmp/mythlink.$$
perl -w -e '
my $mythpath= "/myth/tv";
my $altpath= "/myth/pretty";
my $altpath2 = "/myth/pretty2";
my $dfile= "";
my $edate= "";
my %month = ( "01","Jan", "02","Feb", "03","Mar", "04","Apr", "05","May", "06","Jun",
"07","Jul", "08","Aug", "09","Sep", "10","Oct", "11","Nov", "12","Dec");
if (!-d $altpath) {
mkdir $altpath or die "Failed to make directory: $altpath\n";
}
opendir(DIR, $altpath) or die "cannot opendir $altpath: $!";
while (defined($file = readdir(DIR))) {
next if $file =~ /^\.\.?$/; # skip . and ..
@dirlist = (@dirlist,$file);
}
closedir(DIR);
<>;
while (<>) {
chomp;
my ($chanid,$start,$end,$title,$subtitle,$description,$basename) = split /\t/;
$subtitle = "" if(!defined $subtitle);
my $ofn = "${basename}";
# skip if the nuv doesnt exist
do { print "Skipping $mythpath/$ofn\n"; next } unless -e "$mythpath/$ofn";
$start =~ /^....(........)/;
if ($subtitle eq "") {
# if no subtitle then build one from start time.
$subtitle = $month{substr($start,4,2)} . substr($start,6,2);
$subtitle = $subtitle . "_" . substr($start,0,4) . "_" . substr($start,8,4);
}
# my $nfn = "${title}-${subtitle}";
my $fntitle = "${title}";
$fntitle =~ s/&/+/g;
$fntitle =~ s/\047//g;
$fntitle =~ s/[^+0-9a-zA-Z_-]+/_/g;
$fntitle =~ s/_$//g;
my $fnsubtitle = substr($start,2,2).substr($start,4,2).substr($start,6,2)."-".substr($start,8,4). "-${subtitle}";
$fnsubtitle =~ s/&/+/g;
$fnsubtitle =~ s/\047//g;
$fnsubtitle =~ s/[^+0-9a-zA-Z_-]+/_/g;
$fnsubtitle =~ s/_$//g;
my $nfn = "${fntitle}-${fnsubtitle}";
my $nfn2 = "${fntitle}/${fnsubtitle}";
#print "\$ofn = $ofn $title \$nfn = $nfn\n";
# make a list of existing nuv filenames for use later.
$goodfile{$nfn.".mpg"} = 1;
#
# Create symlink in $altpath (pretty) and create description file
#
if (!-s "$altpath/$nfn".".mpg"){
# print "Creating $altpath/$nfn\n";
symlink "$mythpath/$ofn", "$altpath/$nfn".".mpg" or die "Failed to create symlink $altpath/$nfn".".mpg".": $!";
open ($dfile,">$altpath/descriptions/$nfn.mpg.txt") || die("Could not open txt file. $!");
print $dfile "$description \l";
close ($dfile);
}
#
# Create secondary altpath2 -- subdir by series
#
if (!-s "$altpath2/$nfn2".".mpg") {
mkdir "$altpath2/$fntitle";
symlink "$mythpath/$ofn", "$altpath2/$nfn2".".mpg" or die "Failed to create symlink in $altpath2/$nfn2".": $!";
}
}
# remove symlinks that are not pointed at valid files in the database
# Also removed corresponding file in "descriptions"
foreach $fname (@dirlist) {
unlink "$altpath/$fname" unless $goodfile{$fname};
unlink "$altpath/descriptions/$fname.txt" unless $goodfile{$fname};
#print "I want to unlink $altpath/$fname\n" unless $goodfile{$fname};
}
' /tmp/mythlink.$$
#
# Remove sym links in "pretty2" that point at files that
# don't exist anymore
#
find /myth/pretty2 -type l \! -xtype f -name \*.mpg -exec rm \{\} \;
find /myth/pretty -type l \! -xtype f -name \*.mpg -exec ls \{\} \;
rm /tmp/mythlink.$$
Allan