use -V0 --vbr-new by default; add -O option
[soepkiptng.git] / flac2mp3
blobd445a1b18656fc66316384f1dfc34730d6a71f2b
1 #!/usr/bin/perl
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 ############################################################################
22 # CONFIG
24 my $progdir;
25 BEGIN {
26 use Cwd qw'cwd abs_path';
28 # find program directory
29 $_ = $0;
30 while(-l) {
31 my $l = readlink or die "readlink $_: $!\n";
32 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
34 m|(.*)/|;
35 $progdir = abs_path($1);
37 unshift @INC, "$progdir/lib";
39 use Getopt::Std;
40 use DBI;
42 require "$progdir/soepkiptng.lib";
44 $ENV{PATH} = "$progdir/bin:$ENV{PATH}";
45 use lib "$progdir/lib";
47 getopts('dc:p:b:ho:O:');
49 $opt_h and die <<EOF;
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
58 EOF
60 read_configfile(\%conf, $opt_c);
62 $bitrate = $opt_b || "v0";
63 $flac = $opt_p || "flac";
64 $cwd = cwd;
66 $| = 1;
68 $dbh = connect_to_db(\%conf);
70 foreach $flacfile (@ARGV) {
71 next if -d $flacfile;
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+$//;
89 $mp3file .= ".mp3";
90 $opt_o and $mp3file =~ s|.*/|$opt_o/|;
91 $mp3file = $opt_O if defined $opt_O;
93 if(-e $mp3file) {
94 warn "$mp3file exists, skipping.\n";
95 next;
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";
105 } else {
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/,
115 $flacfile;
116 die "$flac: $!\n";
119 system @lamecmd, '-', "$mp3file.tmp";
121 if($?) {
122 warn "$flacfile: $flac failed.\n";
123 close STDIN;
124 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
125 if(($? & 0x7f) == 2) { die "aborted.\n"; }
126 next;
128 close STDIN;
129 if($?) {
130 warn "$flacfile: $flac -d failed.\n";
131 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
132 if(($? & 0x7f) == 2) { die "aborted.\n"; }
133 next;
136 if(-e $mp3file) {
137 warn "$mp3file exists, skipping.\n";
138 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
139 next;
141 rename "$mp3file.tmp", $mp3file or do {
142 warn "rename $mp3file.tmp -> $mp3file: $!\n";
143 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
144 next;
146 if($opt_d) {
147 unlink $flacfile or warn "unlink $flacfile: $!\n";