我在HTML 5媒体播放器播放列表中有一个YouTube视频的汇总列表(从mySql数据库查询)。但是随着时间的推移,由于版权或判断问题,YouTube会禁用某些视频,但这些链接仍在我的列表中。任何人都可以推荐JS或其他解决方案或文章,以查看视频链接是否在x时间内开始启动跳过或下一步操作。请告知。
没有解决方案,因为广泛的谷歌搜索没有建议。
我的基本逻辑是,如果视频在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
}
使用Queru列出代码-基于成功的答复。答案仅适用于单个ID,而不适用于列表...请参见下面的代码:
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 :(得分:1)
“ ...由于版权或判断问题,YouTube将禁用某些视频,但这些链接仍在我的列表中。任何人都可以推荐JS或其他解决方案或文章,以查看视频链接是否未启动在x时间内开始跳过或下一步操作。请提出建议。“
由于您已经在使用PHP代码,因此可能的选择是以下步骤:
1)向https://www.youtube.com/oembed?
+ Youtube video URL
发出请求。
示例请求:
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=R5mpcDWpYSA
2)使用fopen
检查视频可用性。请注意,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)"; }
?>