-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdda-cddbrip
executable file
·176 lines (129 loc) · 4.44 KB
/
cdda-cddbrip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/perl -w
#
# input: - audio CD in /dev/cdrom, (optional) internet connection freedb.freedb.org
# - (optional) command line: -encoders=encoder1[,encoder2,...]
# encoders to use
# default: -encoders=ogg,mp3
# existing encoders: ogg, mp3, wav
# -start=num -stop=num
# first and last track to encode (numbers 1-based)
# default: entire CD
#
# output: ripped files, named "<artist>--<title>.<wav|ogg|mp3>"
package Encoders;
{
use File::Temp qw/ tempfile tempdir /;
#my ($fh, $tmpfile) = tempfile( DIR => tempdir( CLEANUP => 1 ) );
my ($fh, $tmpfile) = tempfile();
$tmpfile .= '.wav';
##private
sub mysystem {
my $cmdstr = join ' ', (map {"'$_'"} @_);
print "cdda-cddbrip: running: $cmdstr\n";
system(@_) and die "couldn't execute: $cmdstr: $!";
}
## private
sub tofilename {
my ($artist, $title) = @_;
my $fn = ($artist?"${artist}--":"") . $title;
$fn =~ s![/ ,;:\'\"]!_!g;
$fn;
}
my $ripped_wav; # cache for rip_wav
## private
# rip($num) -- rip track $num to wav file, return filename
sub rip_wav {
my ($num) = @_;
return $tmpfile if ($num eq $ripped_wav);
$ripped_wav = $num;
mysystem("cdparanoia --output-wav $num $tmpfile");
$tmpfile;
}
sub encode_wav {
my ($num,$artist,$title,$album) = @_;
my $wavfile = rip_wav($num);
my $fn = tofilename($artist, $title);
mysystem ("cp", "-f", $wavfile, "${fn}.wav");
}
sub encode_mp3 {
my ($num,$artist,$title,$album) = @_;
my $wavfile = rip_wav($num);
my $fn = tofilename($artist, $title);
mysystem ( qw/lame -h -b 192/,
'--tt', $title,
($artist? ('--ta',$artist): ()),
($album? ('--tl',$album): ()),
$wavfile,
"${fn}.mp3" );
}
sub encode_ogg {
my ($num,$artist,$title,$album) = @_;
my $wavfile = rip_wav($num);
my $fn = tofilename($artist, $title);
mysystem ( qw/oggenc -b 160/,
'-t', $title,
($artist? ('-a',$artist): ()),
($album? ('-l',$album): ()),
"-o", "${fn}.ogg",
$wavfile );
}
sub encode_echo {
my ($num,$artist,$title,$album) = @_;
print "encode_echo: ", join ' ', (map {"'$_'"} (@_, tofilename($artist, $title))), "\n";
}
sub cleanup {
mysystem("rm","-f",$tmpfile);
}
}
# main prg
package main;
use strict;
no strict 'refs';
use Getopt::Long;
my (@encoders, $start, $stop);
$start=1; $stop=100000;
exit(1) unless GetOptions ("encoders=s" => \@encoders,
"start=s" => \$start,
"stop=s" => \$stop);
@encoders = qw/mp3 ogg/ unless (@encoders); # default
print "start: $start, stop: $stop, encoders: ",join(',',@encoders),"\n";
@encoders = map {"Encoders::encode_${_}"} @encoders;
foreach (@encoders) {
die "undefined encoder: $_" unless defined *{$_}{CODE};
}
use CDDB_get qw( get_cddb );
my %config;
# following variables just need to be declared if different from defaults
#$config{CDDB_HOST}="freedb.freedb.org"; # set cddb host
#$config{CDDB_PORT}=8880; # set cddb port
#$config{CDDB_MODE}="cddb"; # set cddb mode: cddb or http
#$config{CD_DEVICE}="/dev/cdrom"; # set cd device
# user interaction welcome?
$config{input}=1; # 1: ask user if more than one possibility
# 0: no user interaction
# get it on
my %cd=get_cddb(\%config);
my $cdalbum = $cd{title};
my $cdartist = $cd{artist} unless ($cd{artist} =~ /^various/i);
# precaution against some buggy cddb entries...
{
local $/="\r";
chomp($cdalbum); chomp($cdartist);
$/="\n";
chomp($cdalbum); chomp($cdartist);
}
my $n=1;
foreach (@{$cd{track}}) {
next if ($n<$start);
last if ($n>$stop);
my ($artist, $title) = $cdartist? ($cdartist, $_) :
(m!\s*(.*?)\s*/\s*(.*)\s*!? ($1,$2) : (undef, $_));
foreach my $enc (@encoders) {
$enc->($n, $artist, $title, $cdalbum);
}
}
continue {
$n++;
}
# TODO: finally
Encoders::cleanup();