gpt4 book ai didi

javascript - 如何嵌入带有永久 url 的 Youtube 实时聊天?

转载 作者:可可西里 更新时间:2023-10-31 22:13:48 32 4
gpt4 key购买 nike

channel 直播的嵌入 URL 是:

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

它有效,但如果我想在它附近嵌入一个 YouTube 实时聊天以进行当前流式传输,我用于嵌入的 URL 是:

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

问题是这样的:对于每个新的直播流,视频 ID 都会发生变化。这样嵌入的代码就不再有效,下一次流媒体不会显示聊天。我想要一个对我所有的 YouTube 流媒体都有效的永久 URL 实时聊天无需每次手动更改视频 ID。如何解决?也许使用 PHP 或 javascript 脚本读取当前 YouTube URL 并替换聊天嵌入 URL 中的视频 ID?谢谢

最佳答案

您可以像这样使用 PHP 获取视频 ID:

<?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;
}

关于javascript - 如何嵌入带有永久 url 的 Youtube 实时聊天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44354421/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com