Josh .O'Neill
Josh .O'Neill

Reputation: 23

using taglib# to extract mp3 tags

How would i use taglib to extract the id3 tags of an entire folder of mp3 files, I am currently only able to get it to extract the tag of say genre for one mp3 file at a time.I need to be able to extract the id3 tag from an entire folder of mp3 files.

The mp3 files that match the required genre then need to be copied to a new folder location which i have been able to do for one mp3 file but not multiple.

Upvotes: 2

Views: 1241

Answers (1)

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

You need to go through all the files in the directory and check whether each of them has the required genre.

For that, you can use some LINQ:

string genre = "Hip-Hop, Rock"; // Change as required... You can also provide a single genre or even more than 2.
var matchingFiles = Directory.GetFiles(@"Folder\SubFolder", "*.mp3", SearchOption.AllDirectories).Where(x => { var f = TagLib.File.Create(x); return ((TagLib.Id3v2.Tag) f.GetTag(TagTypes.Id3v2)).JoinedGenres == genre; });
foreach (string f in matchingFiles)
{
     System.IO.File.Move(f, Path.Combine(@"D:\NewFolder", new FileInfo(f).Name));
}

I would also like to point out that if the file has multiple genres, you can even set the criteria to select all files which contain that genre:

var matchingFiles = Directory.GetFiles(@"Folder\SubFolder", "*.mp3", SearchOption.AllDirectories).Where(x => { var f = TagLib.File.Create(x); return ((TagLib.Id3v2.Tag) f.GetTag(TagTypes.Id3v2)).Genres.Contains(genre); });

The above would, for example, select files with the following genres, when genre is set to Hip-Hop:

Hip-Hop
Hip-Hop, Rock
Hip-Hop, Trap
Hip-Hop, Rock, Punk, Trap

The above set of genres is obviously not realistic. It's just an example :)

Upvotes: 1

Related Questions