[mythtv-users] Export to XBMC?

David Snider david at davidsnider.org
Wed Jan 30 03:15:56 UTC 2013


On Jan 29, 2013, at 3:20 PM, Frank Feuerbacher <fbacher at brisbin.net> wrote:

> I'm using myth to record movies that I want to encode and then import into XBMC. We have been using XBMC for years and reluctant to migrate. I have seen several tools mentioned for exporting, but a lot of the notes I read appear a bit dated. I'm seeking suggestions on what tools or techniques I consider pursuing.
> 
> Ideally I would like the script to grab all of the latest movies (not TV shows) from Mythtv, convert to mpeg2, name them appropriately for XBMC (Movie_name (release_Year)), place them in a directory and delete the mythtv entry. I will then take care of editing, encoding and importing the file into XBMC. I would like to have the option to edit in Mythtv, but I am having trouble with the transcode command (there is a defect open for this). 
> 
> My guess is that I will end up hacking mythbrake that I have already hacked a bit on. 
> 
This might get you started…  I use this to export shows to Plex, should work just as well for XBMC.  I use it as a user job, and only select it for the recordings that I want to export.   You could tweak it to look for movies I suppose, or just add it as a user job for the movie recording rules you want.

$ cat ExportToPlex.pl 
#!/usr/bin/perl
#
# ExportToPlex.pl
# version 1.0 - First revision
#
# See 'sub usage' for detailed description of what the script does.

use strict;
use warnings;

sub usage {
	print "$0:\n";
	print "
	This Script exports files to a central share in a naming format that Plex likes.
	
	Required arguments:
		-starttime		UTC String of recording start time
		-chanid			Channel ID from MythTVs
		-backend		hostname or IP address of your backend 
		-destination	Location in which exports will reside
	\n";
	exit;

}

# use modules
use LWP::UserAgent;
use HTTP::Request;
use Getopt::Long;
use XML::Simple;
use File::Spec;

#Handle command line arguments
my $numArgs = $#ARGV + 1;
logit("Number of arguments = $numArgs");

my ( $GetOptResult, $help, $starttime, $chanid, $test, $backend, $destination );

#-- prints usage if no command line parameters are passed or there is an unknown
#   parameter or help option is passed
$GetOptResult = GetOptions(
	'help|?'        => \$help,
	'starttime=s'   => \$starttime,      #Start time of recording
	'chanid=s'      => \$chanid,         #chanid of recording
	'backend=s'     => \$backend,        #Backend of the server
	'destination=s' => \$destination,    #Destination folder
	'test' => \$test,    #show what would be done, don't actually do it.
);

usage()
  if ( ( $numArgs < 1 )
	or ( defined $help )
	or ( !$GetOptResult )
	or ( !$starttime )
	or ( !$chanid )
	or ( !$backend )
	or ( !$destination ) );

logit("StartTime = $starttime");
logit("Chanid = $chanid");
logit("Backend = $backend");
logit("destination = $destination");
logit("test = $test") if ( defined $test );
my $wget = `bash -l -c "which wget"` or die "Couldn't find wget";
chomp $wget;
logit("wget = $wget");

#Get recording info
my $agent =
  LWP::UserAgent->new( env_proxy => 1, keep_alive => 1, timeout => 30 );
my $url =
"http://$backend:6544/Dvr/GetRecorded?StartTime=${starttime}&ChanId=${chanid}";
logit("Recording info URL = $url");

my $header = HTTP::Request->new( GET => $url );
my $request = HTTP::Request->new( 'GET', $url, $header );
my $response = $agent->request($request);

# create object
my $xml = new XML::Simple;

if ( $response->is_success ) {

	# read XML file
	my $data  = $xml->XMLin( $response->content );
	my $Title = $data->{Title};
	logit("Title = $Title");
	my $Series = sprintf "%02d", $data->{Season};
	logit("Series = $Series");
	my $Episode = sprintf "%02d", $data->{Episode};
	logit("Episode = $Episode");
	my $ext = $data->{FileName};
	$ext =~ s{.*\.}{};
	logit("Extension = $ext");
	my $Path;
	my $FileName;
	my $Airdate = $data->{Airdate};
	logit("Original Air Date = $Airdate");
	my $MetaData;

#Do we have series data? Name it like: Show Name/Series 1/Show Name - s01e01.mpg
	if ( ( $Series + $Episode ) != 0 ) {
		$Path = $destination . "/" . $Title . "/" . "Season " . $Series . "/";
		$FileName = $Title . " - s" . $Series . "e" . $Episode . "." . $ext;
		$MetaData = $Title . " - s" . $Series . "e" . $Episode . ".xml";
	}
	else {

		#If not? Name it like: Show Name/ShowName - YYYY-MM-DD.ext
		$Path     = $destination . "/" . $Title . "/";
		$FileName = $Title . " - " . $Airdate . "." . $ext;
		$MetaData = $Title . " - " . $Airdate . ".xml";
	}
	logit("Path = $Path");
	logit("Filename = $FileName");
	logit("Meta Data Filename = $MetaData");

	my $FullPath = $Path . $FileName;
	logit("Video file full path = $FullPath");

	# print output
	if ( defined $test ) {
		print "Information found\n";
		print "\tShow Name:\t$Title\n";
		print "\tSeries Number:\t$Series\n";
		print "\tPath:\t\t$Path\n";
		print "\tFilename:\t$FileName\n";
		print "\tMetaData:\t$MetaData\n";
		print "\tFullPath:\t$FullPath\n";
	}

	#Make sure the directory exits, if it doesn't, make it
	system("mkdir -p \"$Path\"")
	  unless -d $Path;
	  
	#Download the file
	$url =
"http://$backend:6544/Content/GetRecording?ChanId=$chanid&StartTime=$starttime";
	logit("Download URL = $url");
	`$wget -nv \"$url\" -O \"$FullPath\"`;

#Create a metadata file to track back to MythTV (put the basename column data in it)
	$FullPath = $Path . $MetaData;
	logit("Metatdata file full path = $FullPath");
	my $hashref = {
		starttime => $starttime,
		chanid    => $chanid,
		filename  => $Path . $FileName,
		path      => $Path,
	};
	my $xml = XML::Simple::XMLout($hashref);
	open MYFILE, ">$FullPath" or die $!;
	print MYFILE $xml;
	close MYFILE;
}
else {

	#Web services didn't return any data
	die " Unable to contact backend at $url\n ";
}

#Functions

sub logit {
	my $s      = shift;
	my $logdir = "/tmp";
	my (
		$logsec,  $logmin,  $loghour, $logmday, $logmon,
		$logyear, $logwday, $logyday, $logisdst
	) = localtime(time);
	my $logtimestamp = sprintf(
		"%4d-%02d-%02d %02d:%02d:%02d",
		$logyear + 1900,
		$logmon + 1,
		$logmday, $loghour, $logmin, $logsec
	);
	$logmon++;
	my $logfile = "$logdir/ExportToPlex-$logmon-$logmday-logfile.log";
	my $fh;
	open( $fh, '>>', "$logfile" ) or die "$logfile: $!";
	print $fh "$logtimestamp $s\n";
	close($fh);
}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.mythtv.org/pipermail/mythtv-users/attachments/20130129/57b307d7/attachment.html>


More information about the mythtv-users mailing list