This Python script allows you to generate a YouTube playlist containing music videos of songs from a Spotify playlist. The script uses the Spotify and YouTube APIs for retrieving track data and searching for music videos on YouTube.
.env file. When using this code or forking the repository, exercise caution and ensure that you protect your sensitive data properly.
Always follow best practices when handling sensitive information. Do not share your credentials publicly or commit them to version control. Use appropriate encryption techniques and secure storage methods for handling sensitive data in production environments.
Please be responsible with your credentials and follow good security practices to keep your data safe.
Please note the following limitations of the current implementation:
-
Long Spotify Playlists: The script may not work efficiently for very long Spotify playlists, as it relies on querying the Spotify API for each track individually and adding each track to a Youtube playlist individually. Spotify and Youtube APIs have rate and usage limitations that could affect the performance for large playlists.
-
Unfunded Developer Accounts: If you are using an unfunded Spotify and/or Youtube developer account, you might encounter rate/ usage limitations and restrictions on certain API endpoints, which could impact the functionality of the script. I maxed out my daily Youtube quota 3 times while writing this code.
In the future, I plan to enhance the code to address these limitations and improve the overall user experience. One potential improvement is to implement progress tracking and allow the script to build the YouTube playlist over multiple days, accommodating the quota limitations enforced by YouTube's API.
Feel free to contribute to the project and suggest additional improvements!
Before running the script, make sure you have the following:
- Python 3.x installed on your system
- A Spotify Developer Account to obtain the Spotify Client ID and Client Secret
- A Google Developer Account to set up OAuth 2.0 for YouTube and get the YouTube API Key
- Clone the repository:
git clone https://github.com/your-username/spotify-to-youtube-playlist.git- Change to the project directory:
cd spotify-to-youtube-playlist- Create a virtual environment (optional, but recommended):
python3 -m venv venv- Activate the virtual environment:
- For macOS/Linux:
source venv/bin/activate- For Windows:
venv\Scripts\activate- Install the required Python packages:
pip install -r requirements.txtTo use the Spotify API, you'll need a Client ID and Client Secret. Here's how to obtain them:
- Visit the Spotify Developer Dashboard: https://developer.spotify.com/dashboard/applications
- Log in with your Spotify account (or create one if you don't have it).
- Click "Create an App" and follow the instructions to create a new Spotify App.
- Once the app is created, you'll find the Client ID and Client Secret on the App's dashboard.
To use the YouTube API, you'll need an API Key. Here's how to obtain it:
- Go to the Google Developer Console: https://console.developers.google.com/
- Create a new project or select an existing one.
- Click "Enable APIs and Services" and search for "YouTube Data API v3."
- Enable the API and create credentials for your project.
- Once the credentials are created, you'll find the API Key under "Credentials."
In order to access private YouTube playlists and perform write operations (creating playlists), you'll need to set up OAuth 2.0:
- In the Google Developer Console, navigate to the "Credentials" section.
- Click "Create Credentials" > "OAuth client ID."
- Select "Desktop app" as the application type.
- Save the generated client secrets JSON file as "client_secrets.json" in the project directory.
Create a file named .env in the project directory and add the following lines:
SPOTIFY_CLIENT_ID=your_spotify_client_id
SPOTIFY_CLIENT_SECRET=your_spotify_client_secret
YOUTUBE_API_KEY=your_youtube_api_key
Replace your_spotify_client_id, your_spotify_client_secret, and your_youtube_api_key with the credentials you obtained in the previous steps.
Run the script and provide the Spotify playlist URL when prompted:
python main.pyThe script will generate a YouTube playlist with music videos of the songs from the Spotify playlist.
The original intent of this project was to create a way to share your favorite Spotify playlists with older relatives who are perplexed by or too stubborn to create or login to a Spotify account. They'll enjoy listening to the music videos on YouTube without the hassle of signing up for a new platform. (You're welcome, mom!)
This turned out to be a really fun and fast project that touches on some key programming concepts in a non-threatening way. In fact, I kind of feel like it would have been a great homework assignment in high school or college. In that spirit, I've left a few exercises for the reader, if you'd like to play around with the code:
-
Mashup Machine: Tweak the search parameters to make a list of something other than music videos. Make a playlist of movie clips, TikTok compilations, or even cooking videos that are (however loosely) based on Spotify playlists.
-
Instant Karaoke Night: Tweak the search parameters to find lyrics videos and turn your Spotify playlist into a Youtube karaoke party playlist!
-
Kpop Bootcamp Generator: Tweak the search parameters to find dance tutorial videos for your favorite Kpop Spotify playlist so you can learn all the moves of your favorite idol.