Skip to content

Instantly share code, notes, and snippets.

@pirtleshell
Created December 8, 2016 03:05
Show Gist options
  • Save pirtleshell/47af6c40647e37f00470fbe401bcc54d to your computer and use it in GitHub Desktop.
Save pirtleshell/47af6c40647e37f00470fbe401bcc54d to your computer and use it in GitHub Desktop.
Easily create a musical fingerprint from an MP3 and submit it to AcoustId with musicbrainz recording id (must be embedded in MP3).
import acoustid # the library for creating the fingerprints
import urllib.request # for sending the request to AcoustId's server
from mutagen.id3 import ID3 # for extracting the musicbrainz recording id
def main():
api_key = YOUR_API_KEY # find it at https://acoustid.org/api-key
path = '/path/to/directory/of/music'
songs = ['song1.mp3', 'anothersong.mp3'] # an array of the songs in that path
paths = [path + song for song in songs]
for i,song in enumerate(paths):
print('Submitting song) %i'%i)
submit_fingerprint(api_key, song)
def submit_fingerprint(api_key, path):
duration, fprint = acoustid.fingerprint_file(path)
mbid = get_musicbrainz_recording_id(ID3(path))
# build the request for acoustid's web service API. for more info, check out https://acoustid.org/webservice
url = 'http://api.acoustid.org/v2/submit?client=Cem7sCuJSAI' # this is the web API key listed in the docs. you should probably register your own app
url += '&user=' + api_key
url += '&duration.0='+str(int(duration))
url += '&fingerprint.0=' + fprint.decode('utf-8')
url += '&mbid.0=' + mbid
# send the request
r = urllib.request.urlopen(url)
print('Submitted.')
def get_musicbrainz_recording_id(id3):
# the mp3's are assumed to have the recording id embedded in them.
# this can easily be done by importing your music to beets (https://github.com/beetbox/beets)
return id3.getall('UFID')[0].data.decode('utf-8')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment