Make index.php

From MythTV Official Wiki
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.


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

or run it from a cron file - maybe something like this :

rm -rf /mythtv/index/*
php -f /mythtv/make_index.php

The Code

Script.png make_index.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
##         00.03 - Added starttime to filenames - stops problems if multiple shows per night.

# 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_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 ;"; 

# Now loop through each program
while ($i < $num) {

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

	# 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-$sttime" . ".mpg";
	$metadata = "$outfilepath" . "/" . "$title-$startdate-$sttime" . ".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");

# and close the database now we are finished with it


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.