Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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?
[ambrus]: marto: no, not that way. if you just want to test for an existing id attribute, then write *[@id] , or you can match a regexp to the value of that attribute like *[@id=~/something/ ]
[marto]: ambrus, thanks I ended up going with '//*[starts- with(@id, "thread_")]...
[ambrus]: Sometimes it's easier to just match elements in ways other than xpath, like directly with perl code.
[marto]: this is a cheap hack, a script that doesn't need to run for long, using WWW::Mechanize:: Firefox to drive some awful interface.
[marto]: if it were anything more serious I'd spend more time looking into it properly :P

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2016-12-08 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (140 votes). Check out past polls.