Mythvideo Playlists

From MythTV Official Wiki
Jump to: navigation, search

List-add.png Todo: Script needs to be updated to use Perl bindings for database credentials.

MythTV allows you to set a "next file" in metadata editor screen. If you have a lot of movies / series and want (especially for series) continuous playing of episodes, you might want to take a look at this script.

What result will you get

Continuous playing of all your video files. If the last file of the playlist ended, it will continue playing with the first file in the playlist.

You may exclude directories of files from this mechanism.

mythvideo_autoplaylist.pl

If not already installed you will need the following perl packages:

  • DBI
  • File

Add this script to a location you like. In my case it's /root/bin

Alter the values for $db (database), $user (database user), $pass (database password) and $host (database host) to your environment. The array @ignored_dirs holds the directories you may like to exclude from setting "next file".

#!/usr/bin/perl -w

use DBI;
use File::Spec;

################################################################################
##### BEGIN CONFIG #############################################################
################################################################################

## mysql user database name
$db ="mythconverg";

## mysql database user name
$user = "mythtv";

## mysql database password
$pass = "mythtv";

## mysql hostname : in most cases "localhost" but it can be diffrent, too
$host="localhost";

# No 'next file' for files in this dirs, ignore them
@ignored_dirs = ('converted', 'testing');

################################################################################
##### END OF CONFIG ############################################################
################################################################################

## Return code
my $rc = 0;

## Establish DB connection
$dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pass);

############################################################################
#################  Set childids to have auto playlists  ####################
############################################################################

$query = "SELECT intid, title, filename FROM ".$db.".videometadata WHERE 1=1";
   
if (@ignored_dirs > 0)
{   
    foreach (@ignored_dirs)
    {  
        $query .= " AND filename NOT LIKE '" . $_ . "'";
    }
}

$query .= " ORDER BY filename;";

$sqlQuery  = $dbh->prepare($query)
or die "Can't prepare $query: $dbh->errstr\n";
$rv = $sqlQuery->execute
or die "can't execute the query: $sqlQuery->errstr";
   
if ($sqlQuery->rows > 0)
{
    $rows = $sqlQuery->fetchall_arrayref();

    for ($i = 0; $i < $#{$rows}; $i++)
    {  
        (undef,$directories,undef) = File::Spec->splitpath($$rows[$i][2]);
        @dirs = File::Spec->splitdir($directories);

        my $in_ignored_dir = 0;

        foreach (@dirs)
        {  
            $in_ignored_dir = (in_array($_, \@ignored_dirs)) ? 1 : $in_ignored_dir;
        }
        next if ($in_ignored_dir);

        $query = "update ".$db.".videometadata set childid='".$$rows[$i+1][0]."' where intid='".$$rows[$i][0]."';";

        $sqlQuery  = $dbh->prepare($query)
        or die "Can't prepare $query: $dbh->errstr\n";
        $rv = $sqlQuery->execute
        or die "can't execute the query: $sqlQuery->errstr";
    }

    # Run a final query which sets last 'next file' to the first file of your videos
    $query = "update ".$db.".videometadata set childid='".$$rows[0][0]."' where intid='".$$rows[$#{$rows}][0]."';";

    $sqlQuery  = $dbh->prepare($query)
    or die "Can't prepare $query: $dbh->errstr\n";
    $rv = $sqlQuery->execute
    or die "can't execute the query: $sqlQuery->errstr";
}

$rc = $sqlQuery->finish;

############################################################################
######################### Some functions ###################################
############################################################################

sub in_array
{
    my ($item, $array) = @_;
    my %hash = map { $_ => 1 } @$array;
    if ($hash{$item}) { return 1; } else { return 0; }
}

Add this script to your crontab

You may add this script to your crontab so it get's executed periodically. In my case it's the root users' crontab.

# crontab -e

Add a line like

*/30 * * * *       /root/bin/mythvideo_autoplaylists.pl

This will execute the script every 30 minutes.

Alternatively you may attach this script to MythTV_System_Events.

Done

You may now start the script once to have your "next file" field populated. Please remember that your video files have to be added to MythVideo with "MENU->Scan For Changes" first.

Enjoy :)