Making Spotify Song Downloader Using Python(mp3)

Trix Cyrus - Sep 24 - - Dev Community

Author: Trix Cyrus

Why Download Spotify Tracks with Python?

For offline listening.
To have your favorite tracks in MP3 format.
For creating a personal music collection.
~ With Python, downloading Spotify tracks becomes a simple, automated task.

Let's Get Started!
Step 1: Install spotdl

First, you'll need to install the spotdl library, which is a lightweight Python tool for downloading Spotify tracks in MP3 format.

Open your terminal and run the following command:

pip install spotdl
Enter fullscreen mode Exit fullscreen mode

Step 2: Writing the Python Script

Now, we will create the Python script to download Spotify tracks, albums, or playlists.

Create a new Python file:

nano spotify_downloader.py
Enter fullscreen mode Exit fullscreen mode

Next, paste the following script in that file:

import subprocess

def download_spotify_mp3():
    print("Spotify to MP3 Downloader")

    content_type = input("What do you want to download? (Enter 'track', 'playlist', or 'album'): ").strip().lower()

    if content_type not in ['track', 'playlist', 'album']:
        print("Invalid choice. Please enter 'track', 'playlist', or 'album'.")
        return

    spotify_url = input(f"Enter the Spotify {content_type} URL: ").strip()

    try:
        print(f"\nDownloading {content_type} as MP3...")
        subprocess.run(["spotdl", "--format", "mp3", spotify_url])
        print(f"\nDownload of {content_type} completed in MP3 format!\n")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    download_spotify_mp3()

Enter fullscreen mode Exit fullscreen mode

Step 3: Running the Script

Once the script is saved, run the following command:

python spotify_downloader.py
Enter fullscreen mode Exit fullscreen mode

The script will prompt you to input the Spotify URL for a track, playlist, or album. It will download the content in MP3 format.

And All Set!

Now you can easily download Spotify tracks in MP3 format directly to your device using Python.

Comment below if any issues or errors occur.

~ TrixSec

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .