Merge branch 'topic/sweetums' (early part)
[soepkiptng.git] / flac2ogg
blob64b4f2603d8ffbeffc4c458ffbae550c90285946
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 $bitrate = 192;
26 my $progdir;
27 BEGIN {
28 use Cwd qw'cwd abs_path';
30 # find program directory
31 $_ = $0;
32 while(-l) {
33 my $l = readlink or die "readlink $_: $!\n";
34 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
36 m|(.*)/|;
37 $progdir = abs_path($1);
39 unshift @INC, "$progdir/lib";
41 use Getopt::Std;
42 use DBI;
43 use MP3::Tag;
45 require "$progdir/soepkiptng.lib";
47 $ENV{PATH} = "$progdir/bin:$ENV{PATH}";
48 use lib "$progdir/lib";
50 getopts('dc:p:b:h');
52 $opt_h and die <<EOF;
54 Usage: flac2ogg [-d] [-p flac_binary] [-b bitrate] [-c configfile] file...
56 -d : delete original after successful conversion
57 -b bitrate : kbitrate to encode to (default 192)
58 -c configfile : override soepkiptng config file
60 EOF
62 read_configfile(\%conf, $opt_c);
64 $bitrate = $opt_b || 192;
65 $flac = $opt_p || "flac";
66 $cwd = cwd;
68 $| = 1;
70 $dbh = DBI->connect("DBI:$conf{'db_type'}:$conf{'db_name'}:$conf{'db_host'}",
71 $conf{'db_user'}, $conf{'db_pass'})
72 or die "can't connect to database";
74 foreach $flacfile (@ARGV) {
75 next if -d $flacfile;
77 if($flacfile !~ m|^/|) { $flacfile = "$cwd/$flacfile"; }
78 $flacfile =~ s!(^|/)(\./)+!\1!g;
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 $oggfile = $flacfile;
93 $oggfile =~ s|.*/||;
94 $oggfile =~ s/\.\w+//;
95 $oggfile .= ".ogg";
97 if(-e $oggfile) {
98 warn "$oggfile exists, skipping.\n";
99 next;
102 my @oggcmd = ('oggenc', '-r', '-b', $bitrate, '-o', "$oggfile.tmp");
103 if($ti) { push @oggcmd, "-t", $ti; }
104 if($ar) { push @oggcmd, "-a", $ar; }
105 if($al) { push @oggcmd, "-l", $al; }
106 if($tr) { push @oggcmd, "-N", $tr; }
108 if(open(STDIN, "-|") == 0) {
109 exec $flac, qw/-sdc --endian=little --sign=signed --force-raw-format/,
110 $flacfile;
113 system @oggcmd, '-';
115 if($?) {
116 warn "$flacfile: oggenc failed.\n";
117 close STDIN;
118 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
119 if(($? & 0x7f) == 2) { die "aborted.\n"; }
120 next;
122 close STDIN;
123 if($?) {
124 warn "$flacfile: $flac -d failed.\n";
125 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
126 if(($? & 0x7f) == 2) { die "aborted.\n"; }
127 next;
130 if(-e $oggfile) {
131 warn "$oggfile exists, skipping.\n";
132 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
133 next;
135 rename "$oggfile.tmp", $oggfile or do {
136 warn "rename $oggfile.tmp -> $oggfile: $!\n";
137 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
138 next;
140 if($opt_d) {
141 unlink $flacfile or warn "unlink $flacfile: $!\n";