0

So I'm making a DIY Youtube Shuffler because apparently Youtube can't shuffle my playlists properly. I have no experience in HTMl or APIs or anything, but I stole enough code from here to make it work. But, it maxes out at 200 videos even though my playlist is a lot longer than that (around 2500). The code is below. I know this is a limitation with the Youtube API, but is there any workaround?

<html lang="en">
  <head>
    <title>YouTube Shuffle!</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="https://www.youtube.com/s/desktop/94d44772/img/favicon_144x144.png" sizes="144x144">
    <style>
      body, html { margin: 0; padding: 0; }
    </style>
  </head>  
  <body>
 <div id="player"></div>
    <script>
        // 2. This code loads the IFrame Player API code asynchronously.
        var tag = document.createElement('script');

        tag.src = "https://www.youtube.com/iframe_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

        // 3. This function creates an <iframe> (and YouTube player)
        //    after the API code downloads.
        function onYouTubeIframeAPIReady() {
            var numPl = Math.floor((Math.random() * 50) + 1);
            var player = new YT.Player("player", {
                height: '390',
                width: '640',
                playerVars: {
                    listType:'playlist',
                    list:'PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke',
                    index: numPl,
                    autoplay: 1,
    },
                events: {
                    'onReady': function (event) {
                        //event.target.cuePlaylist({list: "PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke"});
                        //event.target.playVideo();
                        setTimeout(function() {
                            event.target.setShuffle({'shufflePlaylist' : true});
                        }, 1000);
                    }
                }
            });
        }
    </script>
  </body>
</html>
  • I don't know any easy workaround (as the only one I think about is about using [YouTube Data API v3](https://developers.google.com/youtube/v3) [PlaylistItems: list](https://developers.google.com/youtube/v3/docs/playlistItems/list) endpoint and make your own _offline playlist_). – Benjamin Loison Sep 08 '22 at 21:03
  • I guess the 200 videos limit is related to the [pagination mechanism](https://developers.google.com/youtube/v3/guides/implementation/pagination) of playlists that YouTube UI also have (even if it seems on my end that the number of videos in a page is about 100 and not 200 by scrolling on https://www.youtube.com/playlist?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke). – Benjamin Loison Sep 08 '22 at 21:03
  • @BenjaminLoison Apologies if this sounds kind of stupid, but is it possible to concatenate the pages together? – Godly Avenger Sep 08 '22 at 21:17
  • I am not keen on the YouTube IFrame API so I don't know if you can easily adapt your code in such a way. However yes you are free to retrieve all the video ids of your playlist by using pagination of YouTube Data API v3 and then produce your own JavaScript playlist player (because generating your own shuffled playlist everytime you want to listen to your playlist would cost far too much in quota as [PlaylistItems: insert](https://developers.google.com/youtube/v3/docs/playlistItems/insert) costs 100 quota for each video). – Benjamin Loison Sep 08 '22 at 21:33
  • Even if the YouTube UI endpoint is free and is able to add all your video ids in a single shot cf https://stackoverflow.com/a/71970181/7123660 – Benjamin Loison Sep 08 '22 at 21:34
  • @BenjaminLoison So it would be easier (and cheaper) to grab all the IDs (using the Data API), put them in an array, shuffle the array, and then play the videos from the IDs? – Godly Avenger Sep 08 '22 at 21:43
  • I would do so, but as I said "play the videos from the IDs" doesn't seem trivial as generating this shuffled playlist with YouTube Data API v3 costs a lot of quota. There is the possibility to do it in a quota free manner but still doesn't seem very clean. The most clean approach would be to code a playlist mechanism using JavaScript that is able to switch from a video to the other thanks to the shuffled video array. – Benjamin Loison Sep 08 '22 at 21:45
  • @BenjaminLoison If I figure out a way to do that (which seems very difficult lol), would I still be able to use media keys to skip through the videos? It's a pretty important feature to me. – Godly Avenger Sep 08 '22 at 22:06
  • As far as using media keys is possible by YouTube, it is possible for everyone I would say. – Benjamin Loison Sep 08 '22 at 22:11
  • I have found good Youtube shufflers on Github and whatnot, but they just...don't work with the skip button, hence why I'm building it myself. – Godly Avenger Sep 08 '22 at 22:20

1 Answers1

0

NOTE: The 200 limit YouTube has is - as far as I know - undocumented, but, it exists. If you really need to retrieve all the videos on a playlist, you have to make the requests using YouTube Data API and get every video on the said playlist, then, you have to use more code for create your own video player.

If you're fine with YouTube's generated shuffled playlist, then, change these lines from:

setTimeout(function() {
    event.target.setShuffle({'shufflePlaylist' : true});
}, 1000);

To:

event.target.setShuffle(true);

With this change, the API will return the playlist shuffled - again, only the 200 shuffled videos.


Full code:

// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
function onYouTubeIframeAPIReady() {
  var numPl = Math.floor((Math.random() * 50) + 1);
  var player = new YT.Player("player", {
    height: '390',
    width: '640',
    playerVars: {
      listType: 'playlist',
      list: 'PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke',
      index: numPl,
      autoplay: 1,
    },
    muted: 1,
    events: {
      'onReady': function(event) {
        // Change made here: 
        event.target.setShuffle(true);
      }
    }
  });
}
<div id="player"></div>
<!--

First video returned: 
https://youtu.be/UE2vC5v8xLE?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 101

Next video: 
https://youtu.be/XCyKJD6uQyg?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 110

Next video: 
https://youtu.be/tOKdQ0-2ky4?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 26

Next video: 
https://youtu.be/MeljgyLR2D0?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 21

↑ You see there the playlist returned the playlist shuffled.

https://youtu.be/a0w-UJkTVFU?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
> Video no disponible at my country - the player stops and you have to reload the page for reload the shuffled playlist (also, you can search how to skip non-aavilable videos on a YouTube playlist).

https://youtu.be/1FiK9AJHk3k?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 19

https://youtu.be/gY_sEitPgpM?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 173

https://youtu.be/ZzZ1qmXZBuY?list=PL0mcz16a5pG0evLcpesj7g7CcFpwRhfke
posición: 136

-->