Search code examples
python-3.xmusicbrainz

How to get track metadata from acoustID with Python


I have a file.mp3 with unknown tags. I've used this to get the acoustID:

import acoustid
acoustID= acoustid.match(API_KEY, filepath)

acoustID contains several candidates, let's say I choose the best one: '0f6eb38a-d6c9-4f87-a9a7-6e7b0eeb4281'.

I've tried this but it doesn't give many info:

import musicbrainzngs
musicbrainzngs.get_recording_by_id(acoustID)

How to get the corresponding tags (album, tack number, genre, band, etc...) from this acoustID ?


Solution

  • Finally found this: https://acousticbrainz.org/. From your browser you can use this url: https://acousticbrainz.org/<my acoustID>. From your code you can get a big json data file with all metadata on this track from this url: https://acousticbrainz.org/api/v1/<my acoustID>/low-level

    For a python script you can use something like this:

    import acoustid
    import urllib.request, json
    
    file = "path/to/myAudioFile"
    API_KEY = 'cSpUJKpD'
    
    candidates = acoustid.match(API_KEY, file)  # returns a generator of candidates
    
    best_score, best_acoustId, best_title, best_artist = next(candidates)
    
    metadata = {}
    with urllib.request.urlopen("https://acousticbrainz.org/api/v1/" + best_acoustId + "/low-level") as url:
        data = json.loads(url.read().decode())
        metadata = data['metadata']
    

    For more info about acoustid.match() you can check their github page, especially here: https://github.com/beetbox/pyacoustid/blob/master/aidmatch.py