Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed Youtube live chat with url permanent?

The embed URL for a channel's live stream is:

https://www.youtube.com/embed/live_stream?channel=CHANNEL_ID

and it works but if I want to embed near at it a YouTube live chat for current streaming the URL that I use for the embed is:

https://www.youtube.com/live_chat?v=VIDEOID&embed_domain=DOMAINURL 

The problem is this: for every new live stream the Video ID changes. So that the embedded code isn't valid anymore and chat isn't displayed for next streaming.I want a permanent URL live chat valid for all my YouTube streaming without change video id manually every time. How to resolve? Perhaps with a script in PHP or javascript that read current YouTube URL and replace video id in chat embed URL? thanks

like image 966
grigione Avatar asked Jun 04 '17 13:06

grigione


People also ask

Can YouTube live chat be embedded?

During a live stream, you can embed live chat on your own site by using an iframe. Note: embedding live chat isn't available on mobile web. Get the video ID for the live stream. You can get the video ID from the watch page URL (youtube.com/watch?

How do you get a permanent YouTube link?

Go to youtube.com/account_advanced. Copy your Channel ID shown below. Now, type in youtube.com/channel/PASTE YOUR CHANNEL ID HERE/live. Your permanent YouTube Live Page should look like this: www.YouTube.com/channel/UC7qJ6z7MVPtqP8DXv0mXGaA.


1 Answers

You could get the video ID using PHP like this:

<?php

try {
    $videoId = getLiveVideoID('CHANNEL_ID');

    // Output the Chat URL
    echo "The Chat URL is https://www.youtube.com/live_chat?v=".$videoId;
} catch(Exception $e) {
    // Echo the generated error
    echo "ERROR: ".$e->getMessage();
}

// The method which finds the video ID
function getLiveVideoID($channelId)
{
    $videoId = null;

    // Fetch the livestream page
    if($data = file_get_contents('https://www.youtube.com/embed/live_stream?channel='.$channelId))
    {
        // Find the video ID in there
        if(preg_match('/\'VIDEO_ID\': \"(.*?)\"/', $data, $matches))
            $videoId = $matches[1];
        else
            throw new Exception('Couldn\'t find video ID');
    }
    else
        throw new Exception('Couldn\'t fetch data');

    return $videoId;
}
like image 191
RoelVB Avatar answered Sep 28 '22 03:09

RoelVB