1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 if ( ! defined $ARGV[0] ) {
7 print "Need to pass dir\n";
8 exit 1;
9 }
10
11 my $dir_path = shift(@ARGV);
12
13 if ( ! -d $dir_path ) {
14 print "$dir_path doesnt look like a dir\n";
15 exit 1;
16 }
17
18 my @info_files;
19 my @audio_files;
20
21 foreach my $file ( split("\n",`ls $dir_path`) ) {
22
23 if ( $file =~ m/^(audio_[0-9]{2}.inf)/ ) {
24 push(@info_files, $1);
25 } elsif ( $file =~ m/^(audio_[0-9]{2}.wav)/ ) {
26 push(@audio_files, $1);
27 }
28
29 }
30
31 my %tracks_map;
32 @tracks_map{@audio_files} = @info_files;
33
34 sub sanitize_title($) {
35
36 my $title = shift;
37 my $san_title;
38 open(my $fh, ">>", \$san_title) or die "Couldnt open $!\n";
39 foreach my $char ( split("",$title) ) {
40 if ( $char =~ m/[\'\"\:\,\.\&\(\)]/ ) {
41 print $fh "";
42 } elsif ( $char =~ m/\s/ ) {
43 print $fh "_";
44 } else {
45 print $fh "$char";
46 }
47 }
48
49 close $fh;
50 return $san_title;
51
52 }
53
54 foreach my $audio_file ( sort keys %tracks_map ) {
55 my $track_title = "null";
56 foreach my $line ( split("\n", `cat $dir_path/$tracks_map{$audio_file}`) ) {
57 if ( $line =~ m/^Tracktitle=\s+(.*)$/ ) {
58 $track_title = sanitize_title($1);
59 }
60 }
61 if ( $track_title eq "null" ) {
62 print "Couldnt get title from $tracks_map{$audio_file}\n";
63 exit 1;
64 }
65 #print "$audio_file -> $tracks_map{$audio_file} -> '$track_title'\n";
66 $tracks_map{$audio_file} = $track_title;
67 }
68
69 foreach my $audio_file ( sort keys %tracks_map ) {
70
71 my $filename;
72 if ( $audio_file =~ m/^audio_([0-9]{2}).wav/ ) {
73 $filename = $1 . "_" . $tracks_map{$audio_file} . ".wav";
74 } else {
75 print "could get track number\n";
76 exit 1;
77 }
78
79 print "mv $dir_path/$audio_file $dir_path/$filename\n";
80 system("mv $dir_path/$audio_file $dir_path/$filename") == 0
81 or die "Failed to mv $audio_file\n";
82
83 }