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

时间:2011-06-13 13:25:50

标签: php api youtube youtube-api youtube-channels

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

我通过此(API)网址收到了youtube Feed: http://gdata.youtube.com/feeds/base/users/#userid#/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile

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

7 个答案:

答案 0 :(得分:4)

这是我的方式。慢,但它的工作原理。 :)

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;    
}

答案 1 :(得分:3)

使用此:

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

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

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"
}
}
]
}

如需更多参考,请查看here

答案 2 :(得分:2)

$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']);

使用youtube data api v3来做到这一点。 here's the link

答案 3 :(得分:0)

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

答案 4 :(得分:0)

我知道这是一个古老的问题,但是我写了一些可以做到这里要求的东西:

<击> https://gist.github.com/gport/5693404

只需编辑第23行:

return $videoids[array_rand($videoids)];

要:

return $videoids;

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

编辑:对不起家伙(@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;

    }

答案 5 :(得分:0)

search API似乎是一个好主意,可以通过一个循环检索所有视频,但显然不是。如果某个频道有数千(超过1600)个视频,search将无法返回所有视频:最后几个结果页面将为空,结果数量将会出错,并且给定页面的数量仅为20页。

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

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

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

结果:

{
    "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"
                }
            }
        }
    ]
}

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

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

过滤结果:

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

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

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

结果(缩短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"
                }
            }
        },
        {
            ...
        }
    ]
}

或过滤过的只能获取视频的ID(以及下一页令牌):

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

结果:

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

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

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

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

在最后一页上没有nextPageToken键。

答案 6 :(得分:-4)

注意:

中的数组到字符串转换

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

第28行