-
Set Up Spotify API Credentials
To access your Spotify playlists, you'll need to create a Spotify app to obtain the necessary credentials.
a. Create a Spotify App
- Visit the Spotify Developer Dashboard and log in with your Spotify account.
- Click on "Create an App".
- Fill in the required details and accept the terms.
- Once the app is created, you'll see your Client ID and Client Secret.
b. Set the Redirect URI
- In your app's settings, click on "Edit Settings".
- Under "Redirect URIs", add a URI (e.g.,
http://localhost:8888/callback
). - Save the changes.
c. Update the Script with Your Credentials
Replace the placeholders in the script with your actual credentials:
SPOTIFY_CLIENT_ID = 'YOUR_SPOTIFY_CLIENT_ID' SPOTIFY_CLIENT_SECRET = 'YOUR_SPOTIFY_CLIENT_SECRET' SPOTIFY_REDIRECT_URI = 'YOUR_SPOTIFY_REDIRECT_URI' # e.g., 'http://localhost:8888/callback'
-
Obtain Your Spotify Playlist ID
-
Open Spotify and navigate to the playlist you want to convert.
-
Click on the three dots (...) next to the playlist name.
-
Select "Share" > "Copy Link to Playlist".
-
The link will look like
https://open.spotify.com/playlist/PLAYLIST_ID?...
. -
Extract the
PLAYLIST_ID
from the URL and replace it in the script:SPOTIFY_PLAYLIST_ID = 'YOUR_SPOTIFY_PLAYLIST_ID'
-
-
Customize Playlist Details
In the script, set your desired YouTube Music playlist name and privacy status:
playlist_name = 'Your Playlist Name' # Replace with your desired name privacy_status = 'PRIVATE' # Options: 'PRIVATE', 'PUBLIC', 'UNLISTED'
-
Install Required Libraries
Make sure you have both
spotipy
andytmusicapi
installed:pip install spotipy ytmusicapi
-
Run the Script
Now you're ready to run the script:
python your_script_name.py
-
Spotify Authentication:
- Uses
spotipy.SpotifyOAuth
to authenticate with Spotify. - Requires
client_id
,client_secret
, andredirect_uri
.
- Uses
-
Retrieve Spotify Playlist Tracks:
- The function
get_spotify_playlist_tracks
fetches all tracks from the specified Spotify playlist. - Handles pagination to retrieve playlists with more than 100 tracks.
- The function
-
Initialize YouTube Music API:
- Creates an instance of
YTMusic
for API calls usingOauth
.
- Creates an instance of
-
Create YouTube Music Playlist:
- Creates a new playlist on YouTube Music with the specified name and privacy status.
-
Search and Add Tracks:
- Iterates over each track from the Spotify playlist.
- Searches YouTube Music for the track using the song name and artist(s).
- Adds the best matching track to the YouTube Music playlist.
- Includes a
time.sleep(1)
call to respect rate limits.
-
Error Handling:
- If a track isn't found on YouTube Music, it prints a message and continues.
- Privacy: Keep your authentication information secure, it is sensitive information.