1 | #!/usr/bin/perl -w |
---|
2 | # dvbradioexport.pl v2.0 |
---|
3 | # By Justin Hornsby 22 October 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 = 'mythback'; |
---|
26 | $exportdir = '/home/mythtv/'; |
---|
27 | $maxbitrate = '256'; |
---|
28 | $bitrate = '192'; |
---|
29 | $prefix = '/usr/bin'; |
---|
30 | |
---|
31 | $dir = '/video/videorec/'; # Where the recordings are |
---|
32 | |
---|
33 | $db = undef; |
---|
34 | $connect = undef; |
---|
35 | $debug = 0; |
---|
36 | ###################################### |
---|
37 | # # |
---|
38 | # SUBROUTINES ARE HERE # |
---|
39 | # # |
---|
40 | ###################################### |
---|
41 | |
---|
42 | |
---|
43 | # Try to find the database |
---|
44 | # |
---|
45 | sub findDatabase() |
---|
46 | { |
---|
47 | my $found = 0; |
---|
48 | my @mysql = ('/usr/local/share/mythtv/mysql.txt', |
---|
49 | '/usr/share/mythtv/mysql.txt', |
---|
50 | '/etc/mythtv/mysql.txt', |
---|
51 | '/usr/local/etc/mythtv/mysql.txt', |
---|
52 | "$ENV{HOME}/.mythtv/mysql.txt", |
---|
53 | 'mysql.txt' |
---|
54 | ); |
---|
55 | foreach my $file (@mysql) { |
---|
56 | next unless (-e $file); |
---|
57 | $found = 1; |
---|
58 | open(CONF, $file) or die "Unable to open $file: $!\n\n"; |
---|
59 | while (my $line = <CONF>) { |
---|
60 | # Cleanup |
---|
61 | next if ($line =~ /^\s*#/); |
---|
62 | $line =~ s/^str //; |
---|
63 | chomp($line); |
---|
64 | |
---|
65 | # Split off the var=val pairs |
---|
66 | my ($var, $val) = split(/\=/, $line, 2); |
---|
67 | next unless ($var && $var =~ /\w/); |
---|
68 | if ($var eq 'DBHostName') { |
---|
69 | $host = $val; |
---|
70 | } elsif ($var eq 'DBUserName') { |
---|
71 | $user = $val; |
---|
72 | } elsif ($var eq 'DBName') { |
---|
73 | $database = $val; |
---|
74 | } elsif ($var eq 'DBPassword') { |
---|
75 | $pass = $val; |
---|
76 | } |
---|
77 | } |
---|
78 | close CONF; |
---|
79 | } |
---|
80 | |
---|
81 | if ( ! $found ) |
---|
82 | { die "Unable to locate mysql.txt: $!\n\n" } |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | # DB connect subroutine |
---|
87 | # ======================= |
---|
88 | # openDatabase() - Connect or die |
---|
89 | # |
---|
90 | |
---|
91 | sub openDatabase() |
---|
92 | { |
---|
93 | $db = DBI->connect("dbi:mysql:database=$database" . |
---|
94 | ":host=$host", |
---|
95 | $user, $pass) |
---|
96 | or die "Cannot connect to database: $!\n\n"; |
---|
97 | |
---|
98 | return $db; |
---|
99 | } |
---|
100 | |
---|
101 | ################################## |
---|
102 | # # |
---|
103 | # Main code starts here !! # |
---|
104 | # # |
---|
105 | ################################## |
---|
106 | # |
---|
107 | # |
---|
108 | # FIND THE DATABASE SETTINGS |
---|
109 | |
---|
110 | $usage = "\nHow to use dvbradioexport.pl \n\ndvbradioexport.pl exportdir=/foo/bar starttime=%STARTTIME% |
---|
111 | chanid=%CHANID maxbitrate=x debug\n" |
---|
112 | ."\n%CHANID% = channel ID associated with the recording to export\n" |
---|
113 | ."%STARTTIME% = recording start time in either 'yyyy-mm-dd hh:mm:ss' or 'yyyymmddhhmmss' format\n" |
---|
114 | ."exportdir = dir to export completed MP3 files to (note the user the script runs as must have write permission on that dir\n" |
---|
115 | ."maxbitrate = maximum bitrate for the export to use. If more than the original file's bitrate, the original |
---|
116 | bitrate will be used \n" |
---|
117 | ."debug = enable debugging information - outputs which commands would be run etc\n"; |
---|
118 | |
---|
119 | findDatabase(); |
---|
120 | |
---|
121 | # get this script's ARGS |
---|
122 | # |
---|
123 | |
---|
124 | $num = $#ARGV + 1; |
---|
125 | |
---|
126 | # if user hasn't passed enough arguments, die and print the usage info |
---|
127 | |
---|
128 | if ($num le "2") { |
---|
129 | die "$usage"; |
---|
130 | } |
---|
131 | |
---|
132 | # |
---|
133 | # Get all the arguments |
---|
134 | # |
---|
135 | |
---|
136 | foreach (@ARGV){ |
---|
137 | if ($_ =~ m/debug/) { |
---|
138 | $debug = 1; |
---|
139 | } |
---|
140 | elsif ($_ =~ m/maxbitrate/) { |
---|
141 | $maxbitrate = (split(/\=/,$_))[1]; |
---|
142 | } |
---|
143 | elsif ($_ =~ m/starttime/) { |
---|
144 | $starttime = (split(/\=/,$_))[1]; |
---|
145 | } |
---|
146 | elsif ($_ =~ m/chanid/) { |
---|
147 | $chanid = (split(/\=/,$_))[1]; |
---|
148 | } |
---|
149 | elsif ($_ =~ m/exportdir/) { |
---|
150 | $exportdir = (split(/\=/,$_))[1]; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | # |
---|
155 | # connect to database |
---|
156 | # |
---|
157 | $connect = openDatabase(); |
---|
158 | |
---|
159 | # PREPARE THE QUERY |
---|
160 | $query = "SELECT title, subtitle FROM recorded WHERE chanid=$chanid AND starttime='$starttime'"; |
---|
161 | |
---|
162 | $query_handle = $connect->prepare($query); |
---|
163 | |
---|
164 | |
---|
165 | # EXECUTE THE QUERY |
---|
166 | $query_handle->execute() || die "Cannot connect to database \n"; |
---|
167 | |
---|
168 | # BIND TABLE COLUMNS TO VARIABLES |
---|
169 | $query_handle->bind_columns(undef, \$title, \$subtitle); |
---|
170 | |
---|
171 | # LOOP THROUGH RESULTS |
---|
172 | $query_handle->fetch(); |
---|
173 | |
---|
174 | # FIND WHERE THE RECORDINGS LIVE |
---|
175 | $query = "SELECT data FROM settings WHERE value='RecordFilePrefix' AND hostname='$hostname'"; |
---|
176 | |
---|
177 | $query_handle = $connect->prepare($query); |
---|
178 | $query_handle->execute() || die "Unable to query settings table"; |
---|
179 | |
---|
180 | my ($dir) = $query_handle->fetchrow_array; |
---|
181 | |
---|
182 | # FIND OUT THE CHANNEL NAME |
---|
183 | $query = "SELECT name FROM channel WHERE chanid=$chanid"; |
---|
184 | $query_handle = $connect->prepare($query); |
---|
185 | $query_handle->execute() || die "Unable to query settings table"; |
---|
186 | |
---|
187 | my ($channame) = $query_handle->fetchrow_array; |
---|
188 | |
---|
189 | # replace whitespace in channame with dashes |
---|
190 | $channame =~ s/\s+/-/g; |
---|
191 | |
---|
192 | # replace whitespace in title with underscores |
---|
193 | $title =~ s/\W+/_/g; |
---|
194 | # replace whitespace in subtitle with underscores |
---|
195 | $subtitle =~ s/\W+/_/g; |
---|
196 | |
---|
197 | # |
---|
198 | # Remove non alphanumeric chars from $starttime & $endtime |
---|
199 | # |
---|
200 | |
---|
201 | $newstarttime = $starttime; |
---|
202 | |
---|
203 | $newstarttime =~ s/[|^\W|\s|-|]//g; |
---|
204 | |
---|
205 | $year = substr($newstarttime, 0, 4); |
---|
206 | |
---|
207 | $filename = $dir."/".$chanid."_".$newstarttime.".mpg"; |
---|
208 | |
---|
209 | $newfilename = $exportdir."/".$channame."_".$title."_".$subtitle."_".$newstarttime.".mp3"; |
---|
210 | |
---|
211 | if ($debug) { |
---|
212 | print "\n\n Source filename:$filename \nDestination filename:$newfilename\n \n"; |
---|
213 | } |
---|
214 | # |
---|
215 | # Now run ffmpeg to find out what bitrate the stream is |
---|
216 | # |
---|
217 | |
---|
218 | $output = `ffmpeg -i $filename 2>&1`; |
---|
219 | |
---|
220 | @words = split(/Audio/, $output); |
---|
221 | @br = split(/\,/, $words[1]); |
---|
222 | @out = split(/kb/, $br[3]); |
---|
223 | |
---|
224 | $origbitrate = $out[0]; |
---|
225 | |
---|
226 | # |
---|
227 | # If maximum bitrate is less than the source bitrate, allow the max bitrate to remain |
---|
228 | # |
---|
229 | |
---|
230 | $bitrate = $maxbitrate; |
---|
231 | |
---|
232 | if ($maxbitrate > $origbitrate) { |
---|
233 | print "maxbitrate is greater than original. Using source bitrate to save space\n"; |
---|
234 | $bitrate = $origbitrate; |
---|
235 | } |
---|
236 | |
---|
237 | # |
---|
238 | # Now run ffmpeg to get mp3 out of the dvb radio recording |
---|
239 | # |
---|
240 | |
---|
241 | $command = $prefix."/nice -n19 ffmpeg -i $filename -ab $bitrate -acodec mp3 -f mp3 '$newfilename' 2>&1"; |
---|
242 | |
---|
243 | if ($debug) { |
---|
244 | print "\n\nUSING $command \n\n"; |
---|
245 | } |
---|
246 | |
---|
247 | system "$command"; |
---|
248 | |
---|
249 | # Now tag the finished MP3 file |
---|
250 | |
---|
251 | $command = "id3tag --song='$title' --album='$subtitle' --artist='$channame' --comment='Transcoded by MythTV' --year=$year |
---|
252 | '$newfilename'"; |
---|
253 | |
---|
254 | if ($debug) { |
---|
255 | print "\n\nUsing $command \n\n"; |
---|
256 | } |
---|
257 | system "$command"; |
---|