my code: import acrcloud import os import eyed3 import requests import json from…
- User
my code:
import acrcloud import os import eyed3 import requests import json from acrcloud.recognizer import ACRCloudRecognizer
ACRCloud API credentials
ACR_HOST = "identify-ap-southeast-1.acrcloud.com" ACR_ACCESS_KEY = "fe9d03703ee501887c5570fff859bee9" ACR_ACCESS_SECRET = "PFbmdVo4ZjRkT7AI3l1NLGGtGtsgIbC9vs1ydgYb"
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') # 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', '') audio_file.tag.comments.set(u"Explicit: Yes") audio_file.tag.save(version=eyed3.id3.ID3_V2_3) audio_file.tag.save()Replace 'path_to_your_audio_file.mp3' with the actual file path of the unknown song
if name == "main": audio_file_path = 'C:/Users/ILEG-i5-11/Downloads/Music/Unknown_file.mp3' song_tags = recognize_song(audio_file_path) if song_tags: print(f'Song identified: {song_tags}') set_id3_tags_mp3(audio_file_path, song_tags)
# Renaming the file after identifying the song and setting tags artist_name = song_tags.get('artists')[0].get('name') song_title = song_tags.get('title') if artist_name and song_title: new_file_name = f"{artist_name} - {song_title}.mp3" new_file_path = os.path.join(os.path.dirname(audio_file_path), new_file_name) os.rename(audio_file_path, new_file_path) print(f"File has been renamed to: {new_file_name}") else: print('Could not identify the song.') - Assistant