3 ############################################################################
4 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
6 ############################################################################
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # A copy of the GNU General Public License is available on the World Wide Web
18 # at `http://www.gnu.org/copyleft/gpl.html'. You can also obtain it by
19 # writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 # Boston, MA 02111-1307, USA.
21 ############################################################################
29 use Cwd qw
'cwd abs_path';
31 # find program directory
34 my $l = readlink or die "readlink $_: $!\n";
35 if($l =~ m
|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
38 $progdir = abs_path
($1);
40 unshift @INC, "$progdir/lib";
46 require "$progdir/soepkiptng.lib";
48 $ENV{PATH
} = "$progdir/bin:$ENV{PATH}";
49 use lib
"$progdir/lib";
55 Usage: flac2mp3 [-d] [-p flac_binary] [-b bitrate] [-c configfile] file...
57 -d : delete original after successful conversion
58 -b bitrate : kbitrate to encode to (default 192)
59 -c configfile : override soepkiptng config file
63 read_configfile
(\
%conf, $opt_c);
65 $bitrate = $opt_b || 192;
66 $flac = $opt_p || "flac";
71 $dbh = DBI
->connect("DBI:$conf{'db_type'}:$conf{'db_name'}:$conf{'db_host'}",
72 $conf{'db_user'}, $conf{'db_pass'})
73 or die "can't connect to database";
75 foreach $flacfile (@ARGV) {
78 $flacfile =~ s
~^(.*/)?(.*)~abs_path($1 || ".") . "/$2"~e;
80 $q = "SELECT title
,artist
.name
,album
.name
,track
" .
81 " FROM song
,artist
,album
" .
82 " WHERE song
.artist_id
=artist
.id AND song
.album_id
=album
.id
" .
83 " AND present AND filename
=?
";
84 $sth = $dbh->prepare($q);
85 $sth->execute($flacfile)
86 or die "can
't do sql command: " . $dbh->errstr;
88 my ($ti, $ar, $al, $tr) = $sth->fetchrow_array or do {
89 warn "$flacfile: not found in dbase\n";
92 my $mp3file = $flacfile;
93 $mp3file =~ s/\.\w+$//;
97 warn "$mp3file exists, skipping.\n";
101 my @lamecmd = (qw/lame -h -r -x -s 44.1 -m j --add-id3v2 --id3v2-only --pad-id3v2 --abr/, $bitrate);
102 if($ti) { push @lamecmd, "--tt", $ti; }
103 if($ar) { push @lamecmd, "--ta", $ar; }
104 if($al) { push @lamecmd, "--tl", $al; }
105 if($tr) { push @lamecmd, "--tn", $tr; }
107 if(open(STDIN, "-|") == 0) {
108 exec $flac, qw/-sdc --endian=little --sign=signed --force-raw-format/,
112 system @lamecmd, '-', "$mp3file.tmp";
115 warn "$flacfile: oggenc failed.\n";
117 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
118 if(($? & 0x7f) == 2) { die "aborted.\n"; }
123 warn "$flacfile: $flac -d failed.\n";
124 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
125 if(($? & 0x7f) == 2) { die "aborted.\n"; }
130 warn "$mp3file exists, skipping.\n";
131 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
134 rename "$mp3file.tmp", $mp3file or do {
135 warn "rename $mp3file.tmp -> $mp3file: $!\n";
136 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
140 unlink $flacfile or warn "unlink $flacfile: $!\n";