00001 #!/usr/bin/perl -w
00002 # dvbradioexport.pl v2.0
00003 # By Justin Hornsby 22 October 2006
00004 #
00005 # Storage Group Support by Stuart Auchterlonie 28 April 2007
00006 #
00007 # A MythTV user job script for exporting DVB radio recordings to MP3 format
00008 #
00009 # Usage info will be output if the script is run with no arguments (or insufficient arguments)
00010 #
00011 # Contains elements of mythtv.pl by Nigel Pearson
00012 # Initial idea from nuvexport
00013 #
00014 # requirements: id3tag, ffmpeg with mp3 exporting enabled, PERL and the DBI & DBD::mysql modules
00015 # MythTV perl bindings.
00016 #
00017
00018 # PERL MODULES WE WILL BE USING
00019 use DBI;
00020 use DBD::mysql;
00021 use MythTV;
00022
00023 my $exportdir = '/home/mythtv/';
00024 my $maxbitrate = '256';
00025 my $bitrate = '192';
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\ndvbradioexport.pl exportdir=/foo/bar starttime=%STARTTIME%
00037 chanid=%CHANID% maxbitrate=x 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 MP3 files to (note the user the script runs as must have write permission on that
00041 dir)\n"
00042 ."maxbitrate = maximum bitrate for the export to use. If more than the original file's bitrate, the original
00043 bitrate will be used \n"
00044 ."debug = enable debugging information - outputs which commands would be run etc\n";
00045
00046 # get this script's ARGS
00047 #
00048
00049 $num = $#ARGV + 1;
00050
00051 # if user hasn't passed enough arguments, die and print the usage info
00052
00053 if ($num le "2") {
00054 die "$usage";
00055 }
00056
00057 #
00058 # Get all the arguments
00059 #
00060
00061 foreach (@ARGV){
00062 if ($_ =~ m/debug/) {
00063 $debug = 1;
00064 }
00065 elsif ($_ =~ m/maxbitrate/) {
00066 $maxbitrate = (split(/\=/,$_))[1];
00067 }
00068 elsif ($_ =~ m/starttime/) {
00069 $starttime = (split(/\=/,$_))[1];
00070 }
00071 elsif ($_ =~ m/chanid/) {
00072 $chanid = (split(/\=/,$_))[1];
00073 }
00074 elsif ($_ =~ m/exportdir/) {
00075 $exportdir = (split(/\=/,$_))[1];
00076 }
00077 }
00078
00079 # connect to backend
00080 my $myth = new MythTV();
00081 # connect to database
00082 $connect = $myth->{'dbh'};
00083
00084 # PREPARE THE QUERY
00085 $query = "SELECT title, subtitle, basename FROM recorded WHERE chanid=$chanid AND starttime='$starttime'";
00086 $query_handle = $connect->prepare($query);
00087 $query_handle->execute() || die "Cannot connect to database \n";
00088
00089 # BIND TABLE COLUMNS TO VARIABLES
00090 $query_handle->bind_columns(undef, \$title, \$subtitle, \$basename);
00091
00092 # LOOP THROUGH RESULTS
00093 $query_handle->fetch();
00094
00095 my $schemaVer = $myth->backend_setting('DBSchemaVer');
00096 # Storage Groups were added in DBSchemaVer 1171
00097 # FIND WHERE THE RECORDINGS LIVE
00098 my $dir = 'UNKNOWN';
00099 if ($schemaVer < 1171)
00100 {
00101 if ($debug) {
00102 print ("Using compatibility mode\n");
00103 }
00104 $dir = $myth->backend_setting('RecordFilePrefix');
00105 }
00106 else
00107 {
00108 if ($debug) {
00109 print ("Going into new mode\n");
00110 }
00111 my $storagegroup = new MythTV::StorageGroup();
00112 $dir = $storagegroup->FindRecordingDir($basename);
00113 }
00114
00115 # FIND OUT THE CHANNEL NAME
00116 $query = "SELECT name FROM channel WHERE chanid=$chanid";
00117 $query_handle = $connect->prepare($query);
00118 $query_handle->execute() || die "Unable to query settings table";
00119
00120 my ($channame) = $query_handle->fetchrow_array;
00121
00122 # replace whitespace in channame with dashes
00123 $channame =~ s/\s+/-/g;
00124
00125 # replace whitespace in title with underscores
00126 $title =~ s/\W+/_/g;
00127 # replace whitespace in subtitle with underscores
00128 $subtitle =~ s/\W+/_/g;
00129
00130 #
00131 # Remove non alphanumeric chars from $starttime & $endtime
00132 #
00133
00134 $newstarttime = $starttime;
00135
00136 $newstarttime =~ s/[|^\W|\s|-|]
00137
00138 $year = substr($newstarttime, 0, 4);
00139
00140 $filename = $dir."/".$basename;
00141
00142 $newfilename = $exportdir."/".$channame."_".$title."_".$subtitle."_".$newstarttime.".mp3";
00143
00144 if ($debug)
00145 {
00146 print "\n\n Source filename:$filename \nDestination filename:$newfilename\n \n";
00147 }
00148 #
00149 # Now run ffmpeg to find out what bitrate the stream is
00150 #
00151
00152 $origbitrate = -1;
00153
00154 $output = `ffmpeg -i $filename 2>&1`;
00155
00156 $output =~s/^.*?Audio.*?, (\d*) kb\/s.*$/$1/s;
00157
00158 print $output . "\n";
00159
00160 if ($output =~ /^\d+$/) {
00161 $origbitrate = $output;
00162 }
00163
00164 #
00165 # If maximum bitrate is less than the source bitrate, allow the max bitrate to remain
00166 #
00167
00168 $bitrate = $maxbitrate;
00169
00170 if (($origbitrate != -1) && ($maxbitrate > $origbitrate)) {
00171 print "maxbitrate is greater than original. Using source bitrate to save space\n";
00172 $bitrate = $origbitrate;
00173 }
00174
00175 $bitrate .= "K";
00176
00177 #
00178 # Now run ffmpeg to get mp3 out of the dvb radio recording
00179 #
00180
00181 $command = "nice -n19 ffmpeg -i $filename -ab $bitrate -ac 2 -acodec mp3 -f mp3 '$newfilename' 2>&1";
00182
00183 if ($debug) {
00184 print "\n\nUSING $command \n\n";
00185 }
00186
00187 system "$command";
00188
00189 # Now tag the finished MP3 file
00190
00191 $command = "id3tag --song='$title' --album='$subtitle' --artist='$channame' --comment='Transcoded by MythTV' --year=$year '$newfilename'";
00192
00193 if ($debug) {
00194 print "\n\nUsing $command \n\n";
00195 }
00196 system "$command";