0

I want to print a list of songs (with artist, album, and duration) from my YouTube Music account.

There is no easy way to do this from the app. Doing print-screens as I page through a long list of songs is not tenable.

I would be happy with an export of data to a standard format (plain text, CSV, XML, etc.) that I can manipulate myself.

Any suggestions?

0

I do not recommend playing music from the same tab that you're doing this process in as some of the time it seemed to cause problems for me.

Instructions:

  1. Go to your Playlists page.
  2. Press F12 to open your console.
  3. Paste in the JavaScript code below into your console.
  4. Surprisingly important: Close your console. I've found that YouTube Music works wayyy slower with the console open for some reason.
  5. Click on a playlist that you want to save to text.
  6. If the playlist is longer than 100 songs, scroll to the bottom. Continue until the last batch is loaded (YouTube Music seems to load batches of 100 songs, so if there are 317 songs in your playlist you'll need to scroll to the bottom and wait for it to load 3 times).
  7. Once all songs have loaded, navigate back to the playlists page (same as in step 1.) using the menu or your browsers back button.
  8. Repeat steps 5-7 for all playlists you want to save to text.
  9. Once you've done this for all the playlists you want to save to text, open your console (F12) and either type JSON.stringify(tracklistObj, null, '\t') (change the '\t' to ' ' if you want minimal indentation) or tracklistObj if you just want the JavaScript object to manipulate it your own way. If you want it sorted, run the command Object.values(tracklistObj).forEach(a => a.sort()) before calling the JSON.stringify command.

Be careful to not refresh the page before you've completed all that you want to do or else you'll have to restart from step 1.

// Setup
var tracklistObj = {},
  currentPlaylist,
  checkIntervalTime = 100,
  lastURL;

// Process the visible tracks
function getVisibleTracks() {
  var entries = document.querySelectorAll('ytmusic-responsive-list-item-renderer.ytmusic-playlist-shelf-renderer');
  for (var i = 0; i < entries.length; i++) {
    var l = entries[i];
    var info = l.querySelectorAll("yt-formatted-string");

    var title = info[0].querySelector('a');
    if (title !== null)
      title = title.textContent;

    var artist = info[1].querySelector('a');
    if (artist !== null)
      artist = artist.textContent;

    var album = info[2].querySelector('a');
    if (album !== null)
      album = album.textContent;

    var duration = info[3].querySelector('a');
    if (duration !== null)
      duration = duration.textContent;


    // Add it if it doesn't exist already
    if (tracklistObj[currentPlaylist] && !tracklistObj[currentPlaylist].includes(artist + " - " + title)) {
      tracklistObj[currentPlaylist].push(artist + " - " + title);

      if (printTracksToConsole) {
        console.log(artist + ' - ' + title);
      }
    }
  }
}

function getTracks() {
  currentPlaylist = null;

  var doneLoading = setInterval(function () {
    var playListName = document.querySelector("#header .title");
    if (playListName != null) {
      clearInterval(doneLoading);

      currentPlaylist = playListName.innerText;
      if (tracklistObj[currentPlaylist] === undefined) {
        tracklistObj[currentPlaylist] = [];

        console.log("===================================");
        console.log("Adding to playlist " + currentPlaylist);
      }
    }
  }, 100);

}

// Check every so often for new playlists and/or tracks
setInterval(function() {
  if(window.location.href !== lastURL) {
    lastURL = window.location.href;
    getTracks();
  }

  if(lastURL !== "https://music.youtube.com/library/playlists") 
    getVisibleTracks();
}, checkIntervalTime);

// Whether or not to print the tracks obtained to the console
var printTracksToConsole = false;

You can also print out track names to the console as you go by changing printTracksToConsole to true (you should do this between steps 2 and 3).

Note that currently it's setup only to give Artist - Track name, but you can easily edit the line that has tracklistObj[currentPlaylist].push(artist + " - " + title); with album or duration, and/or whatever formatting you want (including CSV format if you so please). Do this before step 3.

Example output (all YouTube Music playlists I currently have) with default settings. It took about 5 minutes in total to navigate to each of the 52 playlists, scroll down them, and then convert the result to text.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.