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
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?
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With