3 ############################################################################
4 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
5 ############################################################################
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # A copy of the GNU General Public License is available on the World Wide Web
17 # at `http://www.gnu.org/copyleft/gpl.html'. You can also obtain it by
18 # writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA.
20 ############################################################################
26 use Cwd qw
'cwd abs_path';
28 # find program directory
31 my $l = readlink or die "readlink $_: $!\n";
32 if($l =~ m
|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
35 $progdir = abs_path
($1);
37 unshift @INC, "$progdir/lib";
42 require "$progdir/soepkiptng.lib";
44 $ENV{PATH
} = "$progdir/bin:$ENV{PATH}";
45 use lib
"$progdir/lib";
47 getopts
('dc:p:b:ho:O:');
51 Usage: flac2mp3 [-d] [-p flac_binary] [-b bitrate] [-c configfile] file...
53 -d : delete original after successful conversion
54 -b bitrate : kbitrate to encode to (default v0)
55 -c configfile : override soepkiptng config file
56 -O outputfile : specify output file
60 read_configfile
(\
%conf, $opt_c);
62 $bitrate = $opt_b || "v0";
63 $flac = $opt_p || "flac";
68 $dbh = connect_to_db
(\
%conf);
70 foreach $flacfile (@ARGV) {
73 $flacfile =~ s
~^(.*/)?(.*)~abs_path($1 || ".") . "/$2"~e;
75 $q = "SELECT title
,artist
.name
,album
.name
,track
" .
76 " FROM song
,artist
,album
" .
77 " WHERE song
.artist_id
=artist
.id AND song
.album_id
=album
.id
" .
78 " AND present AND filename
=?
";
79 $sth = $dbh->prepare($q);
80 $sth->execute($flacfile)
81 or die "can
't do sql command: " . $dbh->errstr;
83 my ($ti, $ar, $al, $tr) = $sth->fetchrow_array or do {
84 warn "$flacfile: not found in dbase\n";
87 my $mp3file = $flacfile;
88 $mp3file =~ s/\.\w+$//;
90 $opt_o and $mp3file =~ s|.*/|$opt_o/|;
91 $mp3file = $opt_O if defined $opt_O;
94 warn "$mp3file exists, skipping.\n";
98 my @lamecmd = qw/lame -r -s 44.1 --add-id3v2 --pad-id3v2/;
99 push @lamecmd, "--id3v2-only";
100 #push @lamecmd, "--ty", 1991;
101 if($bitrate =~ /^(\d+)/) {
102 push @lamecmd, "--abr", $bitrate;
103 } elsif($bitrate =~ /^(v\d+)/) {
104 push @lamecmd, uc("-$1"), "--vbr-new";
106 die "invalid bitrate: $bitrate\n";
108 if($ti) { push @lamecmd, "--tt", $ti; }
109 if($ar) { push @lamecmd, "--ta", $ar; }
110 if($al) { push @lamecmd, "--tl", $al; }
111 if($tr) { push @lamecmd, "--tn", $tr; }
113 if(open(STDIN, "-|") == 0) {
114 exec $flac, qw/-sdc --endian=little --sign=signed --force-raw-format/,
119 system @lamecmd, '-', "$mp3file.tmp";
122 warn "$flacfile: $flac failed.\n";
124 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
125 if(($? & 0x7f) == 2) { die "aborted.\n"; }
130 warn "$flacfile: $flac -d failed.\n";
131 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
132 if(($? & 0x7f) == 2) { die "aborted.\n"; }
137 warn "$mp3file exists, skipping.\n";
138 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
141 rename "$mp3file.tmp", $mp3file or do {
142 warn "rename $mp3file.tmp -> $mp3file: $!\n";
143 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
147 unlink $flacfile or warn "unlink $flacfile: $!\n";