MythTV Roku Streaming XML List Generator

Once the files have been created, will need to create an XML file the brightscript program on the Roku DVP can parse to display show data. This bit of perl reads the stream directory, sorts by file creation date, picks out the .mp4 files, uses the MythTV bindings to pull out show data, smashes it into some XML and writes it to a file.

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

[perl]
#!/usr/bin/perl -w
use Fcntl;
use MythTV;
use MP4::Info;

# The title of your feed
$title = "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";

# Update below if your MythTV recordings are saved to a different location
$file = "/var/www/stream/mythtv.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);

# 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" standalone="yes"?>n";
printf XML "<feed>n";

##
# Iterate through the directory picking out all the .mp4 files and add them to a Roku XML list – brute force style
##

# 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);
$mp4slength = $mp4info->{SECS};

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

# Connect to the database
$show = $Myth->new_recording($f);
#print "Info: ".$show."n";

$title = $show->format_name(‘%T’, ‘ ‘, ‘ ‘, 1, 0, 1);
$title =~ s/&/and/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";

$progtime = $show->format_name(‘%m-%d-%Y %g:%i %A’, ‘ ‘, ‘ ‘, 1, 0, 1);

$f =~ s/$directory///;
$id = $f;
$id =~ s/_//;
$id =~ s/.mpg$//;

# Adds the item information for each .MP4 file to the a string using the information pulled from the file name above
$item = " <item sdImg="" . $link . "/" . $f . ".png" hdImg="" . $link . "/" . $f . ".png">n";
$item = $item . " <title>" . $title . " – " . $episode ."</title>n";
$item = $item . " <contentId>" . $id . "</contentId>n";
$item = $item . " <contentType>TV</contentType>n";
$item = $item . " <contentQuality>SD</contentQuality>n";
if (-e $directory."/biffile/".$f.".bif") {
$item = $item . " <sdBifUrl>" . $link . "/biffile/" . $f . ".bif</sdBifUrl>n";
}
$item = $item . " <media>n";
$item = $item . " <streamFormat>mp4</streamFormat>n";
$item = $item . " <streamQuality>SD</streamQuality>n";
$item = $item . " <streamBitrate>500</streamBitrate>n";
$item = $item . " <streamUrl>" . $link . "/" . $f . ".mp4</streamUrl>n";
$item = $item . " </media>n";
$item = $item . " <synopsis>" . $progtime . " – " . $summary . "</synopsis>n";
$item = $item . " <genres>TV</genres>n";
$item = $item . " <runtime>" . $mp4slength . "</runtime>n";
$item = $item . " </item>nn";

print XML $item;

}
}
# Adds the closing tags for the feed and closes out the file
printf XML "</feed>n";
close(XML);
[/perl]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: