我们需要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

但是,这并没有帮助。

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


当前回答

最近,我不得不从一个频道检索所有视频,根据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>';

其他回答

从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)

下面是返回频道下所有视频id的代码

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

注意:你可以在 登录https://www.youtube.com/account_advanced。

使用已弃用的API版本2,用于上传的URL(通道UCqAEtEr0A0Eo2IVcuWBfB9g)是:

https://gdata.youtube.com/feeds/users/UCqAEtEr0A0Eo2IVcuWBfB9g/uploads

有一个API版本3。

使用gapi JavaScript API可以做到这一点

<script src="https://apis.google.com/js/api.js"></script>
const start = () => {
  gapi.client
    .init({
      apiKey: "your_youtubeApiKey",
      discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest"],
      scope: "https://www.googleapis.com/auth/youtube.readonly",
    })
    .then(() => {
      console.log("gapi.client initiated");
    })
    .then(() =>
      gapi.client.youtube.channels.list({
        part: "snippet,contentDetails,statistics",
        id: "youtube_channelId",
        // forUsername: 'Bankless',
      })
    )
    .then(
      (res) =>
        // get the youtube related playlist id
        res.result.items[0].contentDetails.relatedPlaylists.uploads
    )
    .then((playlistId) =>
      gapi.client.youtube.playlistItems.list({
        part: "snippet",
        playlistId,
        maxResults: 50,
      })
    )
    .then((res) =>
      // get youtube videos snippets
      res.result.items.map((item) => item.snippet)
    )
    .then((snippets) =>
      snippets.map((snippet) => {
        const { title, description, resourceId } = snippet;
        const { videoId } = resourceId;
        return { title, description, videoId };
      })
    )
    .then((videos) => {
      console.log(videos);
    })
    .catch((err) => console.error(err));
};
gapi.load("client", start);

文档:

https://github.com/google/google-api-javascript-client https://developers.google.com/youtube/v3/guides/auth/client-side-web-apps#callinganapi

在最初的问题被问到之后很久才发布,但我做了一个python包,使用一个非常简单的API来做到这一点。它把所有的视频上传到一个频道,但我不确定这一部分(包括在最初的问题中):

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

也许YouTube在这个问题发布后的8年里发生了变化,但如果没有,我制作的软件包可能不包括这个案例。

使用API:

pip3 install -U yt-videos-list # macOS
pip  install -U yt-videos-list # Windows

# if that doesn't work, try
python3 -m pip install -U yt-videos-list # macOS
python  -m pip install -U yt-videos-list # Windows

然后打开一个python解释器

python3   # macOS
python    # Windows

然后运行程序:

from yt_videos_list import ListCreator
lc = ListCreator()

help(lc) # display API information - shows available parameters and functions

my_url = 'https://www.youtube.com/user/1veritasium'

lc.create_list_for(url=my_url)

Python文档(将最频繁地更新,所以请检查此页面的更新!) 库主页 PyPI页面