Starbucks audio cd script

vbimport

#1

Howdy folks,
Found a, somewhat, simple solution to the starbucks .aud audio cds. I created a script called starcd2mp3 in perl for it. It requires a couple libs but it should run as is. Hope this helps some of you out. If you need help or have questions/comments i started a topic for it in my forum (http://forum.socklabs.com).
~ Nick <http://www.socklabs.com>


#!/usr/bin/perl
# starcd2mp3
# Author: Nick Gerakines <nick@socklabs.com>
# This software released under the terms of the Artistic License
# <http://www.opensource.org/licenses/artistic-license.html>
#
# Usage:
#   Run this script in the directory of the two files. You can also
# the exact names of the files with the --aud= and --txt= arguments.
# This program can also be run with test, verbose and quiet arguments.
# ex: ./starcd2mp3 (--aud=playdisc.aud) (--txt=data.txt) (--verbose. etc....)
# ex: ./starcd2mp3 (--verbose) (--test) (--quiet)
#
#
# About:
#   This program sifts through the index file for the playfile
# and outputs each song based on its start and length.
#
# Technical:
#   The .aud file is basically a huge chunch of encoded mpeg
# layer 2 audio. The data.txt file acts like an index giving
# pointers to songs inside the .aud file. This program will
# load the index file using the Config::IniFiles lib and then
# sift through each audio program and create a directory for it.
# Then it will sift through each song in that program, output
# the song chunk into a temporary .mp2 file. After that it uses
# lame to encode the temp .mp2 file into an mp3 and moves it to the
# proper directory.
#
# ChangeLog:
# 0.1
#     * First Release

$version = "v0.1";

use File::Find ();
use File::Basename;
use Getopt::Long;
use String::ShellQuote;
use Config::IniFiles;

$lame = "/usr/local/bin/lame";

my $ini;
local *CONFIG;

my $opt_txt = "data.TXT";
my $opt_aud = "playdisc.AUD";

print "starcd2mp3 $version
";
print "(c) 2003-2004 Nick Gerakines
";
print "Released without warranty under the terms of the Artistic License

";

GetOptions("help|?",\&showhelp, "test", "txt=s", "aud=s", "quiet", "verbose");

checkfile();

sub showhelp() {
	print "Usage: $0 [options] dir1 dir2 file1 file2 ...

";
	print "Options:
";
	print "--test		Forces a test run, nothing is changed or created.
";
	print "--txt=file	Set the index file to use, default is data.txt
";
	print "--aud=file	Set the .AUD file to use, default is playdisc.aud
";
	print "--verbose	Verbose output
";
	print "--quiet		Quiet output
";
	print "--help		Display this help message
";
	exit;

}

sub checkfile() {
 $lame_verbose = "--silent";
 if ($opt_quiet) {
  print "Warning: You will not see any error messages!
";
 }
 if ($opt_verbose) {
  print "Doing things verbosely.
";
 }
 $ini = new Config::IniFiles -file => $opt_txt;
 my $progname = $ini->val("Playlist", "Title");
 $songcount = $ini->val("Playlist", "Songs");
 $programcount = $ini->val("Playlist", "Programs");
 print "Loading data for Playlist $progname.
";
 print "Playlist has $songcount songs and $programcount programs.
";
 print "Loading aud file $opt_aud in binmode.
";
 open(AUDFILE,"$opt_aud") or die "Can't open file [$opt_aud]
";
 binmode(AUDFILE);
 print "Load complete.
";
 $ps = 1;
 $psi = 1;
 for ($i=1;$i<$programcount+1;$i++) {
  $pt = "Program$i";
  $ptitle = $ini->val($pt, "Title");
  $ptitle =~ s/[\[\]\(\)\{\}!\@#\$\%^&\*\~ ]/_/g;
  $ptitle =~ s/[\'\"]//g;
  $psongs = $ini->val($pt, "Songs");
  print "Program $i : $ptitle ($psongs Songs)
";
  $pdir = shell_quote($ptitle);
  system("mkdir $ptitle");
  for ($psi = 1;$psi<$psongs+1;$psi++){
    $j = $ps;
   $curr_song = "Song$j";
   $sngt = $ini->val($curr_song, "Title");
   $sngt =~ s/(.*)(
|\r|	|\f).*/$1/g;
   $sngt =~ s/\s{2,}?//g;
   $snga = $ini->val($curr_song, "Artist");
   $snga =~ s/(.*)(
|\r|	|\f).*/$1/g;
   $snga =~ s/\s{2,}?//g;
   $sngl = $ini->val($curr_song, "Length");
   $sngl =~ s/(.*)(
|\r|	|\f).*/$1/g;
   $sngl =~ s/\s{2,}?//g;
   $sngs = $ini->val($curr_song, "Start");
   $sngs =~ s/(.*)(
|\r|	|\f).*/$1/g;
   $sngs =~ s/\s{2,}?//g;
   $song_name = "$snga - $sngt";
   if (!$opt_quiet){
   print "Song $j: \'$song_name\' start: $sngs, length: $sngl
";
   }
   if (!$opt_test) {
   open(SONGFILE, ">".$song_name.".mp2");
   binmode(SONGFILE);
   if(read(AUDFILE, $buff, $sngl)) {
    print SONGFILE $buff;
   }
   }
   $mp3 = shell_quote($song_name.".mp3");
   $mp2 = shell_quote($song_name.".mp2");
   $tt = shell_quote($sngt);
   $ta = shell_quote($snga);
   $tl = shell_quote($progname);
   if ($opt_verbose) {
   $lame_verbose = "";
   print "Encoding using lame:.
";
   print " + $lame $lame_verbose --add-id3v2 --tt $tt --ta $ta --tl $ta --mp2input $mp2 $mp3
";
   }
   if ($opt_verbose) {
   }
   if (!$opt_test) {
   system("$lame $lame_verbose --add-id3v2 --tt $tt --ta $ta --tl $ta --mp2input $mp2 $mp3");
   }
   if ($opt_verbose) {
    print "Encoding Done..
";
    print "Removing temp mp2 file.
";
   }
   if (!$opt_test) {
   system("rm -f $mp2");
   }
   if ($opt_verbose) {
    print "Removal Complete.
";
   }
   system("mv -f $mp3 $pdir");
   close (SONGFILE);
   $ps++;
  }
 }
 close (AUDFILE);
 exit;
}