17 November 2004
This post may be outdated due to it was written on 2004. The links may be broken. The code may be not working anymore. Leave comments if needed.

Task

从网上download了一些歌曲,回来用foobar2000放。发现没有显示其歌曲名和歌手名。
因为迷你歌词/MiniLyrics查找配对歌词要通过mp3的歌曲名和歌手名。一个个修改太麻烦了。就想着用perl写个批量修改。

Code

cpan search了下,install MP3::ID3v1Tag
此模块perldoc说的很清楚,我也就照本宣科写了个代码。
其中获得artist,title,ablum的过程不通用。各位自己去parse
#!/usr/bin/perl
# usage: set one directory mp3's ID3v1Tag, such as artist, title, ablum
use MP3::ID3v1Tag;

my $dir = "E:/Music/Santana_Shaman";
opendir(DIR,"$dir");
my @data = readdir(DIR);
closedir(DIR);
@data = grep(/\.mp3$/, @data);

foreach $mp3 (@data) {
	my $mp3_file = new MP3::ID3v1Tag("$dir/$mp3");
	#if($mp3_file->got_tag()) {
		# it's special,here the file name like "Santana Shaman 11 Feels Like Fire Feat DIDO.mp3"
		# u parse file name and got the artist&album&title&..
		my ($artist, $album, undef, $title) = ($mp3 =~ /^([a-zA-Z]+)\s+([a-zA-Z]+)\s+([0-9]+)\s+(.*?)\.mp3$/);
		print "$artist, $album, $title\n";
		#end
		$mp3_file->set_title("$title");
		$mp3_file->set_artist("$artist");
		$mp3_file->set_album("$album");
		#$mp3_file->set_year(2002);
		#$mp3_file->set_genre("Latin-Rock");
		$mp3_file->save() or warn "failed to save $mp3";
  #}
}


blog comments powered by Disqus