Recently I got a collection of bulgarian songs with very nice filenames but no ID3 tags. So I wrote a simple script that reads a file with name like so: “trackN artist – song.mp3″, converts it to cyrillic and writes the tags.

The format of the filename can be easily changed by modifying the regex and you can add other tags as well. I knew in advance that my compilation will have 500 songs, that’s why the track numbers are set to “$track/500″. At this point is is quite custom script for my needs but can be easily changed to translate to other languages and modify other tags.

The cyrillic conversion is not perfect, but I’m too lasy to fix it as it isn’t that bad either.

  1. #!/usr/bin/perl -w
  2. use Data::Dumper;
  3. use MP3::Tag;
  4. use Encode;
  5.  
  6. my $filename = $ARGV[0];
  7. my $artist;
  8. my $track;
  9. my $song;
  10.  
  11. print "Working on $filename\n";
  12.  
  13. @cyr = ('ч','щ','ш','ю','а','б','в','г','д','е','ж','з','и','й','к'
  14. ,'л','м','н','о','п','р','с','т','у','ф','х','ц','ъ','ь','я',
  15. 'у','Ч','Щ','Ш','Ю','А','Б','В', 'Г','Д','Е','Ж','З','И','Й',
  16. 'К','Л','М','Н','О','П','Р','С','Т','У','Ф','Х','Ц','Ъ','Ь','Я','У');
  17.  
  18. @lat = ('ch','sht','sh','iu','a','b','v','g','d','e','j','z','i','j',
  19. 'k','l','m','n','o','p','r','s','t','u','f','h','c','y','y',
  20. 'q','w','Ch','Sht','Sh','Iu','A','B','V', 'G','D','E','J','Z',
  21. 'I','J','K','L','M','N','O','P','R','S','T','U','F','H','C','Y','Y','Q','W');
  22.  
  23. sub trim($)
  24. {
  25. my $string = shift;
  26. $string =~ s/^\s+//;
  27. $string =~ s/\s+$//;
  28. return $string;
  29. }
  30.  
  31. $temp=$filename;
  32. $temp=~s/\.mp3//gi;
  33. if ($temp =~ m/([\d]{3})\s+(.+\s*.+)\s?-\s?(.+\s?.+)/)
  34. {
  35. $track = $1;
  36. $artist = $2;
  37. $song = $3;
  38. }
  39. my $retext = "$track $artist - $song";
  40. print $retext."\t";
  41. $i = 0;
  42. while($i < @lat) {
  43. $l = $lat[$i];
  44. $c = $cyr[$i];
  45. $artist=~s/$l/$c/g;
  46. $song=~s/$l/$c/g;
  47.  
  48. #print $l."\t".$c."\n";
  49. $i++;
  50. }
  51.  
  52.  
  53. $songname = "$track ".trim($artist)." - ".trim($song);
  54. print $songname."\n";
  55.  
  56. $mp3 = MP3::Tag->new($filename);
  57. $mp3->get_tags;
  58. my $id3v2 = $mp3->new_tag("ID3v2");
  59. $id3v2->add_frame('TRCK', "$track/500");
  60. $id3v2->add_frame('TIT2', decode('UTF-8',$song));
  61. $id3v2->add_frame('TPE1', decode('UTF-8',$artist));
  62. $id3v2->write_tag;
  63.