rename_wavs.pl 1.7 KB

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