January 4, 2020

How to download a spotify song or playlist (using Python)

For my kids I use following way to put their spotify playlist on their mp3-player.
It is based on some python scripts available here: https://github.com/ritiek/spotify-downloader

Installation

Of course Python is required. (pip will be installed automatically)

Install spotify-downloader using 
>> pip install -U spotdl
from the Windows command window.
Don't forget to download ffmpeg.exe ; copy the ffmpeg.exe from the bin directory  to have them in your Path but not to C:\Windows\System32 (see section below path issues). Else the conversion to mp3 won't work.

To run pip from my work environment I need to be connected to the work network and the http_proxy and https_ proxy environment variables have to be set appropriately.

Download song/ playlist

  • Copy in Spotify the link to the playlist or song you want to download - clicking on the 3 dots.
  • Open the command line 
To download a song use the command:
>> spotdl -s https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ
For downloading playlist and albums, you need to first load all the tracks into text file and then pass this text file to --list argument. Here is how you would do it for a playlist
$ spotdl --playlist https://open.spotify.com/user/nocopyrightsounds/playlist/7sZbq8QGyMnhKPcLJvCUFD
INFO: Writing 62 tracks to ncs-releases.txt
$ spotdl --list ncs-releases.txt
You can also use as short commands -p instead of --playlist and -l instead of --list.

By default, the mp3 files are downloaded under your default user Music folder.
Metadata like song name, artist etc. are appropriately set (from YouTube).

Troubleshooting

Proxy Handling

To run spotdl, an open connection without proxy is required. If you have set the environment variable http_proxy and https_proxy you need to reset them before running the script.

>> set http_proxy=
>> set https_proxy=

pip install only works through the proxy even if the env variables https_proxy are reset it won't work through a direct connection.

Path issues / multiple accounts


This is an example of batch file:
cd C:\Users\leoda\spotdl
C:\Users\User\AppData\Local\Programs\Python\Python38-32\Scripts\spotdl -p https://open.spotify.com/playlist/3vwnLEqq2oR5aucDxpTQEO
C:\Users\User\AppData\Local\Programs\Python\Python38-32\Scripts\spotdl -l leo-der-coole.txt
pause

Update YouTube-dl

If you get some errors it may help to update the youtube-dl package using:
>> pip install -U youtube-dl


No comments:

Post a Comment