抢占YouTube实时流的当前观看者数量 [英] Grabbing the current viewer count for youtube live streaming

查看:122
本文介绍了抢占YouTube实时流的当前观看者数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望显示youtube实时流的当前观看者人数,我可以使用以下代码对youtube视频执行此操作:

I am looking to display the current viewer count for youtube live streams, i can do this for youtube video with this code :

<?php
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?
part=statistics&id=videiID&key=APIKey");
$json_data = json_decode($JSON, true);
echo $json_data['items'][0]['statistics']['viewCount'];
?>

但这不会显示当前的观看人数!

but this not display the current viewers count !

推荐答案

更新2: 不幸的是,YouTube放弃了对该方法的支持.希望将来会有一个不使用其API的新解决方案(因为它有报价限制).

UPDATE 2: YouTube has unfortunately dropped support for this method. Hopefully there will be a new solution in the future without using their API (since it has quote limits).

尝试以下链接: https://www.youtube.com/live_stats?v= {videoid}

Try this link: https://www.youtube.com/live_stats?v={videoid}

在YouTube上找到一个直播活动,并替换链接中的视频ID.这将检索该特定视频的并发观看次数.

Find a Live event on YouTube and replace the video id in the link. This should retrieve the number of concurrent views for that particular video.

如果是,请刷新页面,然后将其与YouTube页面上的并发观众进行比较.

If so, refresh the page and compare the number with the concurrent viewers on the YouTube page.

更新1:

这里是如何将其添加到网站的示例.您可以设置一个php文件和一个html文件.

Here is an example of how you can add it to your website. You can set up a php file and a html file.

viewers.php:

viewers.php:

<?php
$viewers = file_get_contents('https://www.youtube.com/live_stats?v=y60wDzZt8yg');
echo $viewers;
?>

viewers.html:

viewers.html:

<!DOCTYPE html>
<html>
<body>
<div id="status" style="color: #666; line-height: 24px; font-size: 19px; font-weight: normal; font: 19px Roboto,arial,sans-serif;"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function update() {
    $.ajax({
        url: 'viewers.php',
        dataType: 'text',
        success: function(data) {
            if (parseInt(data) == 0) {
                $("#status").css({ display: "none" });
            } else {
                $("#status").text(parseInt(data) + ' watching now' );
            }
        }
    })
}
update();
var statusIntervalId = window.setInterval(update, 5000);
</script>
</body>
</html>

如您所见,我使用PHP文件通过live_stats链接从YouTube检索数据.然后,我将HTML与jquery一起使用,每5秒动态更新一次数据.

As you can see, I use the PHP file to retrieve the data from YouTube using the live_stats link. I then use the HTML with jquery to update the data dynamically every 5 seconds.

<div id="status"...是显示数据并用一点CSS设置样式的位置.
<script src="...是jQuery库的加载位置.
<script type="...是代码功能,其中jQuery从PHP文件中检索数据并将其显示在第一个div中./如果有0个查看器,则将其隐藏.

<div id="status"... is where the data is shown and styled with a bit of css.
<script src="... is where the jQuery library is loaded.
<script type="... is the code function where jQuery retrieves data from the PHP file and displays it in the first div / hides it if there are 0 viewers.

我当时想您可以将其嵌入到iframe中,这样不会刷新整个页面.您也可以直接将html代码嵌入网页中,而无需使用iframe.如果直接嵌入,则如果PHP文件不在同一相对文件夹中,则必须设置该PHP文件的直接路径.如果有任何错误,只需使用开发人员工具控制台进行故障排除.

I was thinking that you can embed this inside an iframe which won't refresh the entire page. You can also just embed the html code into the webpage directly without an iframe. If you will embed directly, you will have to set a direct path to the PHP file if it's not in the same relative folder. If there are any errors, just troubleshoot with the developer tools console.

请注意,在第一个jQuery间隔刷新数据之前,不会显示查看者的数量.在这种情况下,需要5秒钟.我已对此进行了更新,因此首次更新不需要5秒钟.现在应该可以正常工作了.

Just a heads up, the number of viewers won't show until the first jQuery interval refreshes the data. In this case, 5 seconds. I updated this so it wouldn't take 5 seconds to first update. It should work right away now.

这是它为我工作的图像:

Here is an image of it working for me:

希望这对您有帮助...

Hope this helps...

这篇关于抢占YouTube实时流的当前观看者数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆