pdchaudhary
pdchaudhary

Reputation: 388

how to check video in youtube is live or uploaded using youtube iframe api

I have 'Youtube' video link.

So how to get this video is live or uploaded using 'Youtube api' or 'Youtube iframe api' using video id or 'Youtube' video link?

Upvotes: 0

Views: 754

Answers (2)

pdchaudhary
pdchaudhary

Reputation: 388

<script>
$.getJSON('https://www.googleapis.com/youtube/v3/videos?id=F5xx0NDcO7s&part=contentDetails&key={api key}', function(data) {

    var data1 = data["items"][0]["contentDetails"]["duration"];
    var data2 = data1.substring(2);
    var data3 = data2.substring(0, data2.length - 1);
    console.log(data3);

    if (data3.includes('M')) {
        var array = data3.split("M");
        if (array[0].includes('H')) {
            var hours = array[0].split("H").map(Number);
            console.log(array);

            var second = hours[0] * 60 * 60 + hours[1] * 60 + array[1];
        } else {
            var second = array[0] * 60 + array[1];
        }
    } else {
        var second = data3;
    }
    if (second == 0) {
        console.log("video is live or not uploded");
    } else {
        console.log("video uploded");
    }
});

This is my solution to check live or not using video duration

Upvotes: 1

Muhammad Osama
Muhammad Osama

Reputation: 1047

You can use Youtube oEmbed format for this. Here's a simple function

    function yt_exists($videoID) {
    $theURL = "http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=".$videoID."&format=json";
    $headers = get_headers($theURL);

    return (substr($headers[0], 9, 3) !== "404");
    }

$id = 'yyDUC1LUXSU'; //Video ID

if (yt_exists($id)) {
    //  Video is working and uploaded
} else {
    //  Video is not uploaded or live
}

Upvotes: 0

Related Questions