Izaya
Izaya

Reputation: 1568

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 ?

Upvotes: 0

Views: 529

Answers (2)

Wieland
Wieland

Reputation: 1681

Your acoustID is not an acoustid, but the MusicBrainz Identifier (MBID) of a Recording.

You're correct that musicbrainzngs.get_recording_by_id by default only returns a very small set of data. It's a wrapper around the MusicBrainz lookup API that returns data based on what was requested via the inc parameter. That is available in musicbrainzngs as the includes parameter to the get_*_by_id functions. If you use

musicbrainzngs.get_recording_by_id("0f6eb38a-d6c9-4f87-a9a7-6e7b0eeb4281", includes=["artists", "releases"])

more data - in this case about the artists of the recording and the releases it appears on - will be returned. You need to define yourself (or ask the user) to chose the correct release, because you can't tell from the MBID of the recording alone which release the should be considered "correct".

Upvotes: 1

Izaya
Izaya

Reputation: 1568

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

Upvotes: 0

Related Questions