代码之家  ›  专栏  ›  技术社区  ›  NetTemple

如果视频“不可用”,是否有跳过播放列表中YouTube视频的修复程序

  •  0
  • NetTemple  · 技术社区  · 5 年前

    没有解决方案,因为大量的谷歌搜索没有任何建议。

    我的基本逻辑是,如果x秒后视频不播放,则跳过,否则播放。

    // 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 
    }
    

    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";
        }
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   VC.One    5 年前

    “…由于版权或判断问题,YouTube将禁用某些视频,但链接仍在我的列表中。有没有人可以推荐一个JS或者其他的解决方案或者文章,看看视频链接是否在x个时间段内没有启动来启动一个跳过或者下一个动作。请告知。”

    既然已经涉及到PHP代码,那么一个可能的选择就是以下步骤:

    https://www.youtube.com/oembed? + Youtube video URL .

    请求示例:

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

    fopen 检查视频可用性。附注a file_exists($url) 在Youtube服务器上无法正常工作(它们总是返回一些页面内容,即使视频本身已被删除)。


    (将回音) OK 200 “或” ERROR 404 “,取决于视频状态…)

    <?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)"; }
    
    ?>
    


    方案2:

    file_get_contents 向Youtube的 get_video_info? .

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

    示例代码:

    <?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)"; }
    
    ?>