Search code examples
videoyoutubeplaylist

Is there a fix for skipping a YouTube video in a playlist, if video is "unavailable"


I have an aggregated list (queried from mySql db) of YouTube videos in an HTML 5 media player playlist. But over time, YouTube will disable certain videos due to copyright or judgement issues, but the links are still in my list. Can anyone recommend a JS or other solution or article that sees if the video link does not start in x amount of time to initiate a skip or next action. Please advise.

No solutions, because extensive googling has no suggestions.

my basic logic is if video does not play after x seconds, then skip, otherwise play.

// THIS ACTUALLY CHECKS PLAYTIME AND ADD TO A COUNTER - CAN I USE SOMETHING SIMILAR?

var counter = 0;
var currentIndex_inc = 0;
function onProgress() {

if(player.currentTime() <= 1){
    counter = 0;
}

//-- ------------------------------------- -->
// ----- COUNTER - If track plays longer than 30 seconds - add 1 --------
//-- ------------------------------------- -->
if(player.currentTime() >= 30  && trackURL != ''){
    if(counter==0){
    counter = 1;
    var playlist_name = "<?php echo $playlist ; ?>";
    var play_type = "<?php echo $type ; ?>";
    var trackURL = player.currentSrc();
    track_source = trackURL.src ;

        if(typeof(track_source)==="undefined"){
         track_source = trackURL;
        };

    $.ajax({
        type: "POST",
        url: "_inc/2018_process_counter.php",
        dataType: "text",
            data: { 
                playlist_name: playlist_name,track_source:track_source }
    }).done(function( data ) {
});
    }
    return false;
}

Logic:
If (video link does not start || video link == live){ 
   skip 
} else if (video link does start || video link == dead) {
   play 
}

List Code with Queru - based on successful reply. The answer works for a single id but not a list... See my code below:

if ($result_a->num_rows > 0) {
        // output data of each row
        while($row = $result_a->fetch_assoc()) {
            $id = $row['id'];
            $share_key = $row['share_key'];
            echo $row['id'];
            echo '<br>';
            echo $row['artist'];
            echo '<br>';
            echo $row['title'];
            echo '<br>';
            echo $row['source_url'];
            echo '<br>';            
            $my_link = $row['source_url'];

            $testlink = substr($my_link, strrpos($my_link, '/' )+1)."\n";

            echo '<p style="color:#ff0000">';
            echo $testlink;
            echo '</p>';            

            //# is ERROR = https://www.youtube.com/watch?v=R5mpcDWpYSA
            // $url = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=R5mpcDWpYSA"; //# test video deleted.

            //# is OK = https://www.youtube.com/watch?v=mLuh_O4mYbA
            $url = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=".$testlink; //# test working (not deleted).
            echo $url;
            echo '<br>';
            try
            {
                set_error_handler(function() { /* # temp ignore Warnings/Errors */ });

                $fop = fopen($url, "rb");
                if ( !$fop && $fop==false) { throw new Exception(); }

                restore_error_handler(); //# restore Warnings/Errors

                echo "OK 200 ::: Youtube video was found";
            }
            catch ( Exception $e ) 
            { echo "Error 404 ::: Youtube video not found (deleted or bad link)"; }




            echo '<hr>';









        }
    } else {
        // echo "0 results";
    }

Solution

  • "...YouTube will disable certain videos due to copyright or judgement issues, but the links are still in my list. Can anyone recommend a JS or other solution or article that sees if the video link does not start in x amount of time to initiate a skip or next action. Please advise."

    Since you're already involving PHP code, then one possible option is these steps:

    1) Make a request to https://www.youtube.com/oembed? + Youtube video URL.

    Example request:

    https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=R5mpcDWpYSA
    

    2) Use fopen to check video availability. Note a file_exists($url) will not work correctly with Youtube servers (they always return some page content, even if video itself has been removed).

    Testable example code below :
    (will echo "OK 200" or "ERROR 404", depending on video status...)

    <?php
    
        //# is ERROR = https://www.youtube.com/watch?v=R5mpcDWpYSA
        $url = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=R5mpcDWpYSA"; //# test video deleted.
    
        //# is OK = https://www.youtube.com/watch?v=mLuh_O4mYbA
        //$url = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=mLuh_O4mYbA"; //# test working (not deleted).
    
        try
        {
            set_error_handler(function() { /* # temp ignore Warnings/Errors */ });
    
            $fop = fopen($url, "rb");
            if ( !$fop && $fop==false) { throw new Exception(); }
    
            restore_error_handler(); //# restore Warnings/Errors
    
            echo "OK 200 ::: Youtube video was found";
        }
        catch ( Exception $e ) 
        { echo "Error 404 ::: Youtube video not found (deleted or bad link)"; }
    
    ?>
    


    Option 2:

    You can also achieve same result by using file_get_contents with a request to Youtube's get_video_info?.

    Example request :

    https://www.youtube.com/get_video_info?video_id=R5mpcDWpYSA
    

    Example code :

    <?php
    
        //# ERROR = https://www.youtube.com/watch?v=R5mpcDWpYSA
        $url = "https://www.youtube.com/get_video_info?video_id=R5mpcDWpYSA"; //# test video deleted.
    
        //# OK = https://www.youtube.com/watch?v=mLuh_O4mYbA
        //$url = "https://www.youtube.com/get_video_info?video_id=mLuh_O4mYbA"; //# test working (not deleted).
    
        $src = file_get_contents($url);
    
        //# find text... playabilityStatus%22%3A%7B%22status%22%3A%22OK ...
        $str1 = "playabilityStatus%22%3A%7B%22status%22%3A%22";
        $pos = strpos($src, $str1);
    
        $result = substr( $src, $pos + (strlen($str1)), 5);
    
        if( $result{0} == "O" && $result{1} == "K" )
        { echo "OK 200 ::: Youtube video was found"; }
        else 
        { echo "Error 404 ::: Youtube video not found (deleted or bad link)"; }
    
    ?>