Skip to content

Instantly share code, notes, and snippets.

@senecaso
Created May 5, 2023 23:42
Show Gist options
  • Save senecaso/784e740f343f12ccc0b4a896f59a5f03 to your computer and use it in GitHub Desktop.
Save senecaso/784e740f343f12ccc0b4a896f59a5f03 to your computer and use it in GitHub Desktop.
YouTube Music: Copy liked music to a playlist

I was looking for a way to copy my "likes" in YouTube music over to a playlist, in the hopes of stopping YTM from losing songs I have liked. I found this script, but it didn't work for me, so I had to look for other solutions. In the end, this is what worked for me.

It's a small Python script that uses the YTMusic python library to copy tracks from my likes to a new playlist. I will try to outline the steps to run the script, but this will assume some familiarity with Python.

Hopefully this will work for others as well!

from ytmusicapi import YTMusic

yt = YTMusic('oauth.json')
playlistId = yt.create_playlist('My Playlist', "Some Description - leave blank if you want need one")
songs = yt.get_liked_songs(5000) # get 5000 liked songs, this is the maximum playlist size
videoIds = map(lambda x: x['videoId'], songs['tracks']) # convert the tracks into a list of "videoId"s
videoIds = [*set(list(videoIds))] # remove duplicate videoIds
yt.add_playlist_items(playlistId, videoIds = videoIds, duplicates = False) # add all of the videoIds to the new playlist

Steps to Run

  1. cd /tmp
  2. copy the script above into /tmp/copy_likes.py
  3. pip install ytmusicapi
  4. ytmusicapi oauth - follow the prompts, copy the URL to a browser and authenticate with your Google account to grant access
  5. python /tmp/copy_likes.py
  6. rm oauth.json - this file grants access to your YTM account, so be sure to nuke it when you are finished
@Ishida0216
Copy link

are you the Top G?

@DeamonDan
Copy link

DeamonDan commented Jun 24, 2023

it does the work but songs gets shuffled in the created playlist.

To get it as it is:
https://gist.github.com/Redbyte1/1e88e0bfbe97584792b7fbb89207b5fe
use this
use oauth.json instead of headers_auth.json
and increase the liked song counter from 500 to your desires num

@Ishida0216
Copy link

naaah man, im fine with the original script. again bro ur the best!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment