Greetings to all habravchan. One fine evening I had the idea to sort out my gigabytes of music. And to be more precise, I simply wanted to delete all the tracks with a low bit rate, so as not to irritate the ears and how much they did not take the place. One could, of course, do everything manually, but, first, laziness, and second, it would take several weeks, if not months. Anyway, I'm a programmer or where? With such plain reasoning, I came to the conclusion that it would be worth automating the process. The question of the language in which the automation will be made was not particularly: I write at work on C #, at home there is also an express version of Visual Studio, which means so. It remains only to find out how with the help of these very sharps and studios to find out the bitrate of the files, so as not to remove anything superfluous. A short but productive googleg led me to one wonderful library.
Short description
So, our library is called Taglib-sharp and, as the name implies, is designed to work with media tags (video, audio and photo). The project is open source and distributed free of charge under the LGPL. Download the library (for example,
from here or
from here ), add a link to it in our project, register TagLib in using and now we are ready to start.
Below is a small example of obtaining information from the tags. The example is not complete, the rest can be easily found in the library help.
// . . var audioFile = TagLib.File.Create(@"C:\Users\Ethiopian\Music\Feindbild.mp3"); // Console.WriteLine(": {0}\n: {1}\n: {2}\n: {3}\n: {4}" , audioFile.Tag.Album , String.Join(", ", audioFile.Tag.Performers) , audioFile.Tag.Title , audioFile.Tag.Year , audioFile.Properties.Duration.ToString("mm\\:ss"));
As a result of executing the code above, the console will produce something like:
')
: Gotterdammerung : Megaherz : Feindbild : 2002 : 04:41
Well, at the very least, they learned how to get information, it's time to use it.
Practical use
After playing with the library, it’s time to return to the original task. What did we want to do there? Aha Delete all entries with a low bit rate. And this is where the .Properties.AudioBitrate property, which is ideally suited for us, comes onto the scene. On it we also will filter out unusable files.
For our modest goals, the usual console utility will be quite enough; as a parameter, we will pass the path to the folder with files, and it will already rake files by tags and delete unnecessary ones. The removal method itself turned out this:
private static void RemoveBadAudio(string foldPath) { // , . DirectoryInfo dir = new DirectoryInfo(foldPath); // int deletedCounter = 0; int savedCounter = 0; // mp3 foreach (FileInfo file in dir.GetFiles("*.mp3")) { // if (TagLib.File.Create(file.FullName).Properties.AudioBitrate < 320) { file.Delete(); deletedCounter++; } else { savedCounter++; } } Console.WriteLine("\n\ : {0} \n : {1}", deletedCounter, savedCounter); }
Well, that's it, the program works, it performs its task. Please do not kick for the code, the main task was to do it quickly and “so that it worked”.
UPD: After the article was published, I received one rather important question from my friend: “What about the subdirectories?”.
Indeed, the originally presented method works only with files lying in the root of a given directory. To capture files in subdirectories, you can use the method:
private static void RemoveBadFilesInSubDirectories(string foldPath) { DirectoryInfo parentDirectory = new DirectoryInfo(foldPath); foreach (DirectoryInfo dir in parentDirectory.GetDirectories()) {
Finally
We considered one of the possible applications of the Taglib-sharp library and simultaneously saved our ears from low-quality mp3 recordings. Of course, this is not the only application of this library. For example, you can automate the renaming of files by tags or the sorting of files by artist folders. Yes, a lot of things, the main thing to start and not be afraid. And for this, let me leave. Hope to see you soon.