handle EINTR return from poll()
[soepkiptng.git] / flac2ogg
blobfafc2e2b5ee7787d7610a9a5caa64a55c2238b5b
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 = connect_to_db(\%conf);
71 foreach $flacfile (@ARGV) {
72 next if -d $flacfile;
74 if($flacfile !~ m|^/|) { $flacfile = "$cwd/$flacfile"; }
75 $flacfile =~ s!(^|/)(\./)+!\1!g;
77 $q = "SELECT title,artist.name,album.name,track" .
78 " FROM song,artist,album" .
79 " WHERE song.artist_id=artist.id AND song.album_id=album.id" .
80 " AND present AND filename=?";
81 $sth = $dbh->prepare($q);
82 $sth->execute($flacfile)
83 or die "can't do sql command: " . $dbh->errstr;
85 my ($ti, $ar, $al, $tr) = $sth->fetchrow_array or do {
86 warn "$flacfile: not found in dbase\n";
89 my $oggfile = $flacfile;
90 $oggfile =~ s|.*/||;
91 $oggfile =~ s/\.\w+//;
92 $oggfile .= ".ogg";
94 if(-e $oggfile) {
95 warn "$oggfile exists, skipping.\n";
96 next;
99 my @oggcmd = ('oggenc', '-r', '-b', $bitrate, '-o', "$oggfile.tmp");
100 if($ti) { push @oggcmd, "-t", $ti; }
101 if($ar) { push @oggcmd, "-a", $ar; }
102 if($al) { push @oggcmd, "-l", $al; }
103 if($tr) { push @oggcmd, "-N", $tr; }
105 if(open(STDIN, "-|") == 0) {
106 exec $flac, qw/-sdc --endian=little --sign=signed --force-raw-format/,
107 $flacfile;
110 system @oggcmd, '-';
112 if($?) {
113 warn "$flacfile: oggenc failed.\n";
114 close STDIN;
115 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
116 if(($? & 0x7f) == 2) { die "aborted.\n"; }
117 next;
119 close STDIN;
120 if($?) {
121 warn "$flacfile: $flac -d failed.\n";
122 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
123 if(($? & 0x7f) == 2) { die "aborted.\n"; }
124 next;
127 if(-e $oggfile) {
128 warn "$oggfile exists, skipping.\n";
129 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
130 next;
132 rename "$oggfile.tmp", $oggfile or do {
133 warn "rename $oggfile.tmp -> $oggfile: $!\n";
134 unlink "$oggfile.tmp" or warn "delete $oggfile.tmp: $!\n";
135 next;
137 if($opt_d) {
138 unlink $flacfile or warn "unlink $flacfile: $!\n";