Make index.php

From MythTV Official Wiki
Revision as of 21:18, 5 September 2012 by Edster (talk | contribs) (The Code: - V00.02 - Update to write metadata file for AppleTV boxes)

Jump to: navigation, search


Author Ed ODonnell
Description A simple script to create a directory structure with links to the recordings inside them so it can be used by simple media players like the Apple TV box.
Supports


make_index.php

Here is a simple script to create a directory structure with links to the recordings inside them so it can be used by simple media players like the Apple TV box. It can be used on the command line.

Command line usage

If using on the command line, the usage information is as follows:

php -f /---location of script---/make_index.php

The Code

Script.png make_index.php

<?php

## Make a set of symbolic links for mythtv recordings
## with program names and dates so they can be used by other media players
##
## by Ed ODonnell
##
## Version 00.01 - First draft
##         00.02 - Added metadata file for appletv Media player

# real mythtv recordings directory (note it ends in /)
$realdir = "/var/lib/mythtv/recordings/";

# Directory to make links in (note it ends in /)
$linkdir = "/var/lib/mythtv/index/";

# Mythtv Database details
$dbserver = "localhost";
$dbname = "mythconverg";
$dbuser = "mythtv";
$dbpasswd = "mythtv";

#First lets check the detination directory exists 
if (!is_dir($linkdir)) {
	if (!mkdir($linkdir, 0777, true)) {
	    die('Failed to create folder');
	}
}

# Next we connect to the database 
mysql_connect($dbserver,$dbuser,$dbpasswd);
mysql_select_db($dbname) or die( "Unable to select database");

# Setup a query to return the names of the programs and their filenames
$query="select chanid, starttime, title, subtitle, description FROM recordedprogram ;"; 
$result=mysql_query($query);
$num=mysql_numrows($result);

# Now loop through each program
$i=0;
while ($i < $num) {
	$chanid=mysql_result($result,$i,"chanid");
	$starttime=mysql_result($result,$i,"starttime");
	$title=mysql_result($result,$i,"title");
	$subtitle=mysql_result($result,$i,"subtitle");
	$description=mysql_result($result,$i,"description");

	# Take just the date from 'startdate' as it also has the time
	$startdate = substr($starttime,0,10);

	# Compile the existing filename from the chanid and the date
	$infilename = $chanid . "_" . substr($starttime,0,4) . substr($starttime,5,2) . substr($starttime,8,2) . substr($starttime,11,2) . substr($starttime,14,2) . substr($starttime,17,2) . ".mpg";
	
	# strip out characters we cannot have in filenames	
	$title = preg_replace('/[^\w\-~_]+/u', '-', $title);

	# get the file names ready for in and out
	$infile = "$realdir" . "$infilename";
	$outfilepath = "$linkdir" . "$title/$startdate";
	$outfile = "$outfilepath" . "/" . "$title-$startdate" . ".mpg";
	$metadata = "$outfilepath" . "/" . "$title-$startdate" . ".xml";

	# create link folder
	if (!is_dir($outfilepath)) {
	        if (!mkdir($outfilepath, 0777, true)) {
	            die('Failed to create link folder\n');
	        }
	}

	# and create a hard link (note I do not use -S for symbolic so it works
	# on firecore media player on an apple tv box. add the -s if you need it
	exec ("ln -v $infile $outfile");

	# and create an xml file wit the metadata in it for appletv box
	$metaHandle = fopen($metadata, 'w') or die("can't open file");
	fwrite($metaHandle, "<media type=\"TV Program\">\n");
	fwrite($metaHandle, "  <title>$title</title>\n");
	fwrite($metaHandle, "  <description>$startdate : $subtitle\n");
	fwrite($metaHandle, "  $description</description>\n");
	fwrite($metaHandle, "</media>\n\n");
	fclose($metaHandle);	

	$i++;
}
# and close the database now we are finished with it
mysql_close();

?>



Warning.png Warning: Note use of hard links

When hard links are made to recordings, it is vital that the recordings drives are manually kept clean, or the auto-expirer is disabled. The auto-expire code does not check to make sure there are no other references to a recording's inode, and assumes if it deletes a file to make room, that room will be cleared up. Since a hard link means a deleted file is just a deleted reference, the expirer will get caught in a death spiral, deleting every single recording on that drive from the database, before eventually terminating the recording due to lack of free space.

The FireCore Media player that I tested on does not work with symbolic links so I took the "-s" off the link command. This make a new filename pointer which points at the same data area on the disk (the same file). If you delete one the other will be left alone.