import acrcloud import os import eyed3 import requests import json from…
- User
import acrcloud import os import eyed3 import requests import json from acrcloud.recognizer import ACRCloudRecognizer
ACRCloud API credentials
ACR_HOST = "" ACR_ACCESS_KEY = "" ACR_ACCESS_SECRET = ""
ACR Cloud setup
config = { 'host': ACR_HOST, 'access_key': ACR_ACCESS_KEY, 'access_secret': ACR_ACCESS_SECRET, 'timeout': 10 # seconds } dir(acrcloud)
Initialize the ACRCloud recognizer
recognizer = ACRCloudRecognizer(config)
Function to recognize the song from an audio file
def recognize_song(audio_file_path): buffer = open(audio_file_path, 'rb').read() result = recognizer.recognize_by_filebuffer(buffer, 0) try: result_dict = json.loads(result) # Parse the JSON string into a dictionary return result_dict['metadata']['music'][0] except (KeyError, IndexError, json.JSONDecodeError) as e: print(f"Error while parsing result: {e}") return None
Function to set ID3 tags
def set_id3_tags_mp3(audio_file_path, tags): audio_file = eyed3.load(audio_file_path) if not audio_file.tag: audio_file.initTag()
audio_file.tag.artist = tags.get('artists')[0].get('name') audio_file.tag.album = tags.get('album').get('name') audio_file.tag.album_artist = tags.get('artists')[0].get('name') audio_file.tag.title = tags.get('title') # Assuming that track number information is present in the response track_str = tags.get('album').get('track_no') if track_str: try: track_num = int(track_str) audio_file.tag.track_num = track_num except ValueError: print(f"Invalid track number format in the tag: {track_str}") # Assuming all artists information like 'songwriters' and 'performers' is available under the 'artists' key all_artists = tags.get('artists') if all_artists: audio_file.tag.composer = ', '.join(artist.get('name') for artist in all_artists if artist.get('role') == 'Composer') audio_file.tag.performer = ', '.join(artist.get('name') for artist in all_artists if artist.get('role') == 'Performer') # Set the release year (if available) release_date = tags.get('release_date') if release_date and len(release_date) >= 4: # Check if release_date contains at least the year year_string = release_date[:4] try: year = int(year_string) # Some versions of eyeD3 require a Date object if available if hasattr(eyed3.id3.tag, 'Date'): audio_file.tag.recording_date = eyed3.id3.tag.Date(year) else: # Otherwise, set it as text_frame audio_file.tag.setTextFrame("TDRC", year_string) except ValueError: print(f"Invalid date format in the tag: {release_date}") # Add more tags here audio_file.tag.genre = tags.get('genres')[0].get('name') # Assuming there's at least one genre audio_file.tag.publisher = "Karthik" # Publisher tag set as 'karthik' # To save the copyright label: audio_file.tag.copyright = tags.get('label', '') # To save the album cover page, you would need to download the image from a source # and then do something like this: # with open("path_to_cover_image.jpg", "rb") as album_art: # audio_file.tag.images.set(3, album_art.read(), "image/jpeg", u"Description") # Example of setting explicit tag in the comments (if you have explicit info): audio_file.tag.comments.set(u"Explicit: Yes") audio_file.tag.save(version=eyed3.id3.ID3_V2_3) audio_file.tag.save() - Assistant
- User
MusicBrainz fetch error: caused by: HTTP Error 400: Bad Request Traceback (most recent call last): File "C:\Users\ILEG-i5-11\Downloads\Compressed\Duplicate\dupwillitwork.py", line 93, in external_cover_path, cover_url = get_album_cover(mbid, save_path_external) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: cannot unpack non-iterable NoneType object
- Assistant