Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

wscd2mp3

by strredwolf (Chaplain)
on May 17, 2001 at 22:30 UTC ( #81329=sourcecode: print w/replies, xml ) Need Help??
Category: Audio Related Programs
Author/Contact Info strredwolf
Description: I got tired of other scripts/etc which took info from CDDB and failed to rip a CD into MP3's. So I wrote up my own scripts! The first one (wscd2wav) gets the CDDB info, uses cdparanoia, and puts the .wav into it's own directory, setting a symlink for each one. The second one (scan4wav2mp3) looks for the symlinks, reads where the .wav is, and uses lame on the .wav. So far it's working very well.

To use, open up two xterms (or use splitvt or screen) and change into the directory you want the MP3's to go to in both. Run wscd2wav in the first, scan4wav2mp3 in the second.

wscd2wav:
#!/usr/bin/perl

### Some of this code came from CHOAD (especially the CDDB handler)

### Modules
use CDDB;

### Stuff
my $HOME = $ENV{HOME};
my $USER = $ENV{USER};
my $HOST = `hostname`; $HOST =~ s/\n//;

$currcd=0;
while(!$done) {
  
  ### Check the CDDB info on this person.
  my(@toc,$ntracks,$i);

  $i=0;
  open(IN,"cdparanoia -Q 2>&1 |") || die $1;
  while(<IN>) {
    chomp;
    if(/^\s*(\d+)\.\s+\d+\s+\[\d+:\d+\.\d+\]\s+\d+\s+\[(\d+):(\d+)\.(\
+d+)\]/) {
      $toc[$i++] = "$1 $2 $3 $4";
      $ntracks=$1;
    } elsif (/^TOTAL\s+[0-9]+\s+\[([0-9]+):([0-9]+)\.([0-9]+)\]/) {
      $toc[$i++] = "999 $1 $2 $3";  # lead-out track
    }
  }
  close IN;

  ### Now get CDDB...
  my $cddb = new CDDB( Host  => "freedb.freedb.org",
               Port  => 888,
               Login => "$USER\@$HOST",
             ) or die $!;

  my ($cddb_id, $track_numbers, $track_lengths, $track_offsets, 
      $total_seconds) = $cddb->calculate_id(@toc);
  
  my @discs = $cddb->get_discs($cddb_id, $track_offsets, $total_second
+s);
  
  if (scalar @discs > 0) {
    ($genre, my $cddb_id, my $title) = @{$discs[0]};
    $title=~/([^\/]*) \/ (.*)/; 
    $artist = $1; $album = $2;
    
    my $disc_info     = $cddb->get_disc_details($genre, $cddb_id);
    @tracks  = @{$disc_info->{'ttitles'}};
  }

  ### Work out the prefixes...
  $prefix = "$artist/$album";
  $prefix =~ tr/ ,.\'\"&//d;
  system "mkdir -p $prefix";
 
  for($i=0; $i<$ntracks; $i++) {
    $f=$tracks[$i];
    $f =~ s/\/[^\/]+$//;
    $f =~ tr/ ,.\'\"&//d;
    $f =~ s/\([^\)]+\)//g;
    $fname="$prefix/$f";
    
    $t=$i+1;

    print "$t>$fname\n";
    system "cdparanoia $t $fname.wav";
    symlink "$fname.wav", "$currcd-$i.trk";
  }

  system "eject -r /dev/hdc";
  $currcd++;
  print "Pausing...";
  <STDIN>;
}

scan4wav2mp3:

#!/usr/bin/perl

print "Scanning for MP3's...\n";
while(1)
{
    sleep 10;
    opendir(DIR,".");
    @d=grep {/\.trk$/} ( readdir(DIR) );
    closedir(DIR);
    
    if(@d) {
    $f=readlink $d[0];
    print "Reencoding $f...\n";
    system "lame -b 128 $f";
    unlink $f;
    unlink $d[0];
    }
}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://81329]
help
Chatterbox?
[Corion]: Two-way data binding means that if you change something in a text box, the corresponding variable in your Javascript gets updated immediately as well, and if you then change the variable, the corresponding text box also gets updated
[Corion]: That is a horrible approach that was also taken by Access, but if you have hooks that prevent automatic saving to the database, it is incredibly handy

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2017-03-30 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (355 votes). Check out past polls.