#!/usr/bin/perl
# starcd2mp3
# Author: Nick Gerakines
# This software released under the terms of the Artistic License
#
#
# 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)
#
# 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
# 0.2
# * Runtime updates, speed increase by removing extra crap
# * Fixed regex issues
# * Fixed options bug
$version = "v0.2";
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\n";
print "(c) 2003-2004 Nick Gerakines\n";
print "Released without warranty under the terms of the Artistic Licen
+se\n\n";
GetOptions("help|?",\&showhelp, "test", "txt=s", \$opt_txt, "aud=s", \
+$opt_aud);
checkfile();
sub showhelp() {
print "Usage: $0 [options] dir1 dir2 file1 file2 ...\n\n";
print "Options:\n";
print "--testForces a test run, nothing is changed or created.\n";
print "--txt=fileSet the index file to use, default is data.txt\n";
print "--aud=fileSet the .AUD file to use, default is playdisc.aud\n"
+;
print "--helpDisplay this help message\n";
exit;
}
sub checkfile() {
$lame_verbose = "--silent";
$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.\n";
print "Playlist has $songcount songs and $programcount programs.\n";
print "Loading aud file $opt_aud in binmode.\n";
open(AUDFILE,"$opt_aud") or die "Can't open file [$opt_aud]\n";
binmode(AUDFILE);
print "Load complete.\n";
$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)\n";
$pdir = shell_quote($ptitle);
if (-d $pdir) {
print "Directory already exists.\n";
} else {
system("mkdir $ptitle");
}
for ($psi = 1;$psi<$psongs+1;$psi++){
$j = $ps;
$curr_song = "Song$j";
$sngt = $ini->val($curr_song, "Title");
$sngt =~ s/[\[\]\(\)\{\}!\@#\$\%^&\*\~\/]/_/g;
$sngt =~ s/(.*)(\n|\r|\t|\f).*/$1/g;
$sngt =~ s/\s{2,}?//g;
$snga = $ini->val($curr_song, "Artist");
$snga =~ s/[\[\]\(\)\{\}!\@#\$\%^&\*\~\/]/_/g;
$snga =~ s/(.*)(\n|\r|\t|\f).*/$1/g;
$snga =~ s/\s{2,}?//g;
$sngl = $ini->val($curr_song, "Length");
$sngl =~ s/(.*)(\n|\r|\t|\f).*/$1/g;
$sngl =~ s/\s{2,}?//g;
$sngs = $ini->val($curr_song, "Start");
$sngs =~ s/(.*)(\n|\r|\t|\f).*/$1/g;
$sngs =~ s/\s{2,}?//g;
$song_name = "$snga - $sngt";
if (!$opt_quiet){
print "Song $j: \'$song_name\' start: $sngs, length: $sngl\n";
}
if (-f $pdir."/".$song_name.".mp3") {
print "file already exists, skipping.\n";
} else {
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);
system("$lame --silent --add-id3v2 --tt $tt --ta $ta --tl $ta --mp
+2input $mp2 $mp3");
system("rm -f $mp2");
system("mv -f $mp3 $pdir");
close (SONGFILE);
}
$ps++;
}
}
close (AUDFILE);
exit;
}
|