blib/lib/Audio/ConvTools.pm

filter: Statementstmt 19.3% 19 / 98 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 29.4% 5 / 17 Podpod 100% 12 / 12 totaltotal 23.8% 36 / 151 CC 13 SCAR 20.8
CC
13
Cov
16%
mp32ogg
12.0
ogg2mp3
12.0
mp32wav
12.0
CRAP 8.0
↑ Summary
1 ################################################################################
2 # Convert audiofiles from one format to another (ogg, mp3 or wav) #
3 # #
4 # Copyright (C) 2006 Michael Hooreman <michael_AT_mijoweb_DOT_net> #
5 ################################################################################
6  
7 #$Id: ConvTools.pm,v 1.3 2006-10-28 10:24:23 michael Exp $
8  
9 =head1 NAME
10  
11 Audio::ConvTools - API to convert audio files from/to mp3 ogg and wav
12  
13 =head1 SYNOPSIS
14  
15 use Audio::ConvTools;
16 use Audio::ConvTools qw/:DEFAULT :Tmp :Log/;
17  
18 $status = ogg2mp3('file.ogg');
19 $status = ogg2wav('file.ogg');
20 $status = ogg2wav('in.ogg', 'out.wav');
21 $status = mp32ogg('file.mp3');
22 $status = mp32wav('file.mp3');
23 $status = mp32wav('in.mp3', 'out.wav');
24 $status = wav2ogg('file.wav');
25 $status = wav2ogg('in.wav', 'out.ogg');
26 $status = wav2mp3('file.wav');
27 $status = wav2mp3('in.wav', 'out.mp3');
28  
29 Audio::ConvTools::logMsg('This is a log message');
30 Audio::ConvTools::errMsg('This is an error message');
31  
32 $tmp = Audio::ConvTools::getTmpFile('.wav');
33 Audio::ConvTools::destroyTmpFile(\$tmp);
34  
35 =head2 OBJECT INTERFACE
36  
37 No object interface is defined
38  
39 =head1 DESCRIPTION
40  
41 C<Audio::ConvTools> provides miscellaneous tools to convert audio files between
42 Ogg Vorbis, MPEG III and Wav files. This is a function interface only.
43  
44 By default, all the conversions functions are exported. You can also export
45 temporary file usage tools with the :Tmp tag, and logging tools with the :Log
46 tag.
47  
48 Moreover, two scipts are provided with this package:
49  
50 =over
51  
52 =item *
53  
54 audiocdmaker To brun audio CD
55  
56 =item *
57  
58 audioconv To convert audio files
59  
60 =back
61  
62 =head2 PROGRAMS USED
63  
64 To do the conversions, this module uses the following linux programs:
65  
66 =over
67  
68 =item *
69  
70 oggdec, tested with version 1.0.1
71  
72 =item *
73  
74 mpg321, tested with version 0.2.10
75  
76 =item *
77  
78 oggenc, tested with version 1.0.2
79  
80 =item *
81  
82 lame, tested with version 3.96.1
83  
84 =back
85  
86 =head1
87  
88 =head1 FUNCTIONS
89  
90 =head2 EXPORTED BY DEFAULT
91  
92 =head3 ogg2mp3
93  
94 This makes a conversion from ogg to mp3:
95  
96 $status = ogg2mp3('file.ogg');
97  
98 This takes the ogg file name as argument, and returns the status of the
99 conversion.
100  
101 It first converts the ogg file to wav using ogg2wav, and then converts the wav
102 to ogg.
103  
104 The input file has to end with '.ogg' (case insensitive). The generated file
105 will have the same name, excepts that '.ogg' will be replaced by 'mp3'.
106  
107 =head3 ogg2wav
108  
109 This makes a conversion from ogg to wav:
110  
111 $status = ogg2wav('file.ogg');
112 $status = ogg2wav('in.ogg', 'out.wav');
113  
114 This takes the ogg file name as first argument, and returns the status of the
115 conversion. If a second argument is provided, this is the name of the resulting
116 wav file.
117  
118 The input file has to end with '.ogg' (case insensitive). If the second
119 argument is not provided, the generated file will have the same name, excepts
120 that '.ogg' will be replaced by 'wav'.
121  
122 =head3 mp32ogg
123  
124 This makes a conversion from ogg to mp3:
125  
126 $status = mp32ogg('file.mp3');
127  
128 This takes the mp3 file name as argument, and returns the status of the
129 conversion.
130  
131 It first converts the mp3 file to wav using mp32wav, and then converts the wav
132 to ogg.
133  
134 The input file has to end with '.mp3' (case insensitive). The generated file
135 will have the same name, excepts that '.mp3' will be replaced by 'ogg'.
136  
137 =head3 mp32wav
138  
139 This makes a conversion from mp3 to wav:
140  
141 $status = mp32wav('file.mp3');
142 $status = mp32wav('in.mp3', 'out.wav');
143  
144 This takes the mp3 file name as first argument, and returns the status of the
145 conversion. If a second argument is provided, this is the name of the resulting
146 wav file.
147  
148 The input file has to end with '.mp3' (case insensitive). If the second
149 argument is not provided, the generated file will have the same name, excepts
150 that '.mp3' will be replaced by 'wav'.
151  
152 =head3 wav2ogg
153  
154 This makes a conversion from wav to ogg:
155  
156 $status = wav2ogg('file.wav');
157 $status = wav2ogg('in.wav', 'out.ogg');
158  
159 This takes the wav file name as first argument, and returns the status of the
160 conversion. If a second argument is provided, this is the name of the resulting
161 ogg file.
162  
163 The input file has to end with '.wav' (case insensitive). If the second
164 argument is not provided, the generated file will have the same name, excepts
165 that '.wav' will be replaced by 'ogg'.
166  
167 =head3 wav2mp3
168  
169 This makes a conversion from wav to mp3:
170  
171 $status = wav2mp3('file.wav');
172 $status = wav2mp3('in.wav', 'out.mp3');
173  
174 This takes the wav file name as first argument, and returns the status of the
175 conversion. If a second argument is provided, this is the name of the resulting
176 mp3 file.
177  
178 The input file has to end with '.wav' (case insensitive). If the second
179 argument is not provided, the generated file will have the same name, excepts
180 that '.wav' will be replaced by 'mp3'.
181  
182 =head2 EXPORTED BY :Log
183  
184 =head3 logMsg
185  
186 This prints a contatenation of getNowStr, "INFO:" and the message to STDERR:
187  
188 logMsg('This is a log message');
189  
190 =head3 errMsg
191  
192 This prints a contatenation of getNowStr, "ERROR:" and the message to STDERR:
193  
194 logMsg('This is an error message');
195  
196 =head2 EXPORTED BY :Tmp
197  
198 =head3 getTmpFile
199  
200 This returns a new File::Temp object with file name extension provided as
201 argument:
202  
203 my $tmp = getTmpFile('.wav');
204  
205 =head3 destroyTmpFile
206  
207 This destroys a temp file given, as reference, as argument:
208  
209 destroyTmpFile(\$tmp)
210  
211 The deferenced argument will be undef at the end of this function.
212  
213 =head2 NEVER EXPORTED
214  
215 =head3 getVersion
216  
217 This returns the version of the module:
218  
219 print "Module Version = " . getVersion();
220  
221 This is used by binary script (the version of the scripts is the version of the
222 module).
223  
224 =head3 getNowTxt
225  
226 This returns the actual date and time in format: Day YYYY-MM-DD hh:mm:ss:
227  
228 print "We are " . getNowStr();
229  
230 This is used by logMsg and errMsg.
231  
232 =head1 SEE ALSO
233  
234 L<File::Temp>, L<String::ShellQuote>
235  
236 =head1 AUTHOR
237  
238 Michael Hooreman C<< <mhooreman at skynet.be> >>
239  
240 =head1 BUGS
241  
242 Please report any bugs or feature requests to
243 C<bug-audioconv-tools at rt.cpan.org>, or through the web interface at
244 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Audio-ConvTools>.
245 I will be notified, and then you'll automatically be notified of progress on
246 your bug as I make changes.
247  
248 =head1 SUPPORT
249  
250 You can find documentation for this module with the perldoc command.
251  
252 perldoc Audio::ConvTools
253  
254 =over 4
255  
256 =item * AnnoCPAN: Annotated CPAN documentation
257  
258 L<http://annocpan.org/dist/Audio-ConvTools>
259  
260 =item * CPAN Ratings
261  
262 L<http://cpanratings.perl.org/d/Audio-ConvTools>
263  
264 =item * RT: CPAN's request tracker
265  
266 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Audio-ConvTools>
267  
268 =item * Search CPAN
269  
270 L<http://search.cpan.org/dist/Audio-ConvTools>
271  
272 =back
273  
274 =head1 COPYRIGHT & LICENSE
275  
276 Copyright 2006 Michael Hooreman, all rights reserved.
277  
278 This program is free software; you can redistribute it and/or modify it
279 under the same terms as Perl itself.
280  
281 =cut
282  
283 package Audio::ConvTools;
284  
285 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
286 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
287  
288 2 require Exporter;
289  
290 2 our @ISA = qw/Exporter/;
291 2 our $VERSION = "0.08";
292 2 our @EXPORT = qw/
293 mp32ogg
294 mp32wav
295 ogg2mp3
296 ogg2wav
297 wav2ogg
298 wav2mp3
299 /;
300 2 our %EXPORT_TAGS = (
301 Log => [qw/
302 logMsg
303 errMsg
304 /],
305 Tmp => [qw/
306 getTmpFile
307 destroyTmpFile
308 /],
309 );
310 2 our @EXPORT_OK = qw/
311 logMsg
312 errMsg
313 getTmpFile
314 destroyTmpFile
315 /;
316  
317 2 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
318 2 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
319  
320 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
321 #$Exporter::Verbose = 1
322 };
323  
324 sub getVersion()
325 0 ▶{
Subroutine getVersion: not called CC=1 SCAR=6.9
Pod documented
326 0 return $VERSION;
327 }
328  
329 sub getNowTxt()
330 0 ▶{
Subroutine getNowTxt: not called CC=1 SCAR=6.9
Pod documented
331 0 my ($s, $m, $h, $D, $M, $Y) = localtime(time);
332 0 return sprintf(
333 "%04d-%02d-%02d %02d:%02d:%02d",
334 $Y+1900, $M+1, $D, $h, $m, $s
335 );
336 }
337  
338 sub logMsg($)
339 0 ▶{
Subroutine logMsg: not called CC=1 SCAR=6.9
Pod documented
340 0 my $txt = shift;
341 0 print STDERR getNowTxt() . ": INFO: " . $txt . $/;
342 }
343  
344 sub errMsg($)
345 0 ▶{
Subroutine errMsg: not called CC=1 SCAR=6.9
Pod documented
346 0 my $txt = shift;
347 0 print STDERR getNowTxt() . ": ERROR: " . $txt . $/;
348 }
349  
350 sub getTmpFile($)
351 0 ▶{
Subroutine getTmpFile: not called CC=1 SCAR=6.9
Pod documented
352 0 my $extension = shift;
353 0 my $tmp = new File::Temp(
354 SUFFIX=>$extension,
355 UNLINK=>1 , #to automatically remove when out of scope
356 );
357 0 return $tmp;
358 }
359  
360 sub destroyTmpFile($)
361 0 ▶{
Subroutine destroyTmpFile: not called CC=1 SCAR=6.9
Pod documented
362 0 my $pTmp = shift;
363 0 $$pTmp->cleanup(); #to be sure
364 0 $$pTmp = undef; #old tmp object is out of scope => automatically cleaned
365 }
366  
367 sub mp32ogg($)
368 0 ▶{
Subroutine mp32ogg: not called CC=3 SCAR=24.8
Pod documented
369 0 my $inFile = shift;
370 0 my $outFile;
371 my $tmpFile;
372 0 my $status;
373  
374 0 ▶ ($inFile =~ /^(.*)\.[Mm][Pp]3$/) or do {
Branch: 1 branch
BranchTrueFalseTotal
$inFile =~ /^(.*)\.[Mm][Pp]3$/ or do { &errMsg("$inFile is not a mp3 file"); return 0 } 0 0 0
375 0 errMsg("$inFile is not a mp3 file");
376 0 return 0;
377 };
378 0 $outFile = "$1.ogg";
379 0 $tmpFile = getTmpFile('.wav');
380  
381 0 $status = mp32wav($inFile, $tmpFile);
382 0 ▶ unless ($status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 0 0
383 0 errMsg("Cannot create temp wav file");
384 0 return $status;
385 }
386  
387 0 $status = wav2ogg($tmpFile, $outFile);
388 0 destroyTmpFile(\$tmpFile);
389  
390 0 return $status;
391 }
392  
393 sub ogg2mp3($)
394 0 ▶{
Subroutine ogg2mp3: not called CC=3 SCAR=24.8
Pod documented
395 0 my $inFile = shift;
396 0 my $outFile;
397 my $tmpFile;
398 0 my $status;
399  
400 0 ▶ ($inFile =~ /^(.*)\.[Oo][Gg][Gg]$/) or do {
Branch: 1 branch
BranchTrueFalseTotal
$inFile =~ /^(.*)\.[Oo][Gg][Gg]$/ or do { &errMsg("$inFile is not a ogg file"); return 0 } 0 0 0
401 0 errMsg("$inFile is not a ogg file");
402 0 return 0;
403 };
404 0 $outFile = "$1.mp3";
405 0 $tmpFile = getTmpFile('.wav');
406  
407 0 $status = ogg2wav($inFile, $tmpFile);
408 0 ▶ unless ($status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 0 0
409 0 errMsg("Cannot create temp wav file");
410 0 return $status;
411 }
412  
413 0 $status = wav2mp3($tmpFile, $outFile);
414 0 destroyTmpFile(\$tmpFile);
415  
416 0 return $status;
417 }
418  
419 sub mp32wav($;$)
420 0 ▶{
Subroutine mp32wav: not called CC=3 SCAR=24.8
Pod documented
421 0 my $inFile = shift;
422 0 my $outFile = shift;
423 0 my $status;
424 0 ▶ ($inFile =~ /^(.*)\.[Mm][Pp]3$/) or do {
Branch: 1 branch
BranchTrueFalseTotal
$inFile =~ /^(.*)\.[Mm][Pp]3$/ or do { &errMsg("$inFile is not an mp3 file"); return 0 } 0 0 0
425 0 errMsg("$inFile is not an mp3 file");
426 0 return 0;
427 };
428 0 ▶ $outFile = "$1.wav" unless defined $outFile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outFile 0 0 0
429 0 $status = system(
430 "mpg321 -w " . shell_quote($outFile) . " " . shell_quote($inFile)
431 );
432 0 return ($status==0);
433 }
434  
435 sub ogg2wav($;$)
436 0 ▶{
Subroutine ogg2wav: not called CC=3 SCAR=24.8
Pod documented
437 0 my $inFile = shift;
438 0 my $outFile = shift;
439 0 my $status;
440 0 ▶ ($inFile =~ /^(.*)\.[Oo][Gg][Gg]$/) or do {
Branch: 1 branch
BranchTrueFalseTotal
$inFile =~ /^(.*)\.[Oo][Gg][Gg]$/ or do { &errMsg("$inFile is not an ogg vorbis file"); return 0 } 0 0 0
441 0 errMsg("$inFile is not an ogg vorbis file");
442 0 return 0;
443 };
444 0 ▶ $outFile = "$1.wav" unless defined $outFile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outFile 0 0 0
445 0 $status = system(
446 "oggdec " . shell_quote($inFile) . " -o " . shell_quote($outFile)
447 );
448 0 return ($status==0);
449 }
450  
451 sub wav2ogg($;$)
452 0 ▶{
Subroutine wav2ogg: not called CC=3 SCAR=24.8
Pod documented
453 0 my $inFile = shift;
454 0 my $outFile = shift;
455 0 my $status;
456 0 ▶ ($inFile =~ /^(.*)\.[Ww][Aa][Vv]$/) or do {
Branch: 1 branch
BranchTrueFalseTotal
$inFile =~ /^(.*)\.[Ww][Aa][Vv]$/ or do { &errMsg("$inFile is not an wav file"); return 0 } 0 0 0
457 0 errMsg("$inFile is not an wav file");
458 0 return 0;
459 };
460 0 ▶ $outFile = "$1.ogg" unless defined $outFile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outFile 0 0 0
461 0 $status = system(
462 "oggenc -q 10 -o " . shell_quote($outFile) . " " . shell_quote($inFile)
463 );
464 0 return ($status==0);
465 }
466  
467 sub wav2mp3($;$)
468 0 ▶{
Subroutine wav2mp3: not called CC=3 SCAR=24.8
Pod documented
469 0 my $inFile = shift;
470 0 my $outFile = shift;
471 0 my $status;
472 0 ▶ ($inFile =~ /^(.*)\.[Ww][Aa][Vv]$/) or do {
Branch: 1 branch
BranchTrueFalseTotal
$inFile =~ /^(.*)\.[Ww][Aa][Vv]$/ or do { &errMsg("$inFile is not an wav file"); return 0 } 0 0 0
473 0 errMsg("$inFile is not an wav file");
474 0 return 0;
475 };
476 0 ▶ $outFile = "$1.mp3" unless defined $outFile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outFile 0 0 0
477 0 $status = system(
478 "lame -h " . shell_quote($inFile) . " " . shell_quote($outFile)
479 );
480 0 return ($status==0);
481 }
482  
483 2 1;
484  
485 __END__
↑ Summary