Git Product home page Git Product logo

spotify-to-mp3-python's Introduction

Spotify to MP3 - Python

The simplest way to convert/download your Spotify playlist into MP3 files, using Python 3.

How To Use

This code is meant to be as simple and easy-to-use as possible. Despite this, there are some setup and usage steps (such as installing necessary packages) that are necessary for this code to work. Please read this section carefully.

1. Clone the respository

To clone this repoistory using Git, use

git clone https://github.com/JayChen35/spotify-to-mp3-python.git

If you aren't familiar with Git, navigate to the top-left of this page and find the green button labeled "Clone or download". Clicking this and then click "Download ZIP". Extract the contents of the downloaded .zip file.

Open a terminal session and navigate to this folder, using cd.

cd spotify-to-mp3-python/

2. Installing dependencies

We will be installing dependencies using pip, the official Python package manager. If you do not have pip, I'd recommend checking this thread to install it.

Copy and paste (and run) the following line in your terminal session to install all necessary packages.

pip3 install spotipy && pip3 install youtube_dl && pip3 install youtube_search && pip3 install yt_dlp && pip3 install ffprobe && pip3 install ffmpeg

3. Setting up Spotify

Unfortunately, I could not find a workaround for this step - it seems like we're forced to go through the Spotify API to fetch information about playlists. But, it doesn't take long at all.

Go to the Spotify dashboard. Log in. Once at the Dashboard, click the green button labeled "Create App". Don't worry - you're not signing up for anything or commiting to something from Spotify. Here, it really doesn't matter what you put for "App name" and "App description". For me, I just put "Testing" for both. Make sure to check both agreement boxes and click "Create".

You should see this:

Spotify App Screen

You will see the "Client ID" field on the left (it's redacted here). Copy and save your Client ID somewhere - you'll need it later. Click "Show client secret" under Client ID and it should show you another long list of characters. Also copy and save your Client Secret.

Next, we need your playlist URI. To do this, simply open Spotify, right-click on the playlist you want to download, hover over "Share", and click "Copy Spotify URI". It should look something like this: spotify:playlist:37i9dQZEVXbJiZcmkrIHGU. When inputting this into the script, make sure to only input the characters after "spotify:playlist:". So for this example, input 37i9dQZEVXbJiZcmkrIHGU. Save your URI somewhere handy.

Alternatively, you can find the URI as follows:

  1. Right-click on the playlist you want to download
  2. Click "Share"
  3. Click "Embed Playlist"
  4. Click "Show Code"
  5. The URI is the code between "https://open.spotify.com/embed/playlist/" and the first "?"

For example in this code snippet:

<iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/11cPCycyvvpL0MDLO648vE?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>

The code is 11cPCycyvvpL0MDLO648vE

4. Running

Running this script is straightforward. Simply run in your terminal session:

python3 spotify_to_mp3.py

If you run into an error saying something like "ffprobe or avprobe not found", check out this solution.

If all goes well, you should see your playlist beginning to download in a folder with the same name. Enjoy!

Modifications

If you don't like inputting your Client ID, Client Secret, Username, and URI every time, you can edit lines 96-99 in spotify_to_mp3.py to set the respective variables into a string containing your credentials instead of prompting with input(). For example, line 98 would become

username = "YourUserName"

Debugging

This script was made in the better part of an afternoon and so it's not, by far, bug-free. Personally, I've run into no problems using this script on any of my playlists, however, your mileage may vary. The most promenant bug I've run into involves the youtube-search package not consistantly turning up results, and most of the time, the best solution is to simply try running the script again and giving it more chances to get the search right.

spotify-to-mp3-python's People

Contributors

alalonde avatar alwinraju avatar everettperiman avatar gabriel-mesq avatar jaychen35 avatar kaganhazalkocdemir avatar kareemxyz avatar kolliden avatar yas19sin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

spotify-to-mp3-python's Issues

ffprobe and ffmpeg not found.

" ffprobe and ffmpeg not found. "
"PostProcessingError('ffprobe and ffmpeg not found. Please install or provide the path using --ffmpeg-location')"

Hey! I'm not sure why it's bringing this message up... I installed all the packages successfully on my terminal and in PyCharm as well.
It only downloads the .txt file and .webm file, no .mp3...
Let me know if you know how to fix this!! thank you!

Got error: HTTP Error 403: Forbidden

The code used to run a couple of months ago, but when I return, I get error 403. When I run it, it starts downloading the YouTube URL data, then I get many errors. The program still makes the folder at the end, except there are .webm.ytdl files rather than MP3.

HTTP Error for GET to https://api.spotify.com/v1/playlists/ returned 400 due to Only valid bearer authentication supported

Hey @JayChen35, I'm still experiencing the issue. Any thoughts as to why?

Playlist URI (excluding "spotify:playlist:"): 6PECGhMPyi4ZsGr8QmqOVE
/Users/J/Dev/spotify-to-mp3-python/spotify_to_mp3.py:14: DeprecationWarning: You're using 'as_dict = True'.get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead.
  token = credentials.get_access_token()
HTTP Error for GET to https://api.spotify.com/v1/playlists/6PECGhMPyi4ZsGr8QmqOVE returned 400 due to Only valid bearer authentication supported
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/spotipy/client.py", line 245, in _internal_call
    response.raise_for_status()
  File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/playlists/6PECGhMPyi4ZsGr8QmqOVE?fields=tracks%2Cnext%2Cname&additional_types=track

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/J/Dev/spotify-to-mp3-python/spotify_to_mp3.py", line 105, in <module>
    playlist_name = write_playlist(username, playlist_uri)
  File "/Users/J/Dev/spotify-to-mp3-python/spotify_to_mp3.py", line 49, in write_playlist
    results = spotify.user_playlist(username, playlist_id, fields='tracks,next,name')
  File "/usr/local/lib/python3.9/site-packages/spotipy/client.py", line 715, in user_playlist
    return self.playlist(playlist_id, fields=fields, market=market)
  File "/usr/local/lib/python3.9/site-packages/spotipy/client.py", line 611, in playlist
    return self._get(
  File "/usr/local/lib/python3.9/site-packages/spotipy/client.py", line 291, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/usr/local/lib/python3.9/site-packages/spotipy/client.py", line 261, in _internal_call
    raise SpotifyException(
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/playlists/6PECGhMPyi4ZsGr8QmqOVE?fields=tracks%2Cnext%2Cname&additional_types=track:
 Only valid bearer authentication supported, reason: None

This playlist not download

Hello,

I execute your script but
playlist_uri = "spotify:playlist:3P1G51oVE116LbqjmzlSIT"
image

Not download

Only valid bearer authentication supported, reason: None

I tried to run this and here's the error i got:

spotify_to_mp3.py:14: DeprecationWarning: You're using 'as_dict = True'.get_access_token will return the token string directly in future versions. Please adjust your code accordingly, or use get_cached_token instead.
  token = credentials.get_access_token()
HTTP Error for GET to https://api.spotify.com/v1/playlists/1KvRs5rFnFv2gHDkD91bSz returned 400 due to Only valid bearer authentication supported
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/spotipy/client.py", line 245, in _internal_call
    response.raise_for_status()
  File "/usr/local/lib/python3.7/dist-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/playlists/1KvRs5rFnFv2gHDkD91bSz?fields=tracks%2Cnext%2Cname&additional_types=track

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "spotify_to_mp3.py", line 107, in <module>
    playlist_name = write_playlist(username, playlist_uri)
  File "spotify_to_mp3.py", line 49, in write_playlist
    results = spotify.user_playlist(username, playlist_id, fields='tracks,next,name')
  File "/usr/local/lib/python3.7/dist-packages/spotipy/client.py", line 715, in user_playlist
    return self.playlist(playlist_id, fields=fields, market=market)
  File "/usr/local/lib/python3.7/dist-packages/spotipy/client.py", line 615, in playlist
    additional_types=",".join(additional_types),
  File "/usr/local/lib/python3.7/dist-packages/spotipy/client.py", line 291, in _get
    return self._internal_call("GET", url, payload, kwargs)
  File "/usr/local/lib/python3.7/dist-packages/spotipy/client.py", line 266, in _internal_call
    headers=response.headers,
spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/playlists/1KvRs5rFnFv2gHDkD91bSz?fields=tracks%2Cnext%2Cname&additional_types=track:
 Only valid bearer authentication supported, reason: None

<playlistname>.txt already exists

Where in the world does this even put the files? I'm happy to delete the text file, but I can't figure out where it lives (and as a result, where the music will live, apparently).

Nevermid. I SWEAR I looked in that folder and saw nothing. And now it's there. Carry on.

Can't seem to figure out how this works

In "Read me" you say: "When inputting this into the script, make sure to only input the characters after "spotify:playlist:". ", but I'm not sure where I have to input the URI or what to do with my Client ID or my Client Secret.

Also if I run the script "python3 spotify_to_mp3.py" I get this:
Traceback (most recent call last):
File "/home/Lorenzo/spotify-to-mp3-python/spotify_to_mp3.py", line 7, in
import yt_dlp
ModuleNotFoundError: No module named ' yt_dlp '

Also as you can tell I don't know much about python, but I'm trying to get into it.
If you could help that would be much appreciated and I appreciate a lot what you do!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.