thelevicole / youtube-to-html5-loader

Load YouTube videos with the HTLML5 <video> element without needing iframes or the YouTube JS API.
https://thelevicole.com/youtube-to-html5-loader/
138 stars 25 forks source link

Video request returns 404/403 #13

Closed anast3t closed 3 years ago

anast3t commented 3 years ago

When loading player, there is extremly high chance to get 404 or 403 on responce (even on example page). As temporary solution i just filter them in loop, but maybe it can be fixed. image

(code example, maybe i'm just doing something wrong)

let gotFlag = false
while (!gotFlag) {
      console.log("trying...")
      let player = new yt2html({
          withAudio: true,
          autoload: false,
      })
      player.addFilter('video.source', function (source, data, element, format, streams) {
          gotFlag = true
          if (Array.isArray(streams) && streams.length > 1) {
              const options = streams;
              options.sort(function (a, b) {
                  const aLabel = parseInt(a.label);
                  const bLabel = parseInt(b.label);

                  if (aLabel < bLabel) {
                      return -1;
                  }

                  if (aLabel > bLabel) {
                      return 1;
                  }

                  return 0;
              });

              for (let index = 0; index < options.length; index++) {
                  const stream = options[index];
                  const option = document.createElement('option');
                  option.value = stream.url;
                  option.text = `${stream.label}`;
                  if (stream.url === source) {
                      option.setAttribute('selected', 'selected');
                  }
                  select.appendChild(option);
              }
              select.disabled = false;
              select.addEventListener('change', function () {
                  element.src = this.value;
              });
          } else {
              select.style.display = 'none';
          }
          return source;
      });
      player.load()
      await this.delay(500)
      if (gotFlag)
          console.log("success")
      else
          console.log("fail")
}

delay:

delay(delay) {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve(2);
        }, delay);
    });
},
anast3t commented 3 years ago

To avoid 403 you need referer field in request header. So you need to send request from server (eg localhost). But now the problem is that google fixed something and request returns constant 404

grcmichael commented 3 years ago

My videos don't load since yesterday and I noticed the same issue on my console : GET https://images21-focus-opensocial.googleusercontent.com/gadgets/proxy?container=none&url=https%3A%2F%2Fwww.youtube.com%2Fget_video_info%3Fvideo_id%3DYizPn42SO6o%26el%3Dembedded%26hl%3Den_US 404 :(

sebiko3 commented 3 years ago

Guess YT updated due to Copyright and such ...

grcmichael commented 3 years ago

So we can't use this lib anymore ?

sebiko3 commented 3 years ago

So we can't use this lib anymore ?

There could be some fix, maybe. However using YT Content in a Player Other than YT Player surely isn't intended by YT and maybe against YT TOS as well. If you rely on it working stable I suggest to use the js api.

anast3t commented 3 years ago

This CAN be fixed because there are still websites from which you can download (like savefrom.net, on main page you can find nonYT player)

anast3t commented 3 years ago

I found what you can use instead of this lib for now: ytdl-core. It returns exactly same data-streaming urls. Maybe i'll try to fix this lib with the source code of that :)

example of usage (vue): image image

thelevicole commented 3 years ago

I've just published version 4.0.1 which contains a quick fix for this issue.

Instead of making responses directly to the YouTube/Google endpoints, I have setup a middleware API (https://yt2html5.com/) to handle the retrieval of video information. It's currently on a shared server so please do not expect rapid responses.

Over the next couple of weeks I will be testing and improving the library and API so keep an eye out for future releases.

Let me know how you get on.