Mythlink for Human Readable File Names

I started using MythTV with Knoppmyth a few years ago. It had a feature where it would put a human readable filename in a folder that linked to the original file that was recorded from live TV by MythTV. I’ve since started using Mythdora and I did some searching and couldn’t find anything like it that worked on Mythdora So I found a copy of the script and modified it to work with both Mythdora 4 and 5.

I share the /storage/videos folder through Samba so I directed the script to put the new links in the /storage/videos/tv folder. You could easily modify it to fit your needs.

Installation instructions:

As your mythtv user, put the mythlink shell script in /home/mythtv

chmod 770 /home/mythtv/mythlink (makes it executable)

mkdir /storage/videos/tv

Edit your crontab by typing “crontab -e”
add this line so the script executes hourly:

1 * * * * /home/mythtv/mythlink.sh

(The default editor for cron is vi. For a cheat sheet look here.

Run the script for the first time, cron will do it for you next time:

/home/mythtv/mythlink


mythlink.sh

#!/bin/sh
# mythlink.sh – Symlinks mythtv files to more readable version in /tv/pretty
# by Dale Gass
# modified by Dave M. for slightly prettier names and doesn’t destroy/recreate valid links.
# (I found a windows box watching a recording would terminate playback if the link was removed)
# Modified by Brakk to work on Mythdora 4 and 5
# and to add the .mpg extension

mysql -uroot mythconverg -B –exec “select chanid,DATE_FORMAT(starttime, ‘%Y%m%d%H%i%s’),title,subtitle from recorded;” >/tmp/mythlink.$$
perl -w -e ‘
my $mythpath= “/storage/recordings”;
my $altpath= “/storage/videos/tv”;
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,$title,$subtitle) = split /\t/;
$subtitle = “” if(!defined $subtitle);
my $ofn = “${chanid}_${start}.mpg”;
do { print “Skipping $mythpath/$ofn\n”; next } unless -e “$mythpath/$ofn”;
$start =~ /^….(……..)/;
#my $nfn = “$1_${title}_${subtitle}”;
my $nfn = “${title}_${subtitle}”;
$nfn =~ s/&/+/g;
$nfn =~ s/\047//g;
$nfn =~ s/[^+0-9a-zA-Z_-]+/_/g;
$nfn =~ s/_$//g;
$nfn = “${nfn}.mpg”;
$goodfile{$nfn} = 1;
if (!-e “$altpath/$nfn”){
print “Creating $nfn\n”;
symlink “$mythpath/$ofn”, “$altpath/$nfn” or die “Failed to create symlink $altpath/$nfn: $!”;
}
}
# remove symlinks that are not pointed at valid files in the database
foreach $fname (@dirlist) {
# switch the comment to the second line after you
# are satisfied that it would not delete a good file.
# unlink “$altpath/$fname” unless $goodfile{$fname};
print “I want to unlink $altpath/$fname\n” unless $goodfile{$fname};
}

‘ /tmp/mythlink.$$
rm /tmp/mythlink.$$

2 replies on “Mythlink for Human Readable File Names”

  1. visitor says:

    I have .mpg and .avi (after transcode) video recordings files in my mythdora system. Any chance to process .avi filenames and keep the .avi extension in the links also. Also, my recordings storage group includes /storage/recordings and /storage2/recordings, any chance to have the script process both? Links can remain in the /storage/videos/tv directory since they do not consume much space in and of themselves.

    Thank You,

  2. Brakk says:

    You could maybe copy the main loop and change everything that says “.mpg” to “.avi” so it parses the folder twice.

    Then have it do it again for the extra folders you have.

Leave a Reply

Your email address will not be published. Required fields are marked *