2
# This program is free software; you can redistribute it and/or modify
3
# it under the terms of the GNU General Public License as published by
4
# the Free Software Foundation; either version 2 of the License, or
5
# (at your option) any later version.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU Library General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
#Copyright 2001 Giuseppe Corbelli - cowo@lugbs.linux.it
18
# This package is inherited by Mediahandler and provides mp3 info access
19
# and decoding. See the attached UML diagram for access methods.
20
# Everything should be done using methods.
29
use vars qw($AUTOLOAD);
33
# 2: scalar containing filename
36
my ($class) = ref($proto)||$proto;
38
#Inherit from BaseInfo
39
my $self = $class->SUPER::new();
41
$self->Filename(shift);
42
if (! $self->Filename) {
43
$self->Error("No input file defined.");
46
if (! -r $self->Filename) {
47
$self->Error("Input file is not readable");
50
if (-z $self->Filename) {
51
$self->Error("Input file has 0 size");
54
#Identify available mp3 decoder
55
foreach my $dir (split (/\:/, $ENV{'PATH'})) {
56
if ( (-x "$dir/lame") && (-s "$dir/lame") ) {
57
$self->decoder_type('lame');
58
$self->decoder("$dir/lame");
61
if ( (-x "$dir/mpg321") && (-s "$dir/mpg321") ) {
62
$self->decoder_type('mpg321');
63
$self->decoder("$dir/mpg321");
66
if ( (-x "$dir/mpg123") && (-s "$dir/mpg123") ) {
67
$self->decoder_type('mpg123');
68
$self->decoder("$dir/mpg123");
72
if ( !$self->decoder ) {
73
$self->Error("Cannot find any of the supported mp3 decoders in \$PATH.");
77
my $ID3tagref = MP3::Info::get_mp3tag ($self->Filename);
78
my $MP3info = MP3::Info::get_mp3info ($self->Filename);
80
#First try the lowercase version, then the first char uppercase, last the all-uppercase
81
foreach my $n (qw(artist album title bitrate year comment genre mm ss secs frequency)) {
82
if ($ID3tagref->{$n}) {
83
$self->$n ($ID3tagref->{"$n"});
84
} elsif ($ID3tagref->{"\u$n"}) {
85
$self->$n ($ID3tagref->{"\u$n"});
86
} elsif ($ID3tagref->{"\U$n"}) {
87
$self->$n ($ID3tagref->{"\U$n"});
92
$MP3info->{"STEREO"} ? $self->channels(2) : $self->channels(1);
94
bless ($self, $class);
102
#Decodes the file in mp3handler instance to the file specified as Outputfile in same instance
103
#Use system() and external decoder to do the work. Return value is external tool's one.
106
my (@temp, $cmdline);
107
if (! -w dirname($self->Filename) ) {
108
$self->Error("Output directory is not writable");
111
printf ("\nDecoding file %s", $self->Filename) if ($self->debug);
112
if (!$self->Outfile) {
113
$_ = $self->Filename; s/\.mp3/\.wav/i;
115
print ("\n\tNo outputfile defined. Used ".$self->Outfile."\n") if ($self->debug);
117
printf (" to file %s\n", $self->Outfile) if ($self->debug);
119
if ( (-e $self->Outfile) && (-s $self->Outfile) && ($self->debug) ) {
120
print $self->Outfile." exists, skipping mp3 decode\n" if ($self->debug);
123
if ($self->decoder_type =~ /lame/i) {
124
$cmdline = $self->decoder." --decode --mp3input -S ".quotemeta ($self->Filename)." ".quotemeta ($self->Outfile);
125
} elsif ($self->decoder_type =~ /mpg321/i) {
126
$cmdline = $self->decoder." -q -w ".quotemeta ($self->Outfile)." ".quotemeta ($self->Filename);
128
$cmdline = $self->decoder." -q -s ".quotemeta ($self->Filename)." \> ".quotemeta ($self->Outfile);
130
system ("".$cmdline);
136
sub print_file_info {
138
printf ("\nFilename : %s, type %s\n", $self->Filename, $self->type);
139
printf ("\tArtist name: %s\t", $self->artist);
140
printf ("Album name: %s\t", $self->album);
141
printf ("Song title: %s\n", $self->title);
142
printf ("\tYear: %s\t", $self->year);
143
printf ("Song comment: %s\t", $self->comment);
144
printf ("Genre: %s\n", $self->genre);
145
printf ("\tDuration: %d min and %d sec\t", $self->durationMM, $self->durationSS);
146
printf ("Average Bitrate: %d kb/s\n", $self->Avgbr);
147
printf ("\t%d channel(s), %d HZ", $self->channels, ($self->frequency) * 1000);
152
# $trackno: Track number
153
# Optional: $filename: filename of .inf file
156
my ($trackno) = shift;
159
$inffilename = shift;
161
$inffilename = $self->Filename;
162
$inffilename =~ s/mp3/inf/i;
165
open ($inffilehandle, ">$inffilename");
166
if (!$inffilehandle) {last};
167
my (@tl) = localtime (time ());
168
$_ = sprintf ("#Created by mp32dao.pl on %02d/%02d/%d %02d:%02d:%02d\n",
169
$tl[3], $tl[4]+1, $tl[5]+1900, $tl[2], $tl[1], $tl[0]);
170
print $inffilehandle $_;
171
print $inffilehandle "#Source file is ".$self->Filename."\n";
172
print $inffilehandle "#Report bugs to Giuseppe \"Cowo\" Corbelli <cowo\@lugbs.linux.it>\n\n";
173
print $inffilehandle "Performer=\t'".$self->Artist."'\n";
174
print $inffilehandle "Tracktitle=\t'".$self->Title."'\n";
175
print $inffilehandle "Albumtitle=\t'".$self->Album."'\n";
176
print $inffilehandle "Tracknumber=\t".($trackno)."\n";
177
close ($inffilehandle);