Create a GUI to Extract Lyrics from a song using Python


Lyrics are the words that are sung in a song that conveys the meaning and emotions of a song. Python offers several libraries to extract lyrics from a song. In this tutorial, we will create a Graphical User Interface (GUI) using Python’s tkinter library that extracts lyrics from a song.

What are the different formats in which songs are available online?

There are several formats in which songs are available online, depending on the platform and the type of file. Some of the most common formats are −

MP3 (MPEG Audio Layer 3) − This is the most common format for music files, which compresses the audio data and removes parts of the file that are not audible to the human ear. MP3 files are widely compatible with most devices and platforms.

AAC (Advanced Audio Coding) − This is a more efficient format than MP3, which offers better sound quality with smaller file sizes. AAC files are commonly used by Apple's iTunes and other Apple devices.

WAV (Waveform Audio File Format) − This is a lossless format that stores audio data in its raw, uncompressed form, resulting in large file sizes. WAV files are commonly used by professionals in the music industry for recording and mixing.

FLAC (Free Lossless Audio Codec) − This is a high-quality, lossless format that compresses audio data without sacrificing quality, resulting in smaller file sizes than WAV files. FLAC files are commonly used by audiophiles and music enthusiasts who want the best possible sound quality.

OGG (Ogg Vorbis) − This is an open-source, lossy format that is similar to MP3 and AAC, but offers better sound quality with smaller file sizes. OGG files are commonly used for streaming music online.

WMA (Windows Media Audio) − This is a proprietary format developed by Microsoft, which is similar to MP3 and AAC, but is less widely compatible with other devices and platforms.

These are some of the most common formats in which songs are available online, and the choice of format may depend on factors such as the platform, the type of device, and the desired sound quality.

What do we understand by encoding and decoding with respect to audio files?

Encoding and decoding are two important concepts in the context of audio files, which refer to the process of converting audio data from one format to another.

Encoding refers to the process of compressing audio data into a specific format to reduce the file size and make it easier to transmit, store, and play back. During encoding, the audio data is analyzed, processed, and converted into a digital format that can be stored as a file. The most common audio encoding formats include MP3, AAC, FLAC, and OGG, among others.

Decoding, on the other hand, refers to the process of converting an encoded audio file back into its original, uncompressed form for playback. During decoding, the compressed audio data is decompressed and converted back into a waveform that can be played back through speakers or headphones.

In summary, encoding and decoding are two key processes involved in the creation and playback of digital audio files. Encoding involves compressing audio data into a specific format for storage, while decoding involves converting the compressed audio file back into its original form for playback.

Prerequisites

Before we dive into the details of creating a GUI, you should have a basic understanding of Python programming, object-oriented programming (OOP) concepts, and how to work with the Tkinter module.

List of recommended settings

  • pip install tkinter, Lyricsgenius library

  • It is expected that the user will have access to any standalone IDE such as VS-Code, PyCharm, Atom or Sublime text.

  • Even online Python compilers can also be used such as Kaggle.com, Google Cloud platform or any other will do.

  • Updated version of Python. At the time of writing the article I have used 3.10.9 version.

  • Knowledge of the use of Jupyter notebook.

  • Knowledge and application of virtual environment would be beneficial but not required also use of APIs in some context would be useful

Steps required to accomplish the task

Let us begin creating a GUI using tkinter to extract lyrics from a song. Create a python file and name it main.py.

Step 1: Import the necessary modules

from tkinter import *
from lyricsgenius import Genius

Next, we need to import the necessary libraries in your Python script. In this case, we need the lyricsgenius library for lyrics extraction and the tkinter library for creating the GUI interface. Here's the import statement

Step 2: Create a GUI window

First, we create a new window using the Tk() function from the tkinter library. We also set the title and dimensions of the window using the title() and geometry() functions, respectively.

window = tk.Tk()
window.title("Lyrics Extractor")
window.geometry("400x300")

Step 3: Create a label for the songs:

Next, we create a label and a text box for the song name using the Label() and Entry() functions from tkinter, respectively. We also create a label and a text box for the artist name.

song_label = tk.Label(window, text="Song Name:")
song_entry = tk.Entry(window)
artist_label = tk.Label(window, text="Artist:")
artist_entry = tk.Entry(window)

We pack these elements in the window using the pack() function.

song_label.pack()
song_entry.pack()
artist_label.pack()
artist_entry.pack()

Step 4: Creating buttons

We also create a button for extracting the lyrics using the Button() function, and pack it in the window.

extract_button = tk.Button(window, text="Extract Lyrics", command=extract_lyrics)
extract_button.pack()

Finally, we create a text box for displaying the lyrics using the Text() function, and pack it in the window.

lyrics_text = tk.Text(window)
lyrics_text.pack()

Step 5: Extracting the Lyrics

We define a function extract_lyrics() that will be called when the user clicks the "Extract Lyrics" button. This function retrieves the song name and artist from the text boxes using the get() function, creates a Genius object using the lyricsgenius library, and searches for the song using the search_song() function. If the song is found, the lyrics are displayed in the text box using the insert() function.

def extract_lyrics():
   # Get the song name and artist from the text boxes
   song = song_entry.get()
   artist = artist_entry.get()

   # Create a Genius object and search for the song
   genius = Genius("YOUR_ACCESS_TOKEN_HERE")
   song = genius.search_song(song, artist)

   # Display the lyrics in the text box
   if song is not None:
      lyrics_text.delete(1.0, tk.END)
      lyrics_text.insert(tk.END, song.lyrics)
   else:
      lyrics_text.delete(1.0, tk.END)
      lyrics_text.insert(tk.END, "Lyrics not found.")

Note that you'll need to obtain a Genius access token to use this code. You can obtain one by creating an account on the Genius website and following the instructions in the documentation for the lyricsgenius library.

Final code, program

import tkinter as tk
from lyricsgenius import Genius

def extract_lyrics():
   # Get the song name and artist from the text boxes
   song = song_entry.get()
   artist = artist_entry.get()

   # Create a Genius object and search for the song
   genius = Genius("YOUR_ACCESS_TOKEN_HERE")
   song = genius.search_song(song, artist)

   # Display the lyrics in the text box
   if song is not None:
      lyrics_text.delete(1.0, tk.END)
      lyrics_text.insert(tk.END, song.lyrics)
   else:
      lyrics_text.delete(1.0, tk.END)
      lyrics_text.insert(tk.END, "Lyrics not found.")
# Create a new window
window = tk.Tk()
window.title("Lyrics Extractor")
window.geometry("400x300")

# Add a label for the song name
song_label = tk.Label(window, text="Song Name:")
song_label.pack()

# Add a text box for the song name
song_entry = tk.Entry(window)
song_entry.pack()

# Add a label for the artist name
artist_label = tk.Label(window, text="Artist:")
artist_label.pack()

# Add a text box for the artist name
artist_entry = tk.Entry(window)
artist_entry.pack()

# Add a button to extract the lyrics
extract_button = tk.Button(window, text="Extract Lyrics", command=extract_lyrics)
extract_button.pack()

# Add a text box to display the lyrics
lyrics_text = tk.Text(window)
lyrics_text.pack()

Output

If the song is found, the lyrics are displayed in the text box. Otherwise, a message is displayed indicating that the lyrics were not found.

Note that you'll need to obtain a Genius access token to use this code. You can obtain one by creating an account on the Genius website and following the instructions in the documentation for the lyricsgenius library.

Conclusion

Hence, we have found that the above mentioned code provides a simple and easy-to-use GUI for extracting the lyrics of a song using Python and the lyricsgenius library. Users can enter the song name and artist in text boxes, click a button to extract the lyrics, and view the lyrics in a text box.

Updated on: 20-Apr-2023

600 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements