#!/usr/bin/perl
use strict;
use warnings;
if ( ! defined $ARGV[0] ) {
print "Need to pass dir\n";
exit 1;
}
my $dir_path = shift(@ARGV);
if ( ! -d $dir_path ) {
print "$dir_path doesnt look like a dir\n";
exit 1;
}
my @info_files;
my @audio_files;
foreach my $file ( split("\n",`ls $dir_path`) ) {
if ( $file =~ m/^(audio_[0-9]{2}.inf)/ ) {
push(@info_files, $1);
} elsif ( $file =~ m/^(audio_[0-9]{2}.wav)/ ) {
push(@audio_files, $1);
}
}
my %tracks_map;
@tracks_map{@audio_files} = @info_files;
sub sanitize_title($) {
my $title = shift;
my $san_title;
open(my $fh, ">>", \$san_title) or die "Couldnt open $!\n";
foreach my $char ( split("",$title) ) {
if ( $char =~ m/[\'\"\:\,\.\&\(\)]/ ) {
print $fh "";
} elsif ( $char =~ m/\s/ ) {
print $fh "_";
} else {
print $fh "$char";
}
}
close $fh;
return $san_title;
}
foreach my $audio_file ( sort keys %tracks_map ) {
my $track_title = "null";
foreach my $line ( split("\n", `cat $dir_path/$tracks_map{$audio_file}`) ) {
if ( $line =~ m/^Tracktitle=\s+(.*)$/ ) {
$track_title = sanitize_title($1);
}
}
if ( $track_title eq "null" ) {
print "Couldnt get title from $tracks_map{$audio_file}\n";
exit 1;
}
#print "$audio_file -> $tracks_map{$audio_file} -> '$track_title'\n";
$tracks_map{$audio_file} = $track_title;
}
foreach my $audio_file ( sort keys %tracks_map ) {
my $filename;
if ( $audio_file =~ m/^audio_([0-9]{2}).wav/ ) {
$filename = $1 . "_" . $tracks_map{$audio_file} . ".wav";
} else {
print "could get track number\n";
exit 1;
}
print "mv $dir_path/$audio_file $dir_path/$filename\n";
system("mv $dir_path/$audio_file $dir_path/$filename") == 0
or die "Failed to mv $audio_file\n";
}