Grabbing the current viewer count for youtube live streaming

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).


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

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.

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

UPDATE 1:


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:

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

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>

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"... 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.

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.

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.

Leave a Comment