import json import os import requests from acrcloud.recognizer import ACRCloudRecognizer…
- User
import json import os import requests from acrcloud.recognizer import ACRCloudRecognizer from genius_api import GeniusApi # Assume genius_api.py is in the same directory and contains the GeniusApi class
ACRCloud API credentials
ACR_HOST = "" ACR_ACCESS_KEY = "" ACR_ACCESS_SECRET = ""
ACR Cloud configuration (update with your credentials)
config = { 'host': ACR_HOST, 'access_key': ACR_ACCESS_KEY, 'access_secret': ACR_ACCESS_SECRET, 'timeout': 10 # seconds }
Initialize the ACRCloud recognizer
recognizer = ACRCloudRecognizer(config)
Initialize the Genius API
genius_api = GeniusApi()
Function to recognize a song using ACRCloud
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) return result_dict['metadata']['music'][0] except (KeyError, IndexError, json.JSONDecodeError) as e: print(f"Error while parsing result: {e}") return None
Function to fetch lyrics from Genius
def get_lyrics_from_genius(artist_name, song_title): # Search for the song on Genius results = genius_api.get_search_by_songs(f"{artist_name} {song_title}") if results: song_info = results[0]['result'] # Take the most relevant result song_id = str(song_info['id']) song_details = genius_api.get_song_by_id(song_id, text_format='plain') return song_details.get('lyrics', "Lyrics not available.") return "Song not found on Genius."
def save_lyrics_to_lrc(lyrics, lrc_path): with open(lrc_path, 'w', encoding='utf-8') as lrc_file: lrc_file.write(lyrics)
Main program to recognize a song and fetch its lyrics
if name == "main": audio_file_path = 'C:/Users/ILEG-i5-11/Downloads/Music/Unknown_file.mp3' # Replace with the actual file path lrc_file_path = 'C:/Users/ILEG-i5-11/Downloads/Music/Unknown_file.lrc' # Output LRC file path song_tags = recognize_song(audio_file_path) if song_tags: artist_name = song_tags['artists'][0]['name'] song_title = song_tags['title'] print(f"Identified Song: {artist_name} - {song_title}") lyrics = get_lyrics_from_genius(artist_name, song_title) if 'plain' in lyrics: lyrics_plain_text = lyrics['plain'] print("Lyrics:\n", lyrics_plain_text) save_lyrics_to_lrc(lyrics_plain_text, lrc_file_path) print(f"Lyrics saved to: {lrc_file_path}") else: print("No lyrics available to save.") else: print("Could not identify the song.")
this code is only giving me only lrc lyrics . I need lrc with timestamps to be played with the audio file
- Assistant
If you want to obtain time-stamped lyrics in the LRC format, which can be synced with an audio file during playback, you will need access to a synchronized lyrics source. Typically, LRC files with time-stamped lyrics also referred to as "synchronized LRC" or "enhanced LRC", involve a more complex process to create than just fetching the plain text lyrics. Genius API doesn't provide time-stamped lyrics, only text.
- User
different service (which service)?
- Assistant