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') # 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()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)
- Assistant
- User
- Assistant
The code you've shared seems to be correct and following the right steps to recognize the song and attempt to fetch lyrics from Genius. However, it looks like no search hits are found on Genius for the provided song title and artist name. This issue might be due to a few reasons:
- User
- Assistant
- User
- Assistant
- User
(myenv) C:\Users\ILEG-i5-11\Downloads\Compressed\Duplicate>python dupwillitwork.py Traceback (most recent call last): File "C:\Users\ILEG-i5-11\Downloads\Compressed\Duplicate\dupwillitwork.py", line 8, in html = BeautifulSoup(page.text, "html.parser") ^^^^ NameError: name 'page' is not defined. Did you mean: 'range'?
- Assistant
The error you're encountering is because the
pagevariable is referenced before it's defined. It seems you have the BeautifulSoup code snippet placed outside of theget_lyrics_from_geniusfunction where thepagevariable has not yet been created.The BeautifulSoup snippet needs to be placed inside the function after you make a request to the Genius lyrics URL. Here is the correct placement within the function:
- User
how to scrap the genius
Couldn't find the lyrics division.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.