Home .NET A handy library for working with media tags

A handy library for working with media tags

by admin

Greetings to all of you hubrabs. One beautiful evening I had the idea to sort my gigabytes of music. More precisely, I wanted to simply delete all the tracks with low bitrate, so as not to irritate your ears and not to take up space in vain. Of course, I could try to do everything manually, but firstly, I’m too lazy and secondly, it would take me a few weeks, if not months. Besides, was I a programmer or what? With this simple reasoning, I came to the idea that it would be worth automating the process. The question of the language of automation was not particularly important: at work I write in C#, at home I also have an express version of Visual Studio, so it must be so. The only thing I need to know is how to check the bitrate of the files using Sharp and Visual Studio in order not to delete anything unnecessary. Short but productive searching led me to one excellent library.

Short description

So, our library is called Taglib-sharp and, as you can see from the name, is designed to work with the tags of the media files (video, audio and photo). The project is open source and distributed for free under the LGPL license. Download the library (for example, from here or from here ), add a link to it in our project, put TagLib in using and here we are ready to go.
Here is a little example how we get information from tags. This example isn’t complete, you can easily find the rest in the library’s help.

//Create a variable with file information. Specify full path as a parameter.var audioFile = TagLib.File.Create(@"C:UsersEthiopianMusicFeindbild.mp3");//display the information we need on the screenConsole.WriteLine("Album : {0} {n Artist : {1} {n Title : {2} {n Year : {3} {n Duration : {4}", audioFile.Tag.Album, String.Join(", ", audioFile.Tag.Performers), audioFile.Tag.Title., audioFile.Tag.Year, audioFile.Properties.Duration.ToString("mm:ss"));

Executing the code above will result in something like :

Album : GotterdammerungArtist : MegaherzTitle : FeindbildYear : 2002Duration : 04:41

Well, we’ve learned how to get information, and now it’s time to use it.

Practical application

After we’ve played around with the library, it’s time to go back to the original task. What did we want to do there? Aha! Remove all low bitrate recordings. And here is where property .Properties.AudioBitrate, which is perfect for us, comes into play. We will use it to sort out useless files.
For our modest purposes, it will be enough to use simple console utility, which will take path to files folder, and then it will sort files by tags and remove unwanted files. The method of deletion itself is the following:

private private void RemoveBadAudio(string foldPath){//Create an object to access the directory, the path of which was written in the console.DirectoryInfo dir = new DirectoryInfo(foldPath);//Create two variables for counting the deleted and abandoned filesint deletedCounter = 0;int savedCounter = 0;//Check each mp3 file in the directoryforeach (FileInfo file in dir.GetFiles("*.mp3")){//Check bitrateif (TagLib.File.Create(file.FullName).Properties.AudioBitrate < 320){file.Delete();deletedCounter++;}else{savedCounter++;}}Console.WriteLine("ndeleted files : {0} {nSaved files : {1}", deletedCounter, savedCounter);}

Well, that’s all, the program works and does its job. Please don’t kick me for the code, the main task was to make it fast and "to make it work".
UPD: After publishing this article, I received one rather important question from my comrade : "What’s the status of subdirectories?"
Indeed, initially the given method works only with files lying in the root of a given directory. To capture files in subdirectories, you can use the :

private private void RemoveBadFilesInSubDirectories(string foldPath){DirectoryInfo parentDirectory = new DirectoryInfo(foldPath);foreach (DirectoryInfo dir in parentDirectory.GetDirectories()){//Here we call the method to remove unnecessary files described above.//The only difference is that now we don't pass the path to the directory as a parameter, but the dir object.}}

In conclusion

We have considered one of possible applications of Taglib-sharp library and at the same time saved our ears from low-quality mp3 recordings. Of course, this is not the only use of Taglib-sharp library. For example, you can automate renaming files by tags or sorting files by artist folders. Well, there are many of them, if you start and don’t be afraid. And for that I’ll say goodbye. I hope to see you soon.

You may also like