YouTube API to fetch all videos on a channel

308,656

Solution 1

You need to look at the YouTube Data API. You will find there documentation about how the API can be accessed. You can also find client libraries.

You could also make the requests yourself. Here is an example URL that retrieves the latest videos from a channel:

https://www.googleapis.com/youtube/v3/search?key={your_key_here}&channelId={channel_id_here}&part=snippet,id&order=date&maxResults=20

After that you will receive a JSON with video ids and details, and you can construct your video URL like this:

http://www.youtube.com/watch?v={video_id_here}

Solution 2

First, you need to get the ID of the playlist that represents the uploads from the user/channel:

https://developers.google.com/youtube/v3/docs/channels/list#try-it

You can specify the username with the forUsername={username} param, or specify mine=true to get your own (you need to authenticate first). Include part=contentDetails to see the playlists.

GET https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=jambrose42&key={YOUR_API_KEY}

In the result "relatedPlaylists" will include "likes" and "uploads" playlists. Grab that "upload" playlist ID. Also note the "id" is your channelID for future reference.

Next, get a list of videos in that playlist:

https://developers.google.com/youtube/v3/docs/playlistItems/list#try-it

Just drop in the playlistId!

GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId=UUpRmvjdu3ixew5ahydZ67uA&key={YOUR_API_KEY}

Solution 3

Here is a video from Google Developers showing how to list all videos in a channel in v3 of the YouTube API.

There are two steps:

  1. Query Channels to get the "uploads" Id. eg https://www.googleapis.com/youtube/v3/channels?id={channel Id}&key={API key}&part=contentDetails

  2. Use this "uploads" Id to query PlaylistItems to get the list of videos. eg https://www.googleapis.com/youtube/v3/playlistItems?playlistId={"uploads" Id}&key={API key}&part=snippet&maxResults=50

Solution 4

To get channels list :

Get Channels list by forUserName:

https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails,statistics&forUsername=Apple&key=

Get channels list by channel id:

https://www.googleapis.com/youtube/v3/channels/?part=snippet,contentDetails,statistics&id=UCE_M8A5yxnLfW0KghEeajjw&key=

Get Channel sections:

https://www.googleapis.com/youtube/v3/channelSections?part=snippet,contentDetails&channelId=UCE_M8A5yxnLfW0KghEeajjw&key=

To get Playlists :

Get Playlists by Channel ID:

https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&channelId=UCq-Fj5jknLsUf-MWSy4_brA&maxResults=50&key=

Get Playlists by Channel ID with pageToken:

https://www.googleapis.com/youtube/v3/playlists?part=snippet,contentDetails&channelId=UCq-Fj5jknLsUf-MWSy4_brA&maxResults=50&key=&pageToken=CDIQAA

To get PlaylistItems :

Get PlaylistItems list by PlayListId:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults=25&playlistId=PLHFlHpPjgk70Yv3kxQvkDEO5n5tMQia5I&key=

To get videos :

Get videos list by video id:

https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=YxLCwfA1cLw&key=

Get videos list by multiple videos id:

https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=YxLCwfA1cLw,Qgy6LaO3SB0,7yPJXGO2Dcw&key=

Get comments list

Get Comment list by video ID:

https://www.googleapis.com/youtube/v3/commentThreads?part=snippet,replies&videoId=el****kQak&key=A**********k

Get Comment list by channel ID:

https://www.googleapis.com/youtube/v3/commentThreads?part=snippet,replies&channelId=U*****Q&key=AI********k

Get Comment list by allThreadsRelatedToChannelId:

https://www.googleapis.com/youtube/v3/commentThreads?part=snippet,replies&allThreadsRelatedToChannelId=UC*****ntcQ&key=AI*****k

Here all api's are Get approach.

Based on channel id we con't get all videos directly, that's the important point here.

For integration https://developers.google.com/youtube/v3/quickstart/ios?ver=swift

Solution 5

Here is the code that will return all video ids under your channel

<?php 
    $baseUrl = 'https://www.googleapis.com/youtube/v3/';
    // https://developers.google.com/youtube/v3/getting-started
    $apiKey = 'API_KEY';
    // If you don't know the channel ID see below
    $channelId = 'CHANNEL_ID';

    $params = [
        'id'=> $channelId,
        'part'=> 'contentDetails',
        'key'=> $apiKey
    ];
    $url = $baseUrl . 'channels?' . http_build_query($params);
    $json = json_decode(file_get_contents($url), true);

    $playlist = $json['items'][0]['contentDetails']['relatedPlaylists']['uploads'];

    $params = [
        'part'=> 'snippet',
        'playlistId' => $playlist,
        'maxResults'=> '50',
        'key'=> $apiKey
    ];
    $url = $baseUrl . 'playlistItems?' . http_build_query($params);
    $json = json_decode(file_get_contents($url), true);

    $videos = [];
    foreach($json['items'] as $video)
        $videos[] = $video['snippet']['resourceId']['videoId'];

    while(isset($json['nextPageToken'])){
        $nextUrl = $url . '&pageToken=' . $json['nextPageToken'];
        $json = json_decode(file_get_contents($nextUrl), true);
        foreach($json['items'] as $video)
            $videos[] = $video['snippet']['resourceId']['videoId'];
    }
    print_r($videos);

Note: You can get channel id at https://www.youtube.com/account_advanced after logged in.

Share:
308,656

Related videos on Youtube

Rajendra Dewani
Author by

Rajendra Dewani

Updated on April 25, 2022

Comments

  • Rajendra Dewani
    Rajendra Dewani about 2 years

    We need a video list by channel name of YouTube (using the API).

    We can get a channel list (only channel name) by using the below API:

    https://gdata.youtube.com/feeds/api/channels?v=2&q=tendulkar
    

    Below is a direct link of channels

    https://www.youtube.com/channel/UCqAEtEr0A0Eo2IVcuWBfB9g
    

    Or

    WWW.YouTube.com/channel/HC-8jgBP-4rlI
    

    Now, we need videos of channel >> UCqAEtEr0A0Eo2IVcuWBfB9g or HC-8jgBP-4rlI.

    We tried

    https://gdata.youtube.com/feeds/api/videos?v=2&uploader=partner&User=UC7Xayrf2k0NZiz3S04WuDNQ https://gdata.youtube.com/feeds/api/videos?v=2&uploader=partner&q=UC7Xayrf2k0NZiz3S04WuDNQ

    But, it does not help.

    We need all the videos posted on the channel. Videos uploaded to a channel can be from multiple users thus I don't think providing a user parameter would help...

    • filthy_wizard
      filthy_wizard over 4 years
      can i get access to my own video files. to essentially download my own content when logged in via the API!!!
  • Rajendra Dewani
    Rajendra Dewani over 10 years
    For UCqAEtEr0A0Eo2IVcuWBfB9g it works, i think this is because its uploaded by a single user. How about HC-8jgBP-4rlI
  • akshay
    akshay over 10 years
    It is easy to say, but why don`t you put your answer with a good and working example???
  • Als
    Als about 10 years
    Didn't check my messages until today. You're right. Only for an user channel.
  • Fábio Perez
    Fábio Perez over 9 years
    This will only return the first maxResults=20 (up to 50) videos, but not the entire channel catalogue. If you want more results, use the pageToken as described here.
  • macabeus
    macabeus over 9 years
    Where is the documentation for that API?
  • Romulus Urakagi Ts'ai
    Romulus Urakagi Ts'ai over 9 years
    I don't find where the requested feature is written in document. Videos have only three filters and none of them relate to channel.
  • akshay
    akshay over 9 years
    Romulus Urakagi Ts'ai: while requesting videos you are giving channelId, that is the filter for channel.
  • Bruce van der Kooij
    Bruce van der Kooij over 8 years
    This API is no longer available: "The YouTube Data API (v2) has been officially deprecated as of March 4, 2014"
  • kristopolous
    kristopolous about 8 years
    Not everyone has a youtube username in this brand new world. Some of them only have a google+ user id number, which doesn't work in the place of a youtube username
  • user3078414
    user3078414 almost 8 years
    The link is dead.
  • Tony Paternite
    Tony Paternite over 7 years
    Worth noting you can only get up to 500 videos from a channel using the next page tokens.
  • Goodlife
    Goodlife over 7 years
    Using the browser Key will save you tie in android too and create your own HTTP CLIENT LIKE Retrofit to fetch data
  • Raja
    Raja over 7 years
    @TonyPaternite How do I get more than 500 videos. I am currently facing this issue.
  • Raja
    Raja over 7 years
    The issue is explained well here. code.google.com/p/gdata-issues/issues/detail?id=4282 Trying with a different range of dates is one way to get more videos it seems.
  • Tony Paternite
    Tony Paternite over 7 years
    @Raja Yeah, I would try that option with the date range. The API doesn't allow more than 500 results from a channel.
  • Ignat
    Ignat over 7 years
    @kristopolous Every YouTube channel has a channel ID. If you go to a YouTube page via Google plus, it uses the Google plus user id as the link to the channel. If you went to the channel from a YouTube video, it uses YouTube's channel ID property instead.
  • JP de la Torre
    JP de la Torre over 7 years
    This is perfect. Particularly because it only spends 2 quota points instead of 100 (that the search call would spend).
  • Ken Sharp
    Ken Sharp over 7 years
    @kristopolous is correct: you can't use those channel IDs as names.
  • hello_there_andy
    hello_there_andy over 7 years
    May I ask what ?key={your_key_here} needs, specifically, what is: your_key_here? How can I get that?
  • akshay
    akshay over 7 years
    @hello_there_andy : You need a Google Account to access the Google Developers Console, request an API key, and register your application. For more info, please check : developers.google.com/youtube/v3/getting-started
  • Muhammad
    Muhammad about 7 years
    Notice that playlist items api will include unlisted and deleted videos too, but you can use search api to list public videos only by channel id
  • ishandutta2007
    ishandutta2007 about 7 years
    Can "uploads" Id change for a given channel ?
  • ishandutta2007
    ishandutta2007 about 7 years
    somteimes it(developers.google.com/youtube/v3/docs/playlistItems/list#‌​try-it) works , sometimes it throws 404 for same uploadId, don't know whats going on.
  • ishandutta2007
    ishandutta2007 about 7 years
    seems uploads are same as channelId , but terribly inconsistent APIs, can some answer this stackoverflow.com/questions/43568521/…
  • mwm
    mwm over 6 years
    this also includes the channel item. for me it works adding also type=video
  • Adrien H
    Adrien H about 6 years
    You can try the api here : developers.google.com/apis-explorer/#search/youtube/youtube/‌​v3/… (apis-exporer - > youtube data api -> search.list ), don't forget to set the parameter type to "video".
  • Joffrey Baratheon
    Joffrey Baratheon almost 6 years
    How does this work? I pasted the API KEY using single quotes ' ' into the api_key variable, then I called the function passing in the channel id, then ran the python program, but nothing happens.
  • Joffrey Baratheon
    Joffrey Baratheon almost 6 years
    Are you sure that this works? The GitHub page is gone and then when I input API Key and channel id into the fields, I get errors.
  • Nikhil VJ
    Nikhil VJ almost 6 years
    @JoffreyBaratheon thanks for pointing out the broken github link. I just ran it.. it works. Getting that playlist id is tricky.. pls see this: github.com/answerquest/answerquest.github.io/issues/2
  • vicke4
    vicke4 almost 6 years
    @ishandutta2007 no uploads id is different from channel id.
  • phapha pha
    phapha pha almost 6 years
    @JoffreyBaratheon the function return an array, you need to contain that to a variable. For example : video_list = get_all_video_in_channel("ABC123EFG456") then you could print it to see the array using print(video_list)
  • cdlvcdlv
    cdlvcdlv almost 6 years
    I think this is the best answer since it doesn't need API key. To make it even more automated you can use @for /f "usebackq tokens=2 delims=: " %a in (`dl-list.py^|findstr information`) do @echo https://www.youtube.com/watch?v=%a. It will print all the URL of the videos. PLAYLIST_ID can be a playlist or channel ID.
  • cdlvcdlv
    cdlvcdlv almost 6 years
    You can infer it but I forgot to say that I saved the code as dl-list.py.
  • Harsh sachdev
    Harsh sachdev about 5 years
    @jambrose in the playlistItems endpoint how to use some date range parameter like publishAfter/ publishedBefore. I tried It, not working So how I fetch all the videos of a channel.
  • Jan Bludau
    Jan Bludau almost 5 years
    nice job. works very well with the updated github link. you will need jquery and papaparse.min.js
  • Nikhil VJ
    Nikhil VJ almost 5 years
    @thE_iNviNciblE thanks for bringing that up. made a mention of it.
  • Jan Bludau
    Jan Bludau almost 5 years
    @nikhil-vj it's not useable for a real world szenario, you shouldn't give the Client Browser the youtube API-Token. But for programming education quite nice job :-)
  • Nikhil VJ
    Nikhil VJ almost 5 years
    @thE_iNviNciblE that's why I let the user put in their own token :P . But API tokens are used all over the net; one can put url restrictions IP restrictions etc at the token management side so even if people take it they can't use it at their end. We can dispose tokens and make new ones quickly. It's not trivial, but not too critical of a threat either.
  • Madushan
    Madushan almost 5 years
    I was scraping channel pages because search endpoint costs 100 points. Liek to say a big THANK YOU for this one. This was not obvious from the docs and it works perfectly. Also the search call doesn't necessarily return only the specified channel, and it gives up after a few 100 results. This endpoint seem to work fine.
  • Khom Nazid
    Khom Nazid almost 5 years
    Does this get all the videos with titles, URL of the video, an image identifying the video, number of likes/comments? Some info on how this publishes would be helpful. Thanks.
  • Aaska Patel
    Aaska Patel almost 5 years
    @virtualmic@Peter Perfect Solution Guys , Thanks
  • Mago Maverick
    Mago Maverick almost 5 years
    This one is definitely a clean solution that works. Currently using in production.
  • CMash
    CMash over 4 years
    beware of using search as it has a quote cost of 100!
  • Berry Blue
    Berry Blue over 4 years
    @CMash This is the issue that I'm encountering. You run through your quota very fast.
  • jtubre
    jtubre over 4 years
    If you want to reduce quota by another 2 units, set part=snippet only. contentDetails (videoId, videoPublishedAt) can be found inside the snippet node so it's redundant.
  • Onkar Musale
    Onkar Musale over 4 years
    it is working fine. but i have multiple channel ids, so how can i send them to api, can i use comma seperated channel ids
  • Onkar Musale
    Onkar Musale over 4 years
    How can i send multiple channel ids to api, can i use comma separated channel ids
  • Matt Booth
    Matt Booth about 4 years
    @vicke4 is correct, they are different and it looks like from the channels I deal with (quite a few) that the channels' uploads ID is the same as the Channel ID, except it starts with UU instead of UC. This may not be a certainty though, I can't find any doco about it.
  • Teemoh
    Teemoh about 4 years
    Please note: the search operation is very expensive (100 quota) compared to the operations suggested by jambrose (3 + 5 quota) (as of march 2020). See Quota Calculator.
  • poke
    poke almost 4 years
    For those that need it - I implemented this approach, you can try it online: yts.sourceforge.net/getchannelvideos.html
  • Rajeshwar
    Rajeshwar almost 4 years
    How can i get the top 10/50/100 youtube channels using api?
  • IvanM
    IvanM almost 4 years
    it may miss some videos! Instead, use "uploads" playlist as described below stackoverflow.com/a/27872244/2154075
  • Yasin Okumuş
    Yasin Okumuş over 3 years
    Even though this might be the quickest way, the cost of "search" is 100 unit and that may result quota exceed.
  • Asylzat
    Asylzat over 3 years
    thanks man, it really works just pasted and everything works great!
  • Scuilla
    Scuilla over 3 years
    @MattBooth is spot on. It seems the pattern is Channel ID = UC + { User ID }, Upload Playlist ID = UU + { User ID } . I'm guessing it's auto-generated for every channel at the point of creation (or for old channels, when the switch happened from YT's API to Google's). Still can't find any source that confirms it, but through testing a few hundred channels, they were all followed this pattern. UC would make sense to denote User Channel, while UU could be User Uploads.
  • TrisT
    TrisT over 3 years
    You can ofc use a channel ID to get this information in the first place, just specify the id parameter instead of forUsername. If you're attempting to get the upload playlist for multiple channels at once like I am, you can specify multiple IDs with either a comma or using the id parameter multiple times.
  • TrisT
    TrisT over 3 years
    Also, one thing I noticed is that as (as far as I can see), the uploads playlist ID is the same as the channel ID with the second character from the left replaced with a U. So for instance the uploads playlist for channelID UC-lHJZR3Gqxm24_Vd_AJ5Yw would be UU-lHJZR3Gqxm24_Vd_AJ5Yw. Makes sense, I assume it would stand for "user channel" and "user uploads". So if you need a few extra quota points I suppose that could be a solution.
  • ying
    ying over 3 years
    Is there a way to search video later than a timestamp?
  • jave.web
    jave.web over 3 years
    Can you please add the information about how to get ALL videos using this based on pageToken from the 2nd part,so the answer is complete for this Q. (I know how to do it, but just for others) :)
  • John Funk
    John Funk about 3 years
    This is exactly what I was looking for, using the "upload" playlist id is far from being intuitive.
  • Tassoman
    Tassoman about 3 years
    JFYF live videos are missing from the uploads related playlist of the channel
  • teran
    teran about 3 years
    it looks like api limits to 20k videos in playlist while search with empty query returns 26k videos
  • teran
    teran about 3 years
    looks like this is limited up to 20k videos
  • Antony
    Antony about 3 years
    Its also worth saying that if the forUsername stops working - revisit the page, and see if it defaults to an ID instead. One of my forUsernames wasn't working but grabbing the ID instead did.
  • Duncan Marshall
    Duncan Marshall about 3 years
    I tried this but it failed with a quota exceeded error, even the only thing I've done with my quota is tried this query and got that error - it's a brand new project. Anything about this that could have caused that?
  • Emiswelt
    Emiswelt over 2 years
    And how do you resolve the playlist_id from the channel list?
  • Ammar Alyousfi
    Ammar Alyousfi over 2 years
    This does not return all channel's videos. It returns all videos in the uploads playlist, but not all channel's videos.
  • jfar_2020
    jfar_2020 over 2 years
    @jambrose How can this be modified to include view count too? I can list all videos from a specific playlist but I need view count too, if I simply change part=snippet to part=snippet,statistics I then get no results.
  • Tom
    Tom about 2 years
    To Download the List one can use Youtube API, too. There is no need to use additional tools like youtube-dl