This blog is about my project, Audio Classifier. Audio Classifier is what you need to sort wrong named music files in messed up music directories. It uses the web service https://acoustid.org to find the details of the musics files in your directory.

The Project was done in Python. What the code does is, make a fingerprint of the music file in your given directory and find a match for it on the acoustid database and return the details of the file to the user. For that user have to register on https://acoustid.org and get an API key for the application. For using it, user should install pyacoustid and import acoustid to get functions.There are many ways to lookup music details on Acoustid. One way is to use the acoustid.fingerprint_file and acoustid.lookup functions. It is as follows,

>>> import acoustid
>>> duration, fingerprint = acoustid.fingerprint_file(path)
>>> details = acoustid.lookup(api_key, fingerprint, duration)

From the details we can get the artist name, album name, song title, etc. Another way is to use the acoustid.match function. It is as follows,

>>> import acoustid
>>> for score, recording_id, title, artist in acoustid.match(api_key, path):
>>>     ...

The match function is what used in the Audio Classifier project. The project uses only artist and title from the data returned by match function. The acoustid.match returns many combinations of artist and title, among them the most occuring combination is found to be the right one. Audio Classifier choose most occuring combination and classify file according to the user choice.

User is given option to just rename the files as artist-title.mp3 or rename it as title.mp3 and move it to directory named artist. There is two more option for user as recurse and inplace. What inplace does is keep the current file as it is and make a copy of it with the name found. Recurse helps you to sort the audio inside subdirectories of the given path.

The arguments to run the project are as follows:

positional arguments:
  path           path of directory to classify

optional arguments:
  -h, --help     show this help message and exit
  -d, --dire     Classified as artist/title.mp3,if False renamed as artist-
                 title.mp3, False by default
  -i, --inplace  Keeps existing files. False by default
  -r, --recurse  Classify subdirectory files, False by default

Here is an example of using the Audio classifier project. The following is how my music folder before using it.

(py3) majid@maG:~/Music$ tree
.
├── 1.mp3
├── a.mp3
├── faodij.mp3
├── fdslf.mp3
├── fgiodjfsa.mp3
└── ss.mp3

I run the program as follows:

python audio_classifier.py /home/majid/Music -d

My music folder now,

(py3) majid@maG:~/Music$ tree
.
├── Ben E. King
│   └── Stand by Me.mp3
├── Coldplay
│   └── Hymn for the Weekend.mp3
├── Eric Clapton
│   ├── Layla.mp3
│   └── Tears in Heaven.mp3
├── Oasis
│   └── Wonderwall.mp3
└── The Beatles
    └── Let It Be.mp3

Tests

One thing new I did with testing was using mock. For that unittest was imported. It is a library that let us mock what a part of our program do in case of testing. We can decide what a function should return during testing with mock.patch(). Here is how i used it to manipulate what glob.glob should return:

from unittest import mock
class Test_get_mp3(unittest.TestCase):

    def test_get_mp3(self):
        with mock.patch('audio_classifier.glob.glob') as mocked_glob:
            mocked_glob.return_value = ['/tmp/2.mp3','/tmp/3.mp3','/tmp/4.mp3']
            mp3_list = get_mp3("/tmp")
            self.assertEqual(len(mp3_list),3)

Here is the link to my project https://github.com/kmmajidh/Audio-classifier. Try it.

Thank you.