php 如何从 Youtube 频道获取所有视频 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6331053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:03:19  来源:igfitidea点击:

How can I get all videos ID's from a Youtube channel

phpapiyoutubeyoutube-apiyoutube-channels

提问by Jake

How can I get all video id's from the youtube data feed?

如何从 youtube 数据源中获取所有视频 ID?

I receive the youtube feed via this (API) URL: http://gdata.youtube.com/feeds/base/users/#userid#/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile

我通过这个 (API) URL 收到 youtube 提要:http: //gdata.youtube.com/feeds/base/users/#userid#/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube- profile

I already know how to extract the links, descriptions and thumbnails from a Channel, but I want to extract all the video Id's from a Channel (e.g. http://www.youtube.com/watch?v=WWooNnPnHTs)

我已经知道如何从频道中提取链接、描述和缩略图,但我想从频道中提取所有视频 ID(例如http://www.youtube.com/watch?v= WooNnPnHTs

回答by Fred

This is my way. Slow, but it works. :)

这是我的方式。慢,但它有效。:)

function getVideos($channel){
    if($channel == ""){
        return false;   
    }
    /* Get number of videos */
    $books = simplexml_load_file('http://gdata.youtube.com/feeds/base/users/'.$channel.'/uploads?max-results=1&start-index=1');
    $numb_videos = $books->children( 'openSearch', true )->totalResults; 
    settype($numb_videos, "integer");

    $ids = array();
    $i = 1;
    for($i = 1; $i <= $numb_videos; $i++){
        $books = simplexml_load_file('http://gdata.youtube.com/feeds/base/users/'.$channel.'/uploads?max-results=1&start-index='.$i);
        $ApiLink  = $books->entry->id;
        settype($ApiLink, "string");
        $ApiLink = str_replace("http://gdata.youtube.com/feeds/base/videos/", "", $ApiLink);
        array_push($ids, $ApiLink);
    }
    return $ids;    
}

回答by akshay

Use this:

用这个:

GET https://www.googleapis.com/youtube/v3/search?part=id&channelId=UC9MAhZQQd9egwWCxrwSIsJQ&maxResults=10&order=date&key={YOUR_API_KEY}

获取https://www.googleapis.com/youtube/v3/search?part=id&channelId=UC9MAhZQQd9egwWCxrwSIsJQ&maxResults=10&order=date&key={YOUR_API_KEY}

And you will get a result for the above url as:

您将获得上述网址的结果:

200 OK

- Show headers -

{
"kind": "youtube#searchListResponse",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/WiiEAt3fgPkFw_831Iveo6mV-IU\"",
"nextPageToken": "CAQQAA",
"pageInfo": {
"totalResults": 1046,
"resultsPerPage": 4
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/OtU1Ja-W-gNf83iiXWzodKk3Ce0\"",
"id": {
"kind": "youtube#video",
"videoId": "jKLMD-LXIgk"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/EUhMCxemh2UGmf2ufGS0IYdcMUs\"",
"id": {
"kind": "youtube#video",
"videoId": "glCQQeH_ddw"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/2IMOnedhjKXxnFZy-PNg5o80kkY\"",
"id": {
"kind": "youtube#video",
"videoId": "yB78MIcmDxs"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"qQvmwbutd8GSt4eS4lhnzoWBZs0/oEb7q9_GwGdXcHsvuRDuNmh_rGQ\"",
"id": {
"kind": "youtube#video",
"videoId": "NnkDja1cXlo"
}
}
]
}

For more reference you may check here

如需更多参考,您可以在此处查看

回答by esh

$channelsResponse = $youtube->channels->listChannels('id,contentDetails', array(
    'mine' => 'true'));

$playlistId = $channelsResponse['items']['contentDetails']['relatedPlaylists']['uploads'];
$searchResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
        'playlistId' => $playlistId,
        'maxResults' => 50,
        'fields' => 'items(snippet(publishedAt,channelId,title,description,thumbnails(default),resourceId)),pageInfo,nextPageToken'));

echo json_encode($searchResponse['items']['contentDetails']['videoId']);

use youtube data api v3 to do that. here's the link

使用 youtube data api v3 来做到这一点。 这是链接

回答by Alejandro

Hi I think you should have to retrieve the value from the v parameter in the link of the video, you can use regex or substrings...

嗨,我认为您应该从视频链接中的 v 参数中检索值,您可以使用正则表达式或子字符串...

回答by Gerben Van Dijk

I know it's an old question, but I wrote up something that does what is asked for here:

我知道这是一个老问题,但我写了一些可以满足这里要求的内容:

https://gist.github.com/gport/5693404

https://gist.github.com/gport/5693404

Just edit line 23:

只需编辑第 23 行:

return $videoids[array_rand($videoids)];

To:

到:

return $videoids;

This will return an array containing the users video ID's :)

这将返回一个包含用户视频 ID 的数组:)

edit: sorry guys ( @testing, @Code_Ed_Student ), no idea why the gist is not there anymore but I found it in my archives, code below. The implementation is a bit different from in the old example, but this achieves the same (you could replace the array by any other logic).

编辑:抱歉,伙计们(@testing,@Code_Ed_Student),不知道为什么要点不再存在,但我在我的档案中找到了它,代码如下。该实现与旧示例中的实现略有不同,但这实现了相同的(您可以用任何其他逻辑替换数组)。

    $feedresult = simplexml_load_file('http://gdata.youtube.com/feeds/api/users/wondervol/uploads');

    if($feedresult) {

    $videoids = array();

    foreach ($feedresult->entry as $video) {

            $media = $video->children('media', true);
            $url = (string)$media->group->player->attributes()->url;
            $index = strrpos($url, "&");
            $url = substr($url, 0, $index);
            $index = strrpos($url, "watch");
            $url = substr($url, $index + 8, strlen($url) - ($index + 8)); 

            $videoids[] = $url;

    }

回答by Eugene Belov

It might look like the searchAPI is a good idea to retrieve all the videos by a single loop, but apparently not. If a channel has thousands (more than 1600) of videos, searchwill not return all of them: few last result pages will be empty, number of results will be wrong, and the amount of given pages will be around only 20 pages.

看起来searchAPI 是通过单个循环检索所有视频的好主意,但显然不是。如果一个频道有数千个(超过 1600 个)视频,search则不会全部返回:最后的结果页面很少是空的,结果数量会出错,给定页面的数量只有 20 页左右。

So, if you need to get all IDs via API, then it's better to use at first channelsAPI to get an "uploads" playlist ID, and then use it in playlistItemsAPI to retrieve all videos.

因此,如果您需要通过 API 获取所有 ID,那么最好首先使用channelsAPI 获取“上传”播放列表 ID,然后在playlistItemsAPI 中使用它来检索所有视频。

First request URL will look like this (using https://www.googleapis.com/youtube/v3/in the beginning, of course):

第一个请求 URL 将如下所示(当然,在开头使用https://www.googleapis.com/youtube/v3/):

channels?part=contentDetails&id=CHANNEL_ID&key=YOUR_TOKEN

Result:

结果:

{
    "kind": "youtube#channelListResponse",
    "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/Nza6t_W5qltBCnHIHKgNiSRu0bE\"",
    "pageInfo": {
    "totalResults": 1,
        "resultsPerPage": 1
    },
    "items": [
        {
            "kind": "youtube#channel",
            "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/95y8HKrtWV3t4apHu0KjgoaNbc0\"",
            "id": "UC9-y-6csu5WGm29I7JiwpnA",
            "contentDetails": {
                "relatedPlaylists": {
                    "likes": "LL9-y-6csu5WGm29I7JiwpnA",
                    "favorites": "FL9-y-6csu5WGm29I7JiwpnA",
                    "uploads": "UU9-y-6csu5WGm29I7JiwpnA",
                    "watchHistory": "HL",
                    "watchLater": "WL"
                }
            }
        }
    ]
}

Or if you don't need tons of bytes, use filter fields like this:

或者,如果您不需要大量字节,请使用如下过滤字段:

channels?fields=items(contentDetails(relatedPlaylists(uploads)))&part=contentDetails&id=CHANNEL_ID&key=YOUR_TOKEN

Filtered result:

过滤结果:

{
    "items": [
        {
            "contentDetails": {
                "relatedPlaylists": {
                    "uploads": "UU9-y-6csu5WGm29I7JiwpnA"
                }
            }
        }
    ]
}

Next step is to get list of all pages with videos:

下一步是获取所有包含视频的页面的列表:

playlistItems?maxResults=50&playlistId=PLAYLIST_ID&part=snippet&key=YOUR_TOKEN

Result (shorten by 1st result of array in itemskey):

结果(items按键中数组的第一个结果缩短):

{
    "kind": "youtube#playlistItemListResponse",
    "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/GmoHN7IVzBjFEtB0547SRegTL9c\"",
    "nextPageToken": "CDIQAA",
    "pageInfo": {
        "totalResults": 399,
        "resultsPerPage": 50
    },
    "items": [
        {
            "kind": "youtube#playlistItem",
            "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/5gxLp2iP0FwcZQWb98LCOGu0TfA\"",
            "id": "VVU5LXktNmNzdTVXR20yOUk3Sml3cG5BLjlua3RyMU1nUy1B",
            "snippet": {
                "publishedAt": "2017-08-03T11:27:38.000Z",
                "channelId": "UC9-y-6csu5WGm29I7JiwpnA",
                "title": "Stop Button Solution? - Computerphile",
                "description": "After seemingly insurmountable issues with Artificial General Intelligence, Rob Miles takes a look at a promising solution.\n\nConcrete Problems in AI Safety: blah blah blah blah blah blah blah blah blah",
                "thumbnails": {
                    "default": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/default.jpg",
                        "width": 120,
                        "height": 90
                    },
                    "medium": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/mqdefault.jpg",
                        "width": 320,
                        "height": 180
                    },
                    "high": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/hqdefault.jpg",
                        "width": 480,
                        "height": 360
                    },
                    "standard": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/sddefault.jpg",
                        "width": 640,
                        "height": 480
                    },
                    "maxres": {
                        "url": "https://i.ytimg.com/vi/9nktr1MgS-A/maxresdefault.jpg",
                        "width": 1280,
                        "height": 720
                    }
                },
                "channelTitle": "Computerphile",
                "playlistId": "UU9-y-6csu5WGm29I7JiwpnA",
                "position": 0,
                "resourceId": {
                    "kind": "youtube#video",
                    "videoId": "9nktr1MgS-A"
                }
            }
        },
        {
            ...
        }
    ]
}

Or the filtered one to get only IDs of the videos (and next page tokens):

或者过滤后只获取视频的 ID(和下一页令牌):

playlistItems?fields=nextPageToken,items(snippet(resourceId(videoId)))&maxResults=50&playlistId=PLAYLIST_ID&part=snippet&key=YOUR_TOKEN

Result:

结果:

{
    "nextPageToken": "CDIQAA",
    "items": [
        {
            "snippet": {
                "resourceId": {
                    "videoId": "9nktr1MgS-A"
                }
            }
        }
    ]
}

You can retrieve next pages by using nextPageTokenkey value, adding it to GET parameters with pageTokenkey:

您可以使用nextPageToken键值检索下一页,使用键将其添加到 GET 参数中pageToken

(I took a token from the result above just for example, it could be different for your request)

(例如,我从上面的结果中获取了一个令牌,您的请求可能会有所不同)

playlistItems?fields=nextPageToken,items(snippet(resourceId(videoId)))&maxResults=50&part=snippet&playlistId=PLAYLIST_ID&key=YOUR_TOKEN&pageToken=CDIQAA

On the last page there will be no nextPageTokenkey.

在最后一页将没有nextPageToken钥匙。

回答by user279297

Notice: Array to string conversion in

注意:数组到字符串的转换

C:\wamp\www\youtube-vimeo-api-playlist-to-database-importer-master\youtube-vimeo-api-playlist-to-database-importer.php

C:\wamp\www\youtube-vimeo-api-playlist-to-database-importer-master\youtube-vimeo-api-playlist-to-database-importer.php

on line 28

在第 28 行