Just for the people who enjoy copy/pasting code snippets into their projects, here's one to extract ID3 information from a single MP3:
private MusicLibraryEntry CreateEntryFromMp3File(FileInfo mp3File)
{
try
{
ID3Tag tag;
// Check for ID3v2 tags first
tag = ID3v2Tag.ReadTag(mp3File.FullName);
if (tag == null)
{
// If no v2 tag, fall back on checking for ID3v1 tags
tag = ID3v1Tag.ReadTag(mp3File.FullName);
}
if (tag == null) // No tag exists
return null;
// TODO: You probably want to use heuristics to derive album/artist/title when none are present
// otherwise, the ID3Sharp code will just fill the values with NULL
MusicLibraryEntry returnVal = new MusicLibraryEntry()
{
Title = tag.Title;
Artist = tag.Artist;
Album = tag.Album;
FilePath = mp3File.FullName;
}
if (string.IsNullOrWhiteSpace(returnVal.Title))
returnVal.Title = fileName.Name;
return returnVal;
}
catch (FormatException e)
{
// The ID3 library tends to throw ArithmeticExceptions and IndexOutOfBounds exceptions
// I modified the source to recast these into FormatExceptions, to indicate a malformed tag.
Console.WriteLine(e.Message);
}
return null;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Just for the people who enjoy copy/pasting code snippets into their projects, here's one to extract ID3 information from a single MP3: