我们需要YouTube的频道名称的视频列表(使用API)。

我们可以通过下面的API获得一个频道列表(只有频道名):

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

下面是频道的直接链接

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

Or

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

现在,我们需要>> UCqAEtEr0A0Eo2IVcuWBfB9g或HC-8jgBP-4rlI频道的视频。

我们尝试

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

但是,这并没有帮助。

我们需要把所有视频发到频道上。上传到一个频道的视频可以来自多个用户,因此我不认为提供用户参数会有帮助…


当前回答

从https://stackoverflow.com/a/65440501/2585501:

如果a)该频道有超过50个视频,或者b)希望将youtube视频id格式化为平面txt列表,则此方法特别有用:

Obtain a Youtube API v3 key (see https://stackoverflow.com/a/65440324/2585501) Obtain the Youtube Channel ID of the channel (see https://stackoverflow.com/a/16326307/2585501) Obtain the Uploads Playlist ID of the channel: https://www.googleapis.com/youtube/v3/channels?id={channel Id}&key={API key}&part=contentDetails (based on https://www.youtube.com/watch?v=RjUlmco7v2M) Install youtube-dl (e.g. pip3 install --upgrade youtube-dl or sudo apt-get install youtube-dl) Download the Uploads Playlist using youtube-dl: youtube-dl -j --flat-playlist "https://<yourYoutubePlaylist>" | jq -r '.id' | sed 's_^_https://youtu.be/_' > videoList.txt (see https://superuser.com/questions/1341684/youtube-dl-how-download-only-the-playlist-not-the-files-therein)

其他回答

从https://stackoverflow.com/a/65440501/2585501:

如果a)该频道有超过50个视频,或者b)希望将youtube视频id格式化为平面txt列表,则此方法特别有用:

Obtain a Youtube API v3 key (see https://stackoverflow.com/a/65440324/2585501) Obtain the Youtube Channel ID of the channel (see https://stackoverflow.com/a/16326307/2585501) Obtain the Uploads Playlist ID of the channel: https://www.googleapis.com/youtube/v3/channels?id={channel Id}&key={API key}&part=contentDetails (based on https://www.youtube.com/watch?v=RjUlmco7v2M) Install youtube-dl (e.g. pip3 install --upgrade youtube-dl or sudo apt-get install youtube-dl) Download the Uploads Playlist using youtube-dl: youtube-dl -j --flat-playlist "https://<yourYoutubePlaylist>" | jq -r '.id' | sed 's_^_https://youtu.be/_' > videoList.txt (see https://superuser.com/questions/1341684/youtube-dl-how-download-only-the-playlist-not-the-files-therein)

下面是一个来自谷歌开发者的视频,展示了如何在YouTube API的v3中列出一个频道中的所有视频。

有两个步骤:

查询Channels以获得“uploads”Id。例如https://www.googleapis.com/youtube/v3/channels?id={channel Id}&key={API key}&part=contentDetails 使用这个“uploads”Id查询PlaylistItems以获得视频列表。例如https://www.googleapis.com/youtube/v3/playlistItems?playlistId={"uploads" Id}&key={API key}&part=snippet&maxResults=50

首先,你需要从用户/频道获取代表上传的播放列表的ID:

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

您可以使用forUsername={username}参数指定用户名,或者使用mine=true来获得自己的用户名(您需要先进行身份验证)。Include part=contentDetails查看播放列表。

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

结果“relatedPlaylists”将包括“likes”和“uploads”播放列表。抓取“上传”播放列表ID。

还要注意,上传播放列表id是你的channelId,前缀是UU而不是UC。

接下来,获取播放列表中的视频列表:

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

只需要输入playlistId!

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

下面是一个不需要任何特殊包的Python替代方案。通过提供频道id,它将返回该频道的视频链接列表。请注意,你需要一个API密钥为它工作。

import urllib
import json

def get_all_video_in_channel(channel_id):
    api_key = YOUR API KEY

    base_video_url = 'https://www.youtube.com/watch?v='
    base_search_url = 'https://www.googleapis.com/youtube/v3/search?'

    first_url = base_search_url+'key={}&channelId={}&part=snippet,id&order=date&maxResults=25'.format(api_key, channel_id)

    video_links = []
    url = first_url
    while True:
        inp = urllib.urlopen(url)
        resp = json.load(inp)

        for i in resp['items']:
            if i['id']['kind'] == "youtube#video":
                video_links.append(base_video_url + i['id']['videoId'])

        try:
            next_page_token = resp['nextPageToken']
            url = first_url + '&pageToken={}'.format(next_page_token)
        except:
            break
    return video_links

最近,我不得不从一个频道检索所有视频,根据YouTube开发者文档: https://developers.google.com/youtube/v3/docs/playlistItems/list

function playlistItemsListByPlaylistId($service, $part, $params) {
    $params = array_filter($params);
    $response = $service->playlistItems->listPlaylistItems(
        $part,
        $params
    );

    print_r($response);
}

playlistItemsListByPlaylistId($service,
    'snippet,contentDetails',
    array('maxResults' => 25, 'playlistId' => 'id of "uploads" playlist'));

$service是Google_Service_YouTube对象。

因此,您必须从频道获取信息以检索“uploads”播放列表,该列表实际上包含该频道上传的所有视频:https://developers.google.com/youtube/v3/docs/channels/list

如果这个API是新的,我强烈建议将代码样例从默认代码片段转换为完整的样例。

因此,从一个频道检索所有视频的基本代码可以是:

class YouTube
{
    const       DEV_KEY = 'YOUR_DEVELOPPER_KEY';
    private     $client;
    private     $youtube;
    private     $lastChannel;

    public function __construct()
    {
        $this->client = new Google_Client();
        $this->client->setDeveloperKey(self::DEV_KEY);
        $this->youtube = new Google_Service_YouTube($this->client);
        $this->lastChannel = false;
    }

    public function getChannelInfoFromName($channel_name)
    {
        if ($this->lastChannel && $this->lastChannel['modelData']['items'][0]['snippet']['title'] == $channel_name)
        {
            return $this->lastChannel;
        }
        $this->lastChannel = $this->youtube->channels->listChannels('snippet, contentDetails, statistics', array(
            'forUsername' => $channel_name,
        ));
        return ($this->lastChannel);
    }

    public function getVideosFromChannelName($channel_name, $max_result = 5)
    {
        $this->getChannelInfoFromName($channel_name);
        $params = [
            'playlistId' => $this->lastChannel['modelData']['items'][0]['contentDetails']['relatedPlaylists']['uploads'],
            'maxResults'=> $max_result,
        ];
        return ($this->youtube->playlistItems->listPlaylistItems('snippet,contentDetails', $params));
    }
}

$yt = new YouTube();
echo '<pre>' . print_r($yt->getVideosFromChannelName('CHANNEL_NAME'), true) . '</pre>';