show only artists that have songs
[soepkiptng.git] / flac2mp3
blobe15a65a2c108f4fe88357b886d484d42e4c57a3b
1 #!/usr/bin/perl
3 ############################################################################
4 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
5 # $Id$
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 ############################################################################
23 # CONFIG
25 $bitrate = 192;
27 my $progdir;
28 BEGIN {
29 use Cwd qw'cwd abs_path';
31 # find program directory
32 $_ = $0;
33 while(-l) {
34 my $l = readlink or die "readlink $_: $!\n";
35 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
37 m|(.*)/|;
38 $progdir = abs_path($1);
40 unshift @INC, "$progdir/lib";
42 use Getopt::Std;
43 use DBI;
44 use MP3::Tag;
46 require "$progdir/soepkiptng.lib";
48 $ENV{PATH} = "$progdir/bin:$ENV{PATH}";
49 use lib "$progdir/lib";
51 getopts('dc:p:b:h');
53 $opt_h and die <<EOF;
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
61 EOF
63 read_configfile(\%conf, $opt_c);
65 $bitrate = $opt_b || 192;
66 $flac = $opt_p || "flac";
67 $cwd = cwd;
69 $| = 1;
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) {
76 next if -d $flacfile;
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+$//;
94 $mp3file .= ".mp3";
96 if(-e $mp3file) {
97 warn "$mp3file exists, skipping.\n";
98 next;
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/,
109 $flacfile;
112 system @lamecmd, '-', "$mp3file.tmp";
114 if($?) {
115 warn "$flacfile: oggenc failed.\n";
116 close STDIN;
117 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
118 if(($? & 0x7f) == 2) { die "aborted.\n"; }
119 next;
121 close STDIN;
122 if($?) {
123 warn "$flacfile: $flac -d failed.\n";
124 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
125 if(($? & 0x7f) == 2) { die "aborted.\n"; }
126 next;
129 if(-e $mp3file) {
130 warn "$mp3file exists, skipping.\n";
131 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
132 next;
134 rename "$mp3file.tmp", $mp3file or do {
135 warn "rename $mp3file.tmp -> $mp3file: $!\n";
136 unlink "$mp3file.tmp" or warn "delete $mp3file.tmp: $!\n";
137 next;
139 if($opt_d) {
140 unlink $flacfile or warn "unlink $flacfile: $!\n";