don't show link to source if it doesn't appear to be a URL
[soepkiptng.git] / raw2ogg
blob4f95182c80e82f7665e75f2b65e1c4aeae3fd9ed
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 my $progdir;
24 BEGIN {
25 use Cwd qw'cwd abs_path';
27 # find program directory
28 $_ = $0;
29 while(-l) {
30 my $l = readlink or die "readlink $_: $!\n";
31 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
33 m|(.*)/|;
34 $progdir = abs_path($1);
36 unshift @INC, "$progdir/lib";
38 use Getopt::Std;
39 use DBI;
40 use MP3::Tag;
42 require "$progdir/soepkiptng.lib";
44 getopts('b:dc:h');
46 $opt_h and die <<EOF;
48 Usage: raw2ogg [-d] [-b bitrate] [-c configfile] file...
50 -d : delete original after successful conversion
51 -b bitrate : kbitrate to encode to (default 192)
52 -c configfile : override soepkiptng config file
54 EOF
56 read_configfile(\%conf, $opt_c);
58 $ENV{PATH} = "$progdir/bin:$ENV{PATH}";
61 $bitrate = $opt_b || 192;
62 $cwd = cwd;
64 $| = 1;
66 sub latin1_to_utf($) {
67 my ($s) = @_;
69 $s =~ s/(.)/pack("U", ord $1)/ge;
70 return $s;
73 $dbh = DBI->connect("DBI:$conf{db_type}:$conf{db_name}:$conf{db_host}",
74 $conf{db_user}, $conf{db_pass})
75 or die "can't connect to database";
77 foreach $rawfile (@ARGV) {
78 next if -d $rawfile;
80 if($rawfile !~ m|^/|) { $rawfile = "$cwd/$rawfile"; }
81 $rawfile =~ s!(^|/)(\./)+!\1!g;
83 ($qfile = $rawfile) =~ s/(.*)\.\w+?$/\1.%/;
84 $q = "SELECT title,artist.name,album.name,track" .
85 " FROM song,artist,album" .
86 " WHERE song.artist_id=artist.id AND song.album_id=album.id" .
87 " AND present AND filename LIKE ?";
88 $sth = $dbh->prepare($q);
89 $sth->execute($qfile)
90 or die "can't do sql command: " . $dbh->errstr;
92 my ($ti, $ar, $al, $tr);
93 ($ti, $ar, $al, $tr) = $sth->fetchrow_array or do {
94 $qfile =~ s|.*/|%/|;
95 $sth->execute($qfile)
96 or die "can't do sql command: " . $dbh->errstr;
98 ($ti, $ar, $al, $tr) = $sth->fetchrow_array or do {
99 warn "$rawfile: not found in dbase\n";
103 my $oggfile = $rawfile;
104 $oggfile =~ s|.*/||;
105 $oggfile =~ s/\.\w+//;
106 $oggfile .= ".ogg";
108 if(-e $oggfile) {
109 warn "$oggfile exists, skipping.\n";
110 next;
113 my @oggargs;
114 if($ti) { push @oggargs, "-t", latin1_to_utf($ti); }
115 if($ar) { push @oggargs, "-a", latin1_to_utf($ar); }
116 if($al) { push @oggargs, "-l", latin1_to_utf($al); }
117 if($tr) { push @oggargs, "-N", $tr; }
119 system 'oggenc', '-r', '-b', $bitrate, @oggargs,
120 '-o', "$oggfile.tmp", $rawfile;
122 if($?) {
123 warn "$rawfile: oggenc failed.\n";
124 close STDIN;
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 $rawfile or warn "unlink $rawfile: $!\n";