]>
pere.pagekite.me Git - homepage.git/blob - linux/cd2mp3
3 # Author: Espen Skoglund <espensk@stud.cs.uit.no>
4 # Date: before 1999-02-08
6 # Rip music from CD and convert to MP3
8 # Hacked by Petter Reinholdtsen <pere@td.org.uit.no>, 1999-02-08
11 print "USAGE: $0 [-h][-t] <topdir> [<start track> [<endtrack>]]\n";
17 while ( $_ = shift ) {
20 } elsif ( /^-?-h(elp)?$/ ) {
21 $0 =~ s!.*/([^/]+)$!\1!;
28 ($topdir, $trbegin, $trend) = @args;
35 $dagrab = find_prog_in_path
("dagrab");
36 $cdparanoia = find_prog_in_path
("cdparanoia");
37 $cdda2wav = find_prog_in_path
("cdda2wav");
38 $gcdplay = find_prog_in_path
("gcdplay");
40 $l3enc = find_prog_in_path
("l3enc");
41 $bladeenc = find_prog_in_path
("bladeenc");
42 $mp3_8hz = find_prog_in_path
("8hz-mp3");
46 $mp3encoder = "$l3enc _infile_ _outfile_";
47 } elsif ( $bladeenc ) {
48 $mp3encoder = "$bladeenc _infile_ _outfile_ -br 192000";
49 } elsif ( $mp3_8hz ) {
50 $mp3encoder = "$mp3_8hz -b 128 _infile_ _outfile_";
53 $tmp = $ENV{TMPDIR
} || '/tmp';
57 ## Get artist and tracknages from CDDB
59 # extract online from CDDB
60 open (GCDPLAY
, "$gcdplay -q -i|") || die "open($gcdplay -q -i |): $!\n";
64 ($artist) = m
%^CD TITLE
: (.+\S
)\s
*/[^/]+$%;
65 ($title) = m
%/\s
*([^/]+)$%;
68 my ($track, $name) = m/^(\d+): (.+)$/;
69 $names[$track] = $name;
74 print "Unable to find gcdplay. It is required to fetch title and track\n";
75 print "names from CDDB. Fetch gcdplay from\n";
76 print "<URL:http://www.korax.net/~mglisic/gcdplay/>.\n";
82 print "Artist: $artist\n";
83 print "Title: $title\n";
84 $destdir = "$topdir/$artist/$title";
85 mkpath
($destdir, 0, 0775);
87 print "Error: CD is unknown to CDDB. Unable to extract info, and\n";
88 print "thereby unable to rip. Add it to CDDB.\n";
91 $numnames = scalar(@names);
95 open( TOC
, "$cdparanoia -Q 2>&1|" ) || die "open(cdparanoia -Q |): $!\n";
97 push( @times, $1 ) if /^\s*\d+\.\s*\d+\s*\[(\d+:\d*)/;
100 } elsif ( $dagrab ) {
101 open( TOC
, "$dagrab -i 2>&1|" ) || die "open(dagrab -i |): $!\n";
103 push( @times, $1 ) if /\d\d:(\d\d:\d\d)/;
106 } elsif ( $gcdplay ) {
107 open( TOC
, "$gcdplay -di 2>&1|" ) || die "open(gcdplay -di |): $!\n";
108 my (@starttimes, $endtime);
111 $endtime = $1 * 60 + $2 if /^\d+ Tracks, (\d+) minutes and (\d+) seconds/;
112 push(@starttimes, $1 * 60 + $2) if /minute: \[(\d+)\] second: \[(\d+)\]/;
117 for $time (@starttimes, $endtime) {
119 my $sec = $time - $lasttime;
120 my $time = sprintf("%02d:%02d", int($sec / 60), ($sec%60));
126 $numtracks = scalar(@times);
128 ## Warn if the number of tracknames does not match number of tracks
129 if ( $numtracks && !$rip_times_only && $numnames != $numtracks ) {
130 warn ("Warn: The number of names is not equal to the Number of ".
131 "tracks on CD.\n (Number of tracks = $numtracks, ".
132 "number of names = $numnames)\n");
134 $trbegin = 1 unless $trbegin;
135 $trend = $numtracks if $numtracks && !$trend;
138 $timesfile = "$destdir/.times";
139 open( TIMES
, ">$timesfile" ) || die "open(>$timesfile): $!\n";
140 foreach $time ( @times ) {
141 print TIMES
"$time\n";
145 exit if $rip_times_only;
147 ## Start processing the tracks
149 foreach $track ( $trbegin .. $trend ) {
151 # Get the track from the CD.
153 system "$cdda2wav -D /dev/cdrom -x -t $track+$track ".
154 "$tmp/track-$track.wav 2>&-";
155 } elsif ( $dagrab ) {
156 system "$dagrab -f $tmp/track-$track.wav $track 2>/dev/null 1>/dev/null";
157 } elsif ( $cdparanoia ) {
158 system "$cdparanoia $track $tmp/track-$track.wav 2>&-";
160 die "I don't know of any cdrippers.\n";
163 # Wait for the previous encoding to get finished.
164 waitpid( $pid, 0 ) if $pid != 0;
166 # Fork off a new process that does the encoding. We may then start
167 # reading another track from the CD.
168 unless ( $pid = fork ) {
170 my $time = $times[$track];
171 my $name = $names[$track] || "Track $track";
172 my $mp3enc = $mp3encoder;
173 my $destfile = sprintf( "%02d - %s.mp3", $track, $name );
174 $destfile =~ s/\"/\\\"/g;
175 $destfile =~ s/\\/\\\\/g;
176 $mp3enc =~ s!_infile_!$tmp/track-$track.wav!;
177 $mp3enc =~ s!_outfile_!\"$destfile\"!;
179 print sprintf("Encoding: %02d - $name\n", $track);
181 # MP3 encode the track
182 system "$mp3enc >/dev/null 2>&1";
184 # Remove temporary wav file.
185 unlink "$tmp/track-$track.wav";
191 # Wait for last encoding to be finished
192 waitpid( $pid, 0 ) if $pid;
194 sub find_prog_in_path
{
196 for $dir (split(/:/, $ENV{PATH
})) {
197 my $fullpath = "$dir/$prog";
198 return $fullpath if ( -x
$fullpath );