Ticket #2233: dvbradioexport.3.pl

File dvbradioexport.3.pl, 4.8 KB (added by Stuart Auchterlonie, 17 years ago)

Updated to use perl bindings and work with storage groups.

Line 
1#!/usr/bin/perl -w
2# dvbradioexport.pl v2.0
3# By Justin Hornsby 22 October 2006
4#
5# Storage Group Support by Stuart Auchterlonie  28 April 2007
6#
7# A MythTV user job script for exporting DVB radio recordings to MP3 format
8#
9# Usage info will be output if the script is run with no arguments (or insufficient arguments)
10#
11# Contains elements of mythtv.pl by Nigel Pearson
12# Initial idea from nuvexport
13#
14# requirements: id3tag, ffmpeg with mp3 exporting enabled, PERL and the DBI & DBD::mysql modules
15#
16
17# PERL MODULES WE WILL BE USING
18use DBI;
19use DBD::mysql;
20use MythTV;
21
22my $exportdir = '/home/mythtv/';
23my $maxbitrate = '256';
24my $bitrate = '192';
25
26$connect = undef;
27$debug = 0;
28
29##################################
30#                                #
31#    Main code starts here !!    #
32#                                #
33##################################
34
35$usage = "\nHow to use dvbradioexport.pl \n\ndvbradioexport.pl exportdir=/foo/bar starttime=%STARTTIME%
36chanid=%CHANID maxbitrate=x debug\n"
37        ."\n%CHANID% = channel ID associated with the recording to export\n"
38        ."%STARTTIME% = recording start time in either 'yyyy-mm-dd hh:mm:ss' or 'yyyymmddhhmmss' format\n"
39        ."exportdir = dir to export completed MP3 files to (note the user the script runs as must have write permission on that dir\n"
40        ."maxbitrate = maximum bitrate for the export to use.  If more than the original file's bitrate, the original
41bitrate will be used \n"
42        ."debug = enable debugging information - outputs which commands would be run etc\n";
43
44# get this script's ARGS
45#
46
47$num = $#ARGV + 1;
48
49# if user hasn't passed enough arguments, die and print the usage info
50
51if ($num le "2") {
52        die "$usage";
53}
54
55#
56# Get all the arguments
57#
58
59foreach (@ARGV){
60        if ($_ =~ m/debug/) {
61                $debug = 1;
62        }
63        elsif ($_ =~ m/maxbitrate/) {
64                $maxbitrate = (split(/\=/,$_))[1];
65        }
66        elsif ($_ =~ m/starttime/) {
67                $starttime = (split(/\=/,$_))[1];
68        }
69        elsif ($_ =~ m/chanid/) {
70                $chanid = (split(/\=/,$_))[1];
71        }
72        elsif ($_ =~ m/exportdir/) {
73                $exportdir = (split(/\=/,$_))[1];
74        }
75}
76
77# connect to backend
78my $myth = new MythTV();
79# connect to database
80$connect = $myth->{'dbh'};
81
82# PREPARE THE QUERY
83$query = "SELECT title, subtitle, basename FROM recorded WHERE chanid=$chanid AND starttime='$starttime'";
84$query_handle = $connect->prepare($query);
85$query_handle->execute() || die "Cannot connect to database \n";
86
87# BIND TABLE COLUMNS TO VARIABLES
88$query_handle->bind_columns(undef, \$title, \$subtitle, \$basename);
89
90# LOOP THROUGH RESULTS
91$query_handle->fetch();
92
93my $schemaVer = $myth->backend_setting('DBSchemaVer');
94# Storage Groups were added in DBSchemaVer 1171
95# FIND WHERE THE RECORDINGS LIVE
96my $dir = 'UNKNOWN';
97if ($schemaVer < 1171)
98{
99    if ($debug) {
100        print ("Using compatibility mode\n");
101    }
102    $dir = $myth->backend_setting('RecordFilePrefix');
103}
104else
105{
106    if ($debug) {
107        print ("Going into new mode\n");
108    }
109    my $storagegroup = new MythTV::StorageGroup();
110    $dir = $storagegroup->FindRecordingDir($basename);
111}
112
113# FIND OUT THE CHANNEL NAME
114$query = "SELECT name FROM channel WHERE chanid=$chanid";
115$query_handle = $connect->prepare($query);
116$query_handle->execute()  || die "Unable to query settings table";
117
118my ($channame) = $query_handle->fetchrow_array;
119
120# replace whitespace in channame with dashes
121$channame =~ s/\s+/-/g;
122
123# replace whitespace in title with underscores
124$title =~ s/\W+/_/g;
125# replace whitespace in subtitle with underscores
126$subtitle =~ s/\W+/_/g;
127
128#
129# Remove non alphanumeric chars from $starttime & $endtime
130#
131 
132$newstarttime = $starttime;
133
134$newstarttime =~ s/[|^\W|\s|-|]//g;
135
136$year = substr($newstarttime, 0, 4);
137
138$filename = $dir."/".$chanid."_".$newstarttime.".mpg";
139
140$newfilename = $exportdir."/".$channame."_".$title."_".$subtitle."_".$newstarttime.".mp3";
141
142if ($debug)
143{
144        print "\n\n Source filename:$filename \nDestination filename:$newfilename\n \n";
145}
146#
147# Now run ffmpeg to find out what bitrate the stream is
148#
149
150$output = `ffmpeg -i $filename 2>&1`;
151
152@words = split(/Audio/, $output);
153@br = split(/\,/, $words[1]);
154@out = split(/kb/, $br[3]);
155
156$origbitrate = $out[0];
157
158#
159# If maximum bitrate is less than the source bitrate, allow the max bitrate to remain
160#
161
162$bitrate = $maxbitrate;
163
164if ($maxbitrate > $origbitrate) {
165    print "maxbitrate is greater than original.  Using source bitrate to save space\n";
166    $bitrate = $origbitrate;
167}
168
169#
170# Now run ffmpeg to get mp3 out of the dvb radio recording
171#
172
173$command = "nice -n19 ffmpeg -i $filename -ab $bitrate -acodec mp3 -f mp3 '$newfilename' 2>&1";
174
175if ($debug) {
176    print "\n\nUSING $command \n\n";
177}
178
179system "$command";
180
181# Now tag the finished MP3 file
182
183$command = "id3tag --song='$title' --album='$subtitle' --artist='$channame' --comment='Transcoded by MythTV' --year=$year '$newfilename'";
184
185if ($debug) {
186    print "\n\nUsing $command \n\n";
187}
188system "$command";