Mar 142012
 

Since I’m already transcoding the recordings to h.264, might as well make them easily accessible to my iPhone or Boxee Box or Miro. Since the filenames are more likely to be seen, might as well make them more human readable. The biggest advantage to this renaming a season of a recorded show for archiving using filebot. If you want to get fancy, could use tversity to stream the rss feed to a playstation 3 or an xbox 360, but I still think the Roku is the easiest frontend for previously transcoded material.

Disclaimer: I’ve removed a few things specific to my oddball setup so I can’t guarantee I didn’t break anything.

Contents of:
/usr/local/bin/itunesfeed.pl

Advertisement:
#!/usr/bin/perl -w

use Fcntl;
use MythTV;
use DateTime;
use DateTime::Format::Mail;
use MP4::Info;

# Generates an iTunes-compatible RSS feed of all MP4 files in the directory
# Fill in the following strings (between the empty quotes) with the information you would like in your RSS Feed

# The title of your feed
$title = "Raging\'s MythTV Box";

# A description of the feed
$description = "Recordings on Raging\'s MythTV Box";

# A link to the folder where your feed and recordings are located. Make sure you do NOT end the link with a "/" (i.e. http://www.example.com/recordings instead of http://www.example.com/recordings/)
$link = "http://ragingcomputer.com:80/stream";
$flink = "http://ragingcomputer.com:80/stream/friendly";

$linkdir = "/var/www/stream/friendly";
$origlinkdir = "/var/www/stream/origfriendly";

$subtitle = "All of the TV on the MythBox";

$summary = "All of the TV on the MythBox";

$author = "Raging";

# A link to the feed itself
$selflink = "http://ragingcomputer.com:80/stream/RSS-feed.xml";

# Information about the feed's author
$name = "Raging Computer";

# Your e-mail address. Must include a \ before the @ symbol (e.g., email\@domain.com)
$email = "ragingcomputer\@gmail.com";

# A link to an image to use for the feed
$image = "http://ragingcomputer.com:80/stream/mythimg.png";

# Update below if your MythTV recordings are saved to a different location
$file = "/var/www/stream/RSS-feed.xml";

# Update below to your MythTV recordings directory if different
$directory = "/var/www/stream";

# Connect to mythbackend
my $Myth = new MythTV();

# Deletes the current feed if it already exists. Always starts a new feed from scratch to account for deleted recordings
unlink($file);

# Deletes all symbolic links so they can be re-created
system("rm " . $linkdir . "/*.mp4");
system("rm " . $linkdir . "/*.png");
system("rm " . $linkdir . "/*.xml");
system("rm " . $origlinkdir . "/*.mpg");

# Creates a new file and fills in the headers of the feed using the information above
sysopen (XML, $file, O_RDWR|O_EXCL|O_CREAT, 0644);
printf XML "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<rss version=\"2.0\" xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\"\n      xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"\n xmlns:media=\"http://search.yahoo.com/mrss/\"\n xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
printf XML "  <channel>\n";
printf XML "    <title>" . $title . "</title>\n";
printf XML "    <link>" . $link . "</link>\n";
printf XML "    <description>" . $description . "</description>\n";
printf XML "    <language>en-us</language>\n";
printf XML "    <itunes:category text='TV &amp; Film'/>\n";
printf XML "    <itunes:explicit>no</itunes:explicit>\n";
printf XML "    <itunes:subtitle>" . $subtitle . "</itunes:subtitle>\n";
printf XML "    <itunes:summary>" . $summary . "</itunes:summary>\n";
printf XML "    <itunes:author>" . $author . "</itunes:author>\n";
printf XML "    <itunes:owner>\n";
printf XML "      <itunes:name>" . $name . "</itunes:name>\n";
printf XML "      <itunes:email>" . $email . "</itunes:email>\n";
printf XML "    </itunes:owner>\n";
printf XML "    <itunes:image href='" . $image . "'/>\n";
printf XML "    <image>\n";
printf XML "      <title>" . $title . "</title>\n";
printf XML "      <width>128</width>\n";
printf XML "      <height>128</height>\n";
printf XML "      <link>" . $link . "</link>\n";
printf XML "      <url>" . $image . "</url>\n";
printf XML "    </image>\n";
printf XML "    <atom:link href='" . $selflink . "' rel='self' type='application/rss+xml'/>\n\n";

# Example format - you can get all of the available options stealing from the mythrename.pl script
$format = '%T %- %Y-%m-%d, %g-%i %A %- %S';

###################################################


# Get a list of the files
opendir(DIR, $directory);
@Files1 = readdir(DIR);
closedir(DIR);

# Loop thru each of these files
foreach $File (@Files1) {

        # Get information (including last modified date) about file
        @FileData = stat($directory."/".$File);

        # Push this into a new array with date at front
        push(@Files, @FileData[9]."&&".$File);

}

# Sort this array
@Files = reverse(sort(@Files));

# Loop thru the files
foreach $File (@Files) {

        # Get the filename back from the string
        ($Date,$f) = split(/\&\&/,$File);

##############################

        if ( $f =~ /\.mp4$/ ){
                # Get data from mp4 file
                $mp4info = get_mp4info($directory . "/" . $f);

                $mp4mlength = $mp4info->{MM};
                $mp4size = $mp4info->{SIZE};
                $mp4slength = $mp4info->{SECS};

                $f =~ s/\.mp4$//;

                # Connect to the database
                $show = $Myth->new_recording($f);
                #print "Info: ".$show."\n";
                $name = $show->format_name($format, '-', '_', 1, 0, 1);
                #print "Name: ".$name."\n";

                $title = $show->format_name('%T', ' ', ' ', 1, 0, 1);
                $title =~ s/&//g;
                #print "Title: ".$title."\n";
                $episode = $show->format_name('%S', ' ', ' ', 1, 0, 1);
                $episode =~ s/&//g;
                #print "Episode: ".$episode."\n";
                $summary = $show->format_name('%R', ' ', ' ', 1, 0, 1);
                $summary =~ s/&//g;
                #print "Summary: ".$summary."\n";

                $channel = $show->format_name('%cn', ' ', ' ', 1, 0, 1);
                #print "Channel: ".$channel."\n";
                $callsign = $show->format_name('%cc', ' ', ' ', 1, 0, 1);
                #print "Channel: ".$callsign."\n";

                $year = $show->format_name('%Y', ' ', ' ', 1, 0, 1);
                #print "Year: ".$year."\n";
                $month = $show->format_name('%m', ' ', ' ', 1, 0, 1);
                #print "Month: ".$month."\n";
                $day = $show->format_name('%d', ' ', ' ', 1, 0, 1);
                #print "Day: ".$day."\n";
                $starthour = $show->format_name('%G', ' ', ' ', 1, 0, 1);
                #print "StartHour: ".$starthour."\n";
                $startmin = $show->format_name('%i', ' ', ' ', 1, 0, 1);
                #print "StartMin: ".$startmin."\n";
                $startsec = $show->format_name('%s', ' ', ' ', 1, 0, 1);
                #print "StartSec: ".$startsec."\n";
                $origair = $show->format_name('%om/%od/%oY', ' ', ' ', 1, 0, 1);
                #print "Orig: ".$origair."\n";

                my $dt = DateTime->new(
                        year => $year, month => $month, day => $day,
                        hour => $starthour, minute => $startmin, second => $startsec,
                        time_zone => "local"
                );

                $str2822date = DateTime::Format::Mail->format_datetime( $dt );
                #print $str2822date."\n";

                # Make long description easier to manage
                $longdesc = "Channel: " . $callsign . " - " . $title . " - " . $episode ."  -  ". $summary ." - Duration: " . $mp4mlength . "min - Original Airdate: " . $origair;

                $fprogtime = $show->format_name('%Y-%m-%d_%H-%i', ' ', ' ', 1, 0, 1);
                $fprogtime2 = $show->format_name('%Y%m%d%H%i', ' ', ' ', 1, 0, 1);
                $progtime = $show->format_name('%m-%d-%Y %g:%i %A', ' ', ' ', 1, 0, 1);
                
                $ftitle = $show->format_name('%T', ' ', ' ', 1, 0, 1);
                $ftitle =~ s/\//-/ig;
                $ftitle =~ s/\\/-/ig;
                $ftitle =~ s/\?/-/ig;
                $ftitle =~ s/\%/-/ig;
                $ftitle =~ s/\*/-/ig;
                $ftitle =~ s/\:/-/ig;
                $ftitle =~ s/\;/-/ig;
                $ftitle =~ s/\|/-/ig;
                $ftitle =~ s/\"/-/ig;
                $ftitle =~ s/\'/-/ig;
                $ftitle =~ s/\</-/ig;
                $ftitle =~ s/\>/-/ig;
                $ftitle =~ s/\./-/ig;
                $ftitle =~ s/&/-/ig;
                $ftitle =~ s/\s/_/ig;
                #print "Title: ".$ftitle."\n";

                $fepisode = $show->format_name('%S', ' ', ' ', 1, 0, 1);
                $fepisode =~ s/\//-/ig;
                $fepisode =~ s/\\/-/ig;
                $fepisode =~ s/\?/-/ig;
                $fepisode =~ s/\%/-/ig;
                $fepisode =~ s/\*/-/ig;
                $fepisode =~ s/\:/-/ig;
                $fepisode =~ s/\;/-/ig;
                $fepisode =~ s/\|/-/ig;
                $fepisode =~ s/\"/-/ig;
                $fepisode =~ s/\'/-/ig;
                $fepisode =~ s/\</-/ig;
                $fepisode =~ s/\>/-/ig;
                $fepisode =~ s/\./-/ig;
                $fepisode =~ s/&/-/ig;
                $fepisode =~ s/\s/_/ig;
                #print "Episode: ".$fepisode."\n";

                # Friendly Link for transcoded video
                $lncmd = "/bin/ln -s " . $directory . "/" . $f . ".mp4 " . $linkdir . "\/" . $fprogtime . "_-_" . $ftitle . "_-_" . $fepisode . ".mp4";
                
                # Friendly Link for thumbnail
                $lncmd2 = "/bin/ln -s " . $directory . "/" . $f . ".png " . $linkdir . "\/" . $fprogtime . "_-_" . $ftitle . "_-_" . $fepisode . ".png";                
                
                # Fix timestamp on transcoded video
                $lncmd3 = "/usr/bin/touch -t " . $fprogtime2 . " " . $directory . "/" . $f . ".mp4" ;
                
                # Set permissions for transcoded video
                $lncmd4 = "/bin/chown mythtv\:mythtv " . $directory . "/" . $f . ".mp4" ;
                
                # Fix timestamp on thumbnail
                $lncmd5 = "/usr/bin/touch -t " . $fprogtime2 . " " . $directory . "/" . $f . ".png" ;
                
                # Set permissions on thumbnail
                $lncmd6 = "/bin/chown mythtv\:mythtv " . $directory . "/" . $f . ".png" ;
                
                # Friendly Link for original video
                $lncmd7 = "/bin/ln -s /var/lib/mythtv/recordings/" . $f . " " . $origlinkdir . "\/" . $fprogtime . "_-_" . $ftitle . "_-_" . $fepisode . ".mpg";


                #print $lncmd . "\n";
                #print $lncmd2 . "\n";
                #print $lncmd3 . "\n";
                #print $lncmd4 . "\n";
                #print $lncmd5 . "\n";
                #print $lncmd6 . "\n";
                #print $lncmd7 . "\n";

                system($lncmd);
                system($lncmd2);
                system($lncmd3);
                system($lncmd4);
                system($lncmd5);
                system($lncmd6);
                system($lncmd7);
                
                $ffilename = $fprogtime . "_-_" . $ftitle . "_-_" . $fepisode;
                
                # Break File Path / Name into ItemNum Identifier Number
                $f =~ s/\.mpg$//;
                $f =~ s/$directory\///;
                $f =~ s/\_//;
                $id = $f;

                # Adds the item information for each .MP4 file to the a string using the information pulled from the file name above
                $item = "    <item>\n";
                $item = $item . "      <title>" . $title . " - " . $episode ."</title>\n";
                $item = $item . "      <link>" . $link . "</link>\n";
                $item = $item . "      <description>" . $longdesc . "</description>\n";
                $item = $item . "      <content:encoded>" . $longdesc . "</content:encoded>\n";
                $item = $item . "      <author>". $email ." (" . $title . ")</author>\n";
                $item = $item . "      <pubDate>" .$str2822date ."</pubDate>\n";

                $item = $item . "      <itunes:explicit>no</itunes:explicit>\n";
                $item = $item . "      <itunes:keywords>MythTV,Feed</itunes:keywords>\n";
                $item = $item . "      <itunes:author>" . $title . "</itunes:author>\n";
                $item = $item . "      <itunes:subtitle>" . $longdesc . "</itunes:subtitle>\n";
                $item = $item . "      <itunes:summary>" . $longdesc . "</itunes:summary>\n";

                $item = $item . "      <enclosure url='" . $flink . "/" . $ffilename . ".mp4' length=\"" . $mp4size . "\" type='video/mp4' />\n";
                $item = $item . "      <guid>" . $flink . "/" . $ffilename . ".mp4</guid>\n";

                $item = $item . "      <media:content duration=\"" . $mp4mlength . "\" medium=\"video\" fileSize=\"" . $mp4size . "\" url='" . $flink . "/" . $ffilename . ".mp4' type='video/mp4'>\n";
                $item = $item . "        <media:title>" . $title . " - " . $episode ."</media:title>\n";
                $item = $item . "        <media:description>" . $longdesc . "</media:description>\n";
                $item = $item . "        <media:thumbnail url=\"" . $flink . "/" . $ffilename . ".png\"  width=\"320\" height=\"240\" />\n";
                $item = $item . "      </media:content>\n";
                $item = $item . "    </item>\n\n";
                
                print XML $item;
                

        }
}

# Adds the closing tags for the feed and closes out the file
printf XML "  </channel>\n";
printf XML "</rss>";
close(XML);

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)