00001 #!/usr/bin/perl -w
00002 # ipodexport.pl v1.0
00003 # By Justin Hornsby 27 July 2007
00004 #
00005 # A MythTV user job script for exporting MythTV recordings to iPod & other xvid/aac formats
00006 #
00007 # Usage info will be output if the script is run with no arguments (or insufficient arguments)
00008 #
00009 # Contains elements of mythtv.pl by Nigel Pearson
00010 # Initial idea from nuvexport
00011 #
00012 # Requirements: ffmpeg with aac & xvid support enabled, perl and the DBI & DBD::mysql modules
00013 # Also requires MythTV perl bindings
00014 #
00015 #
00016 # PERL MODULES WE WILL BE USING
00017 use DBI;
00018 use DBD::mysql;
00019 use MythTV;
00020
00021 # Set default values
00022 my $exportdir = '/home/mythtv/';
00023 my $bitrate = '96';
00024 my $aspect = '4:3';
00025 my $size = '320x240';
00026
00027 $connect = undef;
00028 $debug = 0;
00029
00030 ##################################
00031 # #
00032 # Main code starts here !! #
00033 # #
00034 ##################################
00035
00036 $usage = "\nHow to use dvbradioexport.pl : \n"
00037 ."ipodexport.pl exportdir=/foo/bar starttime=%STARTTIME% chanid=%CHANID bitrate=x size=320x240 aspect=4:3 debug\n"
00038 ."\n%CHANID% = channel ID associated with the recording to export\n"
00039 ."%STARTTIME% = recording start time in either 'yyyy-mm-dd hh:mm:ss' or 'yyyymmddhhmmss' format\n"
00040 ."exportdir = dir to export completed MP4 files to (note the user the script runs as must have write permission on that dir\n"
00041 ."size = frame size of output file. 320x240 is the default value \n"
00042 ."aspect = aspect ratio of output file. Valid values are 4:3 (default) and 16:9 \n"
00043 ."bitrate = audio bitrate in output file in kbps. Default value is 96 \n"
00044 ."debug = enable debugging information - outputs which commands would be run etc\n"
00045 ."\nExample: ipodexport.pl exportdir=/home/juski starttime=20060803205900 chanid=1006 size=320x340 aspect=16:9 bitrate=192 debug \n";
00046
00047 # get this script's ARGS
00048 #
00049
00050 $num = $#ARGV + 1;
00051
00052 # if user hasn't passed enough arguments, die and print the usage info
00053
00054 if ($num le "2") {
00055 die "$usage";
00056 }
00057
00058 #
00059 # Get all the arguments
00060 #
00061
00062 foreach (@ARGV){
00063 if ($_ =~ m/debug/) {
00064 $debug = 1;
00065 }
00066 elsif ($_ =~ m/size/) {
00067 $size = (split(/\=/,$_))[1];
00068 }
00069 elsif ($_ =~ m/aspect/) {
00070 $aspect = (split(/\=/,$_))[1];
00071 }
00072 elsif ($_ =~ m/bitrate/) {
00073 $bitrate = (split(/\=/,$_))[1];
00074 }
00075 elsif ($_ =~ m/starttime/) {
00076 $starttime = (split(/\=/,$_))[1];
00077 }
00078 elsif ($_ =~ m/chanid/) {
00079 $chanid = (split(/\=/,$_))[1];
00080 }
00081 elsif ($_ =~ m/exportdir/) {
00082 $exportdir = (split(/\=/,$_))[1];
00083 }
00084 }
00085
00086 #
00087 #
00088 my $myth = new MythTV();
00089 # connect to database
00090 $connect = $myth->{'dbh'};
00091
00092 # PREPARE THE QUERY
00093 $query = "SELECT title, subtitle FROM recorded WHERE chanid=$chanid AND starttime='$starttime'";
00094
00095 $query_handle = $connect->prepare($query);
00096
00097
00098 # EXECUTE THE QUERY
00099 $query_handle->execute() || die "Cannot connect to database \n";
00100
00101 # BIND TABLE COLUMNS TO VARIABLES
00102 $query_handle->bind_columns(undef, \$title, \$subtitle);
00103
00104 # LOOP THROUGH RESULTS
00105 $query_handle->fetch();
00106
00107 my $schemaVer = $myth->backend_setting('DBSchemaVer');
00108 # Storage Groups were added in DBSchemaVer 1171
00109 # FIND WHERE THE RECORDINGS LIVE
00110 my $dir = 'UNKNOWN';
00111 if ($schemaVer < 1171)
00112 {
00113 if ($debug) {
00114 print ("Using compatibility mode\n");
00115 }
00116 $dir = $myth->backend_setting('RecordFilePrefix');
00117 }
00118 else
00119 {
00120 if ($debug) {
00121 print ("Going into new mode\n");
00122 }
00123 my $storagegroup = new MythTV::StorageGroup();
00124 $dir = $storagegroup->FindRecordingDir($basename);
00125 }
00126
00127 # FIND OUT THE CHANNEL NAME
00128 $query = "SELECT name FROM channel WHERE chanid=$chanid";
00129 $query_handle = $connect->prepare($query);
00130 $query_handle->execute() || die "Unable to query settings table";
00131
00132 my ($channame) = $query_handle->fetchrow_array;
00133
00134 # replace whitespace in channame with dashes
00135 $channame =~ s/\s+/-/g;
00136
00137 # replace non-word characters in title with underscores
00138 $title =~ s/\W+/_/g;
00139 # replace non-word characters in subtitle with underscores
00140 $subtitle =~ s/\W+/_/g;
00141
00142 #
00143 # Remove non alphanumeric chars from $starttime & $endtime
00144 #
00145
00146 $newstarttime = $starttime;
00147 $newstarttime =~ s/[|^\W|\s|-|]
00148 $filename = $dir."/".$chanid."_".$newstarttime.".mpg";
00149 $newfilename = $exportdir."/".$channame."_".$title."_".$subtitle."_".$newstarttime.".mp4";
00150
00151 if ($debug) {
00152 print "\n\n Source filename:$filename \nDestination filename:$newfilename\n \n";
00153 }
00154
00155 #
00156 # Now run ffmpeg to get iPod compatible video with a simple ffmpeg line
00157
00158 $command = "nice -n19 ffmpeg -i $filename -vcodec xvid -b 300 -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac -ab $bitrate -s $size -aspect $aspect '$newfilename' 2>&1";
00159
00160 if ($debug) {
00161 print "\n\nUSING $command \n\n";
00162 }
00163
00164 if (!$debug) {
00165 system "$command";
00166 }