script to copy all songs from a playlist to an mp3 player
[soepkiptng.git] / flac2ogg
blob884a1281e8ad9794affa080f62335e428c249a1f
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;
44 require "$progdir/soepkiptng.lib";
46 $ENV{PATH} = "$progdir/bin:$ENV{PATH}";
47 use lib "$progdir/lib";
49 getopts('dc:p:b:h');
51 $opt_h and die <<EOF;
53 Usage: flac2ogg [-d] [-p flac_binary] [-b bitrate] [-c configfile] file...
55 -d : delete original after successful conversion
56 -b bitrate : kbitrate to encode to (default 192)
57 -c configfile : override soepkiptng config file
59 EOF
61 read_configfile(\%conf, $opt_c);
63 $bitrate = $opt_b || 192;
64 $flac = $opt_p || "flac";
65 $cwd = cwd;
67 $| = 1;
69 $dbh = DBI->connect("DBI:$conf{'db_type'}:$conf{'db_name'}:$conf{'db_host'}",
70 $conf{'db_user'}, $conf{'db_pass'})
71 or die "can't connect to database";
73 foreach $flacfile (@ARGV) {
74 next if -d $flacfile;
76 if($flacfile !~ m|^/|) { $flacfile = "$cwd/$flacfile"; }
77 $flacfile =~ s!(^|/)(\./)+!\1!g;
79 $q = "SELECT title,artist.name,album.name,track" .
80 " FROM song,artist,album" .
81 " WHERE song.artist_id=artist.id AND song.album_id=album.id" .
82 " AND present AND filename=?";
83 $sth = $dbh->prepare($q);
84 $sth->execute($flacfile)
85 or die "can't do sql command: " . $dbh->errstr;
87 my ($ti, $ar, $al, $tr) = $sth->fetchrow_array or do {
88 warn "$flacfile: not found in dbase\n";
91 my $oggfile = $flacfile;
92 $oggfile =~ s|.*/||;
93 $oggfile =~ s/\.\w+//;
94 $oggfile .= ".ogg";
96 if(-e $oggfile) {
97 warn "$oggfile exists, skipping.\n";
98 next;
101 my @oggcmd = ('oggenc', '-r', '-b', $bitrate, '-o', "$oggfile.tmp");
102 if($ti) { push @oggcmd, "-t", $ti; }
103 if($ar) { push @oggcmd, "-a", $ar; }
104 if($al) { push @oggcmd, "-l", $al; }
105 if($tr) { push @oggcmd, "-N", $tr; }
107 if(open(STDIN, "-|") == 0) {
108 exec $flac, qw/-sdc --endian=little --sign=signed --force-raw-format/,
109 $flacfile;
112 system @oggcmd, '-';
114 if($?) {
115 warn "$flacfile: oggenc failed.\n";
116 close STDIN;
117 unlink "$oggfile.tmp" or warn "delete $oggfile.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 "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
125 if(($? & 0x7f) == 2) { die "aborted.\n"; }
126 next;
129 if(-e $oggfile) {
130 warn "$oggfile exists, skipping.\n";
131 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
132 next;
134 rename "$oggfile.tmp", $oggfile or do {
135 warn "rename $oggfile.tmp -> $oggfile: $!\n";
136 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
137 next;
139 if($opt_d) {
140 unlink $flacfile or warn "unlink $flacfile: $!\n";