]> pere.pagekite.me Git - homepage.git/blob - linux/cd2mp3
Generated.
[homepage.git] / linux / cd2mp3
1 #!/usr/bin/perl
2 #
3 # Author: Espen Skoglund <espensk@stud.cs.uit.no>
4 # Date: before 1999-02-08
5 #
6 # Rip music from CD and convert to MP3
7 #
8 # Hacked by Petter Reinholdtsen <pere@td.org.uit.no>, 1999-02-08
9
10 sub usage {
11 print "USAGE: $0 [-h][-t] <topdir> [<start track> [<endtrack>]]\n";
12 }
13
14 use File::Path;
15
16 $rip_times_only = 0;
17 while ( $_ = shift ) {
18 if ( /^-t$/ ) {
19 $rip_times_only = 1;
20 } elsif ( /^-?-h(elp)?$/ ) {
21 $0 =~ s!.*/([^/]+)$!\1!;
22 usage();
23 exit;
24 } else {
25 push( @args, $_ );
26 }
27 }
28 ($topdir, $trbegin, $trend) = @args;
29
30 if ( ! $topdir ) {
31 usage();
32 exit;
33 }
34
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");
39
40 $l3enc = find_prog_in_path("l3enc");
41 $bladeenc = find_prog_in_path("bladeenc");
42 $mp3_8hz = find_prog_in_path("8hz-mp3");
43
44 # Choose encoder
45 if ( $l3enc ) {
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_";
51 }
52
53 $tmp = $ENV{TMPDIR} || '/tmp';
54
55 $[ = 1;
56
57 ## Get artist and tracknages from CDDB
58 if ( $gcdplay ) {
59 # extract online from CDDB
60 open (GCDPLAY, "$gcdplay -q -i|") || die "open($gcdplay -q -i |): $!\n";
61 while (<GCDPLAY>) {
62 chomp;
63 if (m/^CD TITLE:/) {
64 ($artist) = m%^CD TITLE: (.+\S)\s*/[^/]+$%;
65 ($title) = m%/\s*([^/]+)$%;
66 }
67 if (m/\d+: /) {
68 my ($track, $name) = m/^(\d+): (.+)$/;
69 $names[$track] = $name;
70 }
71 }
72 close(GCDPLAY);
73 } else {
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";
77 exit;
78 }
79
80 if ($artist) {
81 $artist =~ s%/%&%g;
82 print "Artist: $artist\n";
83 print "Title: $title\n";
84 $destdir = "$topdir/$artist/$title";
85 mkpath($destdir, 0, 0775);
86 } else {
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";
89 exit;
90 }
91 $numnames = scalar(@names);
92
93 ## Get tracktimes
94 if ( $cdparanoia ) {
95 open( TOC, "$cdparanoia -Q 2>&1|" ) || die "open(cdparanoia -Q |): $!\n";
96 while( <TOC> ) {
97 push( @times, $1 ) if /^\s*\d+\.\s*\d+\s*\[(\d+:\d*)/;
98 }
99 close TOC;
100 } elsif ( $dagrab ) {
101 open( TOC, "$dagrab -i 2>&1|" ) || die "open(dagrab -i |): $!\n";
102 while( <TOC> ) {
103 push( @times, $1 ) if /\d\d:(\d\d:\d\d)/;
104 }
105 close TOC;
106 } elsif ( $gcdplay ) {
107 open( TOC, "$gcdplay -di 2>&1|" ) || die "open(gcdplay -di |): $!\n";
108 my (@starttimes, $endtime);
109 while( <TOC> ) {
110 chomp;
111 $endtime = $1 * 60 + $2 if /^\d+ Tracks, (\d+) minutes and (\d+) seconds/;
112 push(@starttimes, $1 * 60 + $2) if /minute: \[(\d+)\] second: \[(\d+)\]/;
113 }
114 close TOC;
115
116 my $lasttime;
117 for $time (@starttimes, $endtime) {
118 if ($lasttime) {
119 my $sec = $time - $lasttime;
120 my $time = sprintf("%02d:%02d", int($sec / 60), ($sec%60));
121 push(@times, $time);
122 }
123 $lasttime = $time;
124 }
125 }
126 $numtracks = scalar(@times);
127
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");
133 }
134 $trbegin = 1 unless $trbegin;
135 $trend = $numtracks if $numtracks && !$trend;
136
137 ## Store tracktimes.
138 $timesfile = "$destdir/.times";
139 open( TIMES, ">$timesfile" ) || die "open(>$timesfile): $!\n";
140 foreach $time ( @times ) {
141 print TIMES "$time\n";
142 }
143 close TIMES;
144
145 exit if $rip_times_only;
146
147 ## Start processing the tracks
148 my $pid = 0;
149 foreach $track ( $trbegin .. $trend ) {
150
151 # Get the track from the CD.
152 if ( $cdda2wav ) {
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>&-";
159 } else {
160 die "I don't know of any cdrippers.\n";
161 }
162
163 # Wait for the previous encoding to get finished.
164 waitpid( $pid, 0 ) if $pid != 0;
165
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 ) {
169 chdir($destdir);
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\"!;
178
179 print sprintf("Encoding: %02d - $name\n", $track);
180
181 # MP3 encode the track
182 system "$mp3enc >/dev/null 2>&1";
183
184 # Remove temporary wav file.
185 unlink "$tmp/track-$track.wav";
186 exit;
187 }
188
189 }
190
191 # Wait for last encoding to be finished
192 waitpid( $pid, 0 ) if $pid;
193
194 sub find_prog_in_path {
195 my $prog = shift;
196 for $dir (split(/:/, $ENV{PATH})) {
197 my $fullpath = "$dir/$prog";
198 return $fullpath if ( -x $fullpath );
199 }
200 return; # Not found
201 }
202