Using Python to download a YouTube playlist and convert to mp3 for custom Spotify library

Max Klaben
5 min readMay 14, 2020

--

How is this code useful?

Through quarantine, I started getting pretty bored of my usual Spotify playlists, so I was only listening to new remixes on YouTube for the most part. I ended up making a playlist of remixes on YouTube, but the platform isn’t as good as Spotify for listening to songs, and I couldn’t get these songs on my phone. The playlist I made is over 30 songs, and it was too tedious to use a third party website to download each song as an mp3 to be imported into my Spotify, leading me to write this simple code to do the work for me.

The Setup

The setup here is really short. Spotify has a feature that allows us to use local files for our Spotify playlists. To set this up, click the arrow next to your profile name in the top right of Spotify, then click settings. Scroll down until you see “Local Files,” then enable this option and click “Add a Source.” Create a folder where you want these songs to be held, and it should look like this.

What packages will we be using?

The main package in this project will be pytube, which allows us to easily download YouTube videos or playlists. We will also need moviepy because pytube downloads YouTube videos as an mp4, but we want these files as an mp3. Spotify does allow you to import mp4 files, but mp4 files take up much more space than mp3 files. Lastly, we will need to import os and re, which will be explained later on. Your imports should look like the following.

from pytube import YouTubefrom pytube import Playlistimport osimport moviepy.editor as mpimport re

Downloading the playlist

First, grab your playlist link from YouTube, then we will create a Playlist object, which will look like this.

playlist = Playlist("https://www.youtube.com/playlist?list=PLQD9v8pn7Xi7bqIfuq-Q6h_9aeuMpPpel")

Pytube does have a download_all() function for playlists, but they will tell you to iterate through the playlist, since the function will soon be removed. To understand our playlist object, take a look at the following iterations.

#prints each video url, which is the same as iterating through playlist.video_urlsfor url in playlist:
print(url)
#prints address of each YouTube object in the playlistfor vid in playlist.videos:
print(vid)

Pytube allows filtering for multiple things, including resolution, audio only, and more. The following line of code is intended to download the first audio only type, but I ran into some issues with it. My guess is that not every video will have an audio only file type, but, regardless, if this line of code doesn’t work for you, we’ll work around it. If it does work, here’s code for it.

for url in playlist:
YouTube(url).streams.filter(only_audio=True).first().download()

This line of code converts the url into a YouTube object, gets the streams, filters to only audio file types, then downloads the first stream listed. If this line of code works for you, make sure to check the files that were downloaded to see if they are correct. When using this code, for the videos it worked for, the video lengths were doubled, where the second half of each video was silent.

If this line of code is giving you issues, the following will be the first step in working around this. The following line of code simply downloads the first stream for each video. Also, make sure to specify the download location to be the folder for your Spotify imports, shown below.

for url in playlist:
YouTube(url).streams.first().download('/Users/mklaben21/Desktop/custom songs')

How do we convert all of these files to mp3 from mp4?

As mentioned earlier, Spotify does allow you to import mp4 files, but this takes up much more space. As a result, we will convert each video to an mp3 file, then delete the original mp4 file to save space.

First, we want to get the path for our folder used for Spotify local files. To easily do this on mac, right click your folder then hold down the option key. You will then see an option saying “copy “folder name” as Pathname.” We will now make a string to hold the path for our folder, shown below.

folder = "/Users/mklaben21/Desktop/custom songs"

Now we will use os, re, and moviepy to convert the mp4 files to mp3 files and delete the old mp4 files. We now want to iterate through each file in our folder, create the mp3 files, then delete the mp4 files to clean up. The following code will do just that, which will be explained.

for file in os.listdir(folder):
if re.search('mp4', file):
mp4_path = os.path.join(folder,file)
mp3_path = os.path.join(folder,os.path.splitext(file)[0]+'.mp3')
new_file = mp.AudioFileClip(mp4_path)
new_file.write_audiofile(mp3_path)
os.remove(mp4_path)

Let’s break this down. The first line allows us to iterate through files in the given folder. Next, the if statement allows us to target/make sure the file we are looking at is indeed an mp4 file. mp4_path will give us the path for the mp4 file we are looking at in the specific iteration by joining the folder path with the file name. Next, mp3_path will give us a path to a file with the same name as the mp4 file, but now with an mp3 extension rather than an mp4 extension. new_file allows use to create our new mp3 file by using the AudioFileClip function from moviepy. Next, we actually write this file to the path created by our initialization of mp3_path, so we have now created the mp3 file in that same folder. Lastly, we use the remove function to delete the mp4 file. Each video in the playlist is now downloaded as an mp3 file, and the original mp4 files have been deleted. Your folder should look like this now.

But where are these songs in my Spotify now?

Navigate to the left hand side of your Spotify and look under “Your Library.” Towards the bottom, you will see a section called “Local Files.” After clicking this, you will see all the songs/audios you have just downloaded. You can now add these songs to any of your playlists.

Can I get these downloaded songs on my phone?

Yes. Once you add these songs to a playlist, make sure you have the playlist downloaded on both your computer and your phone. Your computer and phone must be on the same network for this to work. Once on the same network, the local files on the playlist will download to your phone. If your playlist was already downloaded, toggle the download off on both your phone and computer and re-enable the download. The only downside is that people who follow your playlist won’t be able to hear these songs, but you now have these songs on both your computer and phone.

--

--