import os
import requests
AUDD_URL = "https://api.audd.io/"
def recognize_track(mp3_file, api_token):
# Check whether the file exists
if not os.path.isfile(mp3_file):
print("ERROR: File not found")
return
# Check file size
file_size = os.path.getsize(mp3_file)
if file_size > 10 * 1024 * 1024:
print("ERROR: File is larger than 10 MB")
return
print("Recognising:", mp3_file)
try:
with open(mp3_file, "rb") as audio_file:
files = {
"file": audio_file
}
data = {
"api_token": api_token
}
response = requests.post(
AUDD_URL,
files=files,
data=data,
timeout=60
)
except requests.exceptions.RequestException as e:
print("Network error:")
print(e)
return
except Exception as e:
print("Error:")
print(e)
return
# Get JSON response
try:
result = response.json()
except ValueError:
print("ERROR: AudD returned an invalid response")
print(response.text)
return
# Display API error
if result.get("status") == "error":
error = result.get("error", {})
print("AUDD API ERROR")
print("Error code:", error.get("error_code"))
print("Error message:", error.get("error_message"))
return
# Get recognition result
track = result.get("result")
# No match
if track is None:
print("No match")
return
# Track recognised
artist = track.get("artist", "Unknown artist")
title = track.get("title", "Unknown title")
print()
print("MATCH FOUND")
print("Artist:", artist)
print("Title :", title)
album = track.get("album")
if album:
print("Album :", album)
# ------------------------------------
# CHANGE THESE TWO VALUES
# ------------------------------------
MP3_FILE = r"song.mp3"
API_TOKEN = "***"
# ------------------------------------
# Run recognition
# ------------------------------------
recognize_track(MP3_FILE, API_TOKEN)
Output:
Recognising: song.mp3
MATCH FOUND
Artist: Wali Bird
Title : Perfection (Interlude)
Album : B.I.R.D