1

How can someone embed a YouTube video into a website in a way that will automatically redirect to a locally stored copy of the video if the video becomes unavailable from YouTube?

For example, I would like to do something like this:

enter image description here

Polyhat
  • 201
  • 2
  • 9

1 Answers1

0

Using the answer posted here, by using jQuery, you can first check if YouTube is reachable by the client and then if it is not reachable, load the video from an alternative source. You can use the following piece of code:

$.ajax({url: "https://www.youtube.com",
    type: "HEAD",
    timeout:1000,
    statusCode: {
        200: function (response) { //YouTube is reachable.
            $("#iframeid").attr("src", "https://www.youtube.com/watch?v=videoid");
        },
        400: function (response) { //YouTube is unreachable.
            $("#iframeid").attr("src", "https://www.example.com/videos/video.mp4");
        } 
    }
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
MBiabanpour
  • 370
  • 1
  • 13
  • Will that only apply to the YouTube servers being online though? What if YouTube is working but the specific video requested is unavailable? – Polyhat May 20 '21 at 06:08
  • No, @Polyhat, actually the piece of code above checks if YouTube.com is reachable (and it can be replaced with any other server address you want), and if it is reachable, then it will let the iFrame source be YouTube. If you want to check if the second source is available or not, then you can again put another Ajax HTTP request within the section that specifies the action to do when YouTube is unreachable. – MBiabanpour May 20 '21 at 06:11