15

I was wondering is there was a way to Auto Embed recent uploads from a YouTube channel to a website? I don't even know where to start. Help?

Damjan Pavlica
  • 31,277
  • 10
  • 71
  • 76
CHASE
  • 501
  • 1
  • 7
  • 21

5 Answers5

23

Use the following code to auto-embed the latest video from a YouTube channel by specifying the channel ID instead of the channel name.

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>

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

Shan Eapen Koshy
  • 2,909
  • 1
  • 28
  • 40
  • 2
    As of Jan 2019, I have confirmed, this is indeed working! First time I found something that pulls in things properly and not just a live stream! – petrosmm Jan 11 '19 at 14:46
  • I was having loads of issues with the YouTube Data API authorisation and this works perfectly for my needs. Excellent solution. – ceindeg Jul 23 '19 at 12:24
  • This is still working in June 2021. Nice! – Reanimation Jun 10 '21 at 17:54
20

This script no longer works, heres an easy way to do it.

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

To get your channel name click "My Channel" and its the line of text after "/user/".

Fiddle


Edit

You can also embed playlists with this:

<iframe width="600" height="340" src="https://www.youtube.com/embed/+lastest?list=PLAYLIST_ID" frameborder="0" allowfullscreen></iframe>
Community
  • 1
  • 1
Shixma
  • 425
  • 1
  • 5
  • 17
  • I can't get this to work with my channel. I don't have a channel username - it's a string of alphanumeric characters and looks like this: 'channel/etcetc' – JCraine Mar 03 '17 at 15:29
  • @JCraine Check my answer for channel ID support – Shan Eapen Koshy Jul 27 '17 at 06:22
  • FYI to people finding this - If you are trying the fiddle or first option and it doesn't load, you might need to use https instead of http – Wayneio Jan 15 '20 at 09:28
  • 1
    Thanks! the playlist ID worked for me. You can find the list ID in the URL of the playlist after list= – Chad Wimberly Sep 03 '20 at 00:22
  • 1
    I tried both solutions. The first one doesn't work anymore and the second one shows me the first video from the playlist but not the latest. :( – Benny Code May 04 '22 at 20:43
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>
CHASE
  • 501
  • 1
  • 7
  • 21
  • Do you know how to auto embed recnt video from playlist? – Manos Sep 30 '14 at 14:15
  • 5
    This has stopped working now. YouTube tells me they've deprecated this API, is there a quick fix to make this work with the new API version? – thephpdev Apr 30 '15 at 08:58
4

In 2022, embedding the latest video from a YouTube channel is quite complicated, but I found a solution that works for personal accounts and brand accounts:

  1. Login to YouTube and open the advanced settings
  2. Copy your channel ID (it will start with "UC" and then some cryptic characters, e.g. "UCYCJ5V6CYFhUr7AhlKaYL4A")
  3. Replace the beginning "UC" with "UU" ("UCYCJ5V6CYFhUr7AhlKaYL4A" becomes "UUYCJ5V6CYFhUr7AhlKaYL4A")
  4. Use the following embed code (using your ID that starts with "UU"):

Embed Code:

<iframe
  src="https://www.youtube-nocookie.com/embed?listType=playlist&list=UUYCJ5V6CYFhUr7AhlKaYL4A"
  width="600"
  height="340"
  allowfullscreen>
</iframe>

Alternative solution for personal accounts:

When you are having a personal YouTube channel that is connected to your Google Mail address, then you can use your Google Mail username to embed your latest upload.

Example: When your email is username@googlemail.com, then you can embed your latest video upload with this code:

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

Some say that it also works with your YouTube channel ID or YouTube channel name but this didn't work for me.

Reference: YouTube IFrame Player API

Benny Code
  • 51,456
  • 28
  • 233
  • 198
0

Pure JS solution

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);
jbritain
  • 94
  • 4