1 | #!/usr/bin/perl -w |
---|
2 | # dvbradioexport.pl |
---|
3 | # By Justin Hornsby 18 August 2006 |
---|
4 | # |
---|
5 | # A MythTV user job script for exporting DVB radio recordings to MP3 format |
---|
6 | # |
---|
7 | # Usage info will be output if the script is run with no arguments (or insufficient arguments) |
---|
8 | # |
---|
9 | # Contains elements of mythtv.pl by Nigel Pearson |
---|
10 | # Initial idea from nuvexport |
---|
11 | # |
---|
12 | # requirements: id3tag, ffmpeg with mp3 exporting enabled, PERL and the DBI & DBD::mysql modules |
---|
13 | # |
---|
14 | |
---|
15 | # PERL MODULES WE WILL BE USING |
---|
16 | use DBI; |
---|
17 | use DBD::mysql |
---|
18 | |
---|
19 | # |
---|
20 | $host = '127.0.0.1'; |
---|
21 | $database = 'mythconverg'; |
---|
22 | $user = 'mythtv'; |
---|
23 | $pass = $user; |
---|
24 | |
---|
25 | $hostname = 'umbongo2'; |
---|
26 | |
---|
27 | $prefix = '/usr/bin'; |
---|
28 | |
---|
29 | $db = undef; |
---|
30 | $connect = undef; |
---|
31 | $debug = 'nodebug'; |
---|
32 | ###################################### |
---|
33 | # # |
---|
34 | # SUBROUTINES ARE HERE # |
---|
35 | # # |
---|
36 | ###################################### |
---|
37 | |
---|
38 | |
---|
39 | # Try to find the database |
---|
40 | # |
---|
41 | sub findDatabase() |
---|
42 | { |
---|
43 | my $found = 0; |
---|
44 | my @mysql = ('/usr/local/share/mythtv/mysql.txt', |
---|
45 | '/usr/share/mythtv/mysql.txt', |
---|
46 | '/etc/mythtv/mysql.txt', |
---|
47 | '/usr/local/etc/mythtv/mysql.txt', |
---|
48 | "$ENV{HOME}/.mythtv/mysql.txt", |
---|
49 | 'mysql.txt' |
---|
50 | ); |
---|
51 | foreach my $file (@mysql) { |
---|
52 | next unless (-e $file); |
---|
53 | $found = 1; |
---|
54 | open(CONF, $file) or die "Unable to open $file: $!\n\n"; |
---|
55 | while (my $line = <CONF>) { |
---|
56 | # Cleanup |
---|
57 | next if ($line =~ /^\s*#/); |
---|
58 | $line =~ s/^str //; |
---|
59 | chomp($line); |
---|
60 | |
---|
61 | # Split off the var=val pairs |
---|
62 | my ($var, $val) = split(/\=/, $line, 2); |
---|
63 | next unless ($var && $var =~ /\w/); |
---|
64 | if ($var eq 'DBHostName') { |
---|
65 | $host = $val; |
---|
66 | } elsif ($var eq 'DBUserName') { |
---|
67 | $user = $val; |
---|
68 | } elsif ($var eq 'DBName') { |
---|
69 | $database = $val; |
---|
70 | } elsif ($var eq 'DBPassword') { |
---|
71 | $pass = $val; |
---|
72 | } |
---|
73 | } |
---|
74 | close CONF; |
---|
75 | } |
---|
76 | |
---|
77 | if ( ! $found ) |
---|
78 | { die "Unable to locate mysql.txt: $!\n\n" } |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | # DB connect subroutine |
---|
83 | # ======================= |
---|
84 | # openDatabase() - Connect or die |
---|
85 | # |
---|
86 | |
---|
87 | sub openDatabase() |
---|
88 | { |
---|
89 | $db = DBI->connect("dbi:mysql:database=$database" . |
---|
90 | ":host=$host", |
---|
91 | $user, $pass) |
---|
92 | or die "Cannot connect to database: $!\n\n"; |
---|
93 | |
---|
94 | return $db; |
---|
95 | } |
---|
96 | |
---|
97 | ################################## |
---|
98 | # # |
---|
99 | # Main code starts here !! # |
---|
100 | # # |
---|
101 | ################################## |
---|
102 | # |
---|
103 | # |
---|
104 | # FIND THE DATABASE SETTINGS |
---|
105 | |
---|
106 | $usage = "\nHow to use dvbradioexport.pl \n\ndvbradioexport.pl %CHANID% %STARTTIME% %ENDTIME% exportdir bitrate --debug\n" |
---|
107 | ."\n%CHANID% = channel ID associated with the recording to export\n" |
---|
108 | ."%STARTTIME% = recording start time in either 'yyyy-mm-dd hh:mm:ss' or 'yyyymmddhhmmss' format\n" |
---|
109 | ."%ENDTIME = as %STARTTIME% but for the recording end time\n" |
---|
110 | ."exportdir = dir to export completed MP3 files to (note the user the script runs as must have write permission on that dir\n" |
---|
111 | ."bitrate = CBR bitrate in kbps (e.g. 192)\n" |
---|
112 | ."--debug = enable debugging information - outputs which commands would be run etc\n"; |
---|
113 | |
---|
114 | findDatabase(); |
---|
115 | |
---|
116 | # get this script's ARGS |
---|
117 | # |
---|
118 | |
---|
119 | $num = $#ARGV + 1; |
---|
120 | |
---|
121 | # if user hasn't passed enough arguments, die and print the usage info |
---|
122 | |
---|
123 | if ($num le "4") { |
---|
124 | die "$usage"; |
---|
125 | } |
---|
126 | |
---|
127 | $chanid = $ARGV[0]; |
---|
128 | $starttime = $ARGV[1]; |
---|
129 | $endtime = $ARGV[2]; |
---|
130 | $exportdir = $ARGV[3]; |
---|
131 | $bitrate = $ARGV[4]; |
---|
132 | |
---|
133 | |
---|
134 | if ($num eq "6") { |
---|
135 | $debug = $ARGV[5]; |
---|
136 | } |
---|
137 | |
---|
138 | if($debug eq "--debug") |
---|
139 | { |
---|
140 | print "\n\n*********************************************************************************************\n"; |
---|
141 | print " EXPORT DVB RADIO SCRIPT IS RUN WITH $chanid $starttime $endtime $exportdir $bitrate \n"; |
---|
142 | print "*********************************************************************************************\n"; |
---|
143 | } |
---|
144 | # |
---|
145 | # connect to database |
---|
146 | # |
---|
147 | $connect = openDatabase(); |
---|
148 | |
---|
149 | # PREPARE THE QUERY |
---|
150 | $query = "SELECT title, subtitle, description FROM recorded WHERE chanid=$chanid AND starttime='$starttime'"; |
---|
151 | |
---|
152 | $query_handle = $connect->prepare($query); |
---|
153 | |
---|
154 | |
---|
155 | # EXECUTE THE QUERY |
---|
156 | $query_handle->execute() || die "Cannot connect to database \n"; |
---|
157 | |
---|
158 | # BIND TABLE COLUMNS TO VARIABLES |
---|
159 | $query_handle->bind_columns(undef, \$title, \$subtitle, \$description); |
---|
160 | |
---|
161 | # LOOP THROUGH RESULTS |
---|
162 | $query_handle->fetch(); |
---|
163 | |
---|
164 | # FIND WHERE THE RECORDINGS LIVE |
---|
165 | $query = "SELECT data FROM settings WHERE value='RecordFilePrefix' AND hostname='$hostname'"; |
---|
166 | |
---|
167 | $query_handle = $connect->prepare($query); |
---|
168 | $query_handle->execute() || die "Unable to query settings table"; |
---|
169 | |
---|
170 | my ($dir) = $query_handle->fetchrow_array; |
---|
171 | |
---|
172 | # FIND OUT THE CHANNEL NAME |
---|
173 | $query = "SELECT name FROM channel WHERE chanid=$chanid"; |
---|
174 | $query_handle = $connect->prepare($query); |
---|
175 | $query_handle->execute() || die "Unable to query settings table"; |
---|
176 | |
---|
177 | my ($channame) = $query_handle->fetchrow_array; |
---|
178 | |
---|
179 | # replace whitespace in channame with dashes |
---|
180 | $channame =~ s/\s+/-/g; |
---|
181 | |
---|
182 | # replace whitespace in title with underscores |
---|
183 | $title =~ s/\W+/_/g; |
---|
184 | # replace whitespace in subtitle with underscores |
---|
185 | $subtitle =~ s/\W+/_/g; |
---|
186 | |
---|
187 | # |
---|
188 | # Remove non alphanumeric chars from $starttime & $endtime |
---|
189 | # |
---|
190 | |
---|
191 | $newstarttime = $starttime; |
---|
192 | |
---|
193 | $newstarttime =~ s/[|^\W|\s|-|]//g; |
---|
194 | |
---|
195 | $newendtime = $endtime; |
---|
196 | $newendtime =~ s/[|^\W|\s|-|]//g; |
---|
197 | |
---|
198 | $filename = $dir."/".$chanid."_".$newstarttime.".mpg"; |
---|
199 | |
---|
200 | $newfilename = $exportdir."/".$channame."_".$title."_".$subtitle."_".$newstarttime.".mp3"; |
---|
201 | |
---|
202 | if ($debug eq "--debug") { |
---|
203 | print "\nSource filename:$filename \nDestination filename:$newfilename\n"; |
---|
204 | } |
---|
205 | |
---|
206 | # |
---|
207 | # Estimate the final size of the MP3 file |
---|
208 | # |
---|
209 | |
---|
210 | # get year from starttime |
---|
211 | $year = substr($newstarttime, 0, 4); |
---|
212 | |
---|
213 | # get hour from starttime |
---|
214 | $starthour = substr($newstarttime, 8,2); |
---|
215 | $startminute = substr($newstarttime,10,2); |
---|
216 | $startsecond = substr($newstarttime,12,2); |
---|
217 | |
---|
218 | $endhour = substr($newendtime, 8,2); |
---|
219 | $endminute = substr($newendtime,10,2); |
---|
220 | $endsecond = substr($newendtime,12,2); |
---|
221 | |
---|
222 | $dhours = $endhour - $starthour; |
---|
223 | $dmins = $endminute - $startminute; |
---|
224 | $dsecs = $endsecond - $startsecond; |
---|
225 | |
---|
226 | $duration = ($dhours * 3600) + ($dmins * 60) + $dsecs; |
---|
227 | |
---|
228 | $newfilesize = $bitrate * $duration /8; |
---|
229 | |
---|
230 | # now check there is sufficient space in $exportdir (i.e. greater than $newfilesize) |
---|
231 | |
---|
232 | open(CMD, "/bin/df -k $exportdir |"); |
---|
233 | while(<CMD>) |
---|
234 | { |
---|
235 | next unless /\d/; |
---|
236 | @f = split(/\s+/); |
---|
237 | $available = $f[3]; |
---|
238 | } |
---|
239 | |
---|
240 | if ( $available > $newfilesize ) |
---|
241 | { |
---|
242 | if($debug eq "--debug") |
---|
243 | { |
---|
244 | print "\nThere is $available"."KB space in $exportdir, enough for $filename of $newfilesize"."KB \n"; |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | else |
---|
249 | { |
---|
250 | die "There is insufficient room for $filename of $newfilesize"."KB"." in $exportdir, which has $available"."KB free \n"; |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | # |
---|
255 | # Now run ffmpeg to get mp3 out of the dvb radio recording |
---|
256 | # |
---|
257 | |
---|
258 | $command = $prefix."/nice -n19 ffmpeg -i $filename -ab 192 -acodec mp3 -f mp3 '$newfilename' 2>&1"; |
---|
259 | |
---|
260 | if ($debug eq "--debug") { |
---|
261 | print "\nUSING $command \n"; |
---|
262 | } |
---|
263 | |
---|
264 | system "$command"; |
---|
265 | |
---|
266 | # Now tag the finished MP3 file |
---|
267 | |
---|
268 | $command = "id3tag --song=$title --album=$subtitle --artist=$channame --comment='$description' --year=$year $newfilename"; |
---|
269 | |
---|
270 | if ($debug eq "--debug") { |
---|
271 | print "\nUsing $command \n"; |
---|
272 | } |
---|
273 | system "$command"; |
---|