13

我想知道有没有办法将最近上传的内容从 YouTube 频道自动嵌入到网站?我什至不知道从哪里开始。帮助?

4

4 回答 4

21

该脚本不再有效,这是一种简单的方法。

<iframe width="600" height="340" src="https://www.youtube.com/embed?listType=user_uploads&list=YOUR_CHANNEL_NAME_HERE" frameborder="0" allowfullscreen></iframe> 

要获取您的频道名称,请单击“我的频道”和“/user/”之后的文本行。

小提琴


编辑

您还可以使用以下内容嵌入播放列表:

<iframe width="600" height="340" src="https://www.youtube.com/embed/+lastest?list=PLAYLIST_ID" frameborder="0" allowfullscreen></iframe>
于 2015-05-06T23:54:01.303 回答
21

使用以下代码通过指定频道 ID 而不是频道名称来自动嵌入 YouTube 频道中的最新视频。

var channelID = "UC0xXUfNSFQN3qOmf_G_nT5w";
var reqURL = "https://www.youtube.com/feeds/videos.xml?channel_id=";
$.getJSON("https://api.rss2json.com/v1/api.json?rss_url=" + encodeURIComponent(reqURL)+channelID, function(data) {
   var link = data.items[0].link;
   var id = link.substr(link.indexOf("=")+1);
$("#youtube_video").attr("src","https://youtube.com/embed/"+id + "?controls=0&showinfo=0&rel=0");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id="youtube_video" width="600" height="340" frameborder="0" allowfullscreen></iframe>

来源:https ://codegena.com/auto-embed-latest-video-youtube-channel/

于 2017-07-27T06:18:01.973 回答
10
<!DOCTYPE html>
<html>
<head>
    <title>YouTube Recent Upload Thing</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<div id="static_video"></div>
    <script type="text/javascript">
        function showVideo(response) {
            if(response.data && response.data.items) {
                var items = response.data.items;
                if(items.length>0) {
                    var item = items[0];
                    var videoid = "http://www.youtube.com/embed/"+item.id;
                    console.log("Latest ID: '"+videoid+"'");
                    var video = "<iframe width='420' height='315' src='"+videoid+"' frameborder='0' allowfullscreen></iframe>"; 
                    $('#static_video').html(video);
                }
            }
        }
    </script>
    <script type="text/javascript" src="https://gdata.youtube.com/feeds/api/users/urusernamehere/uploads?max-results=1&orderby=published&v=2&alt=jsonc&callback=showVideo"></script>
</body>
</html>
于 2013-08-16T07:29:20.093 回答
0

纯JS解决方案

const frame = document.getElementById("ytEmbed");
const channelID = "[CHANNEL ID HERE]";
var guid="";
            
fetch("https://api.rss2json.com/v1/api.json?rss_url=" + encodeURIComponent("https://www.youtube.com/feeds/videos.xml?channel_id=" + channelID))
    .then(response => response.json())
    .then(data => {
        guid = data["items"][0]["guid"]
        const embedURL = "https://youtube.com/embed/" + guid.replace("yt:video:", "")
        frame.src = embedURL;
    })
    .catch(console.error);
于 2022-03-04T10:22:31.093 回答