如何根据视频网址从Youtube API获取Youtube视频ID?

时间:2017-05-10 15:42:15

标签: youtube youtube-api youtube-data-api

我有一个页面,用户可以粘贴Youtube视频网址,然后我需要提取视频的ID以获取视频的缩略图。困难的部分是获得从视频网址中提取视频ID的可靠方法。这里有许多正则表达式解决方案,但对我来说,这些都不是100%可靠的。这里有几个:

  1. JavaScript REGEX: How do I get the YouTube video id from a URL?
  2. Youtube API - Extract video ID
  3. 这就像是一个拥有更长正则表达式的比赛。我觉得正确的方法是从Youtube的api获取这些信息,但看着它,似乎这个选项不可用:

    https://developers.google.com/apis-explorer/#p/youtube/v3/

    正如您所看到的,所有内容都基于视频ID。我觉得很荒谬,我需要实际的视频ID来获取有关视频的任何信息,因为没有真实世界的用户甚至不知道视频ID是什么。 Vimeo在其API中内置了此功能。这是一个例子:

    https://vimeo.com/api/oembed.json?url=https://vimeo.com/29474908

    有没有人为此提供不涉及正则表达式的解决方案?

1 个答案:

答案 0 :(得分:1)

执行search with the API适用于不同的YouTube网址类型。该网址作为查询字词 q 传递。

https://www.googleapis.com/youtube/v3/search/?key=<YOUR_KEY>&part=snippet&q=youtu.be/M7lc1UVf-VE

也许某些情况可能导致多个项目,但正常的搜索结果只是一个匹配:

{
    kind: "youtube#searchListResponse",
    etag: ""m2yskBQFythfE4irbTIeOgYYfBU/j2Px-5q--mgJEsrfjg4L0Mgn_L8"",
    regionCode: "ES",
    pageInfo: {
        totalResults: 1,
        resultsPerPage: 5
    },
    items: [
        {
            kind: "youtube#searchResult",
            etag: ""m2yskBQFythfE4irbTIeOgYYfBU/_1gFVi_i_djlS4OZWPGtcZ3iSLQ"",
            id: {
                kind: "youtube#video",
                videoId: "M7lc1UVf-VE"
            },
            snippet: {
                publishedAt: "2013-04-10T17:25:04.000Z",
                channelId: "UC_x5XG1OV2P6uZZ5FSM9Ttw",
                title: "YouTube Developers Live: Embedded Web Player Customization",
                description: "On this week's show, Jeff Posnick covers everything you need to know about using player parameters to customize the YouTube iframe-embedded player.",
                thumbnails: {
                    default: {
                        url: "https://i.ytimg.com/vi/M7lc1UVf-VE/default.jpg",
                        width: 120,
                        height: 90
                    },
                    medium: {
                        url: "https://i.ytimg.com/vi/M7lc1UVf-VE/mqdefault.jpg",
                        width: 320,
                        height: 180
                    },
                    high: {
                        url: "https://i.ytimg.com/vi/M7lc1UVf-VE/hqdefault.jpg",
                        width: 480,
                        height: 360
                    }
                },
                channelTitle: "Google Developers",
                liveBroadcastContent: "none"
            }
        }
    ]
}

我使用this answer中的一些网址变体进行了测试,其中大多数工作都是:

var urls = [
    '//www.youtube-nocookie.com/embed/M7lc1UVf-VE?rel=0',
    'https://www.youtube.com/watch?v=M7lc1UVf-VE&feature=channel',
    'https://www.youtube.com/watch?v=M7lc1UVf-VE&playnext_from=TL&videos=osPknwzXEas&feature=sub',
    'https://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I', // <---- invalid
    'https://youtu.be/M7lc1UVf-VE',
    'https://www.youtube.com/watch?v=M7lc1UVf-VE&feature=youtu.be',
    'https://youtu.be/M7lc1UVf-VE',
    'https://www.youtube.com/watch?v=M7lc1UVf-VE&feature=channel',
    'https://www.youtube.com/watch?v=M7lc1UVf-VE&playnext_from=TL&videos=osPknwzXEas&feature=sub',
    'https://www.youtube.com/ytscreeningroom?v=M7lc1UVf-VE', // <---- invalid
    'https://www.youtube.com/embed/M7lc1UVf-VE?rel=0',
    'https://www.youtube.com/watch?v=M7lc1UVf-VE',
    'https://youtube.com/v/M7lc1UVf-VE?feature=youtube_gdata_player',
    'https://youtube.com/vi/M7lc1UVf-VE?feature=youtube_gdata_player', // <---- invalid
    'https://youtube.com/?v=M7lc1UVf-VE&feature=youtube_gdata_player',
    'https://www.youtube.com/watch?v=M7lc1UVf-VE&feature=youtube_gdata_player',
    'https://youtube.com/?vi=M7lc1UVf-VE&feature=youtube_gdata_player', // <---- invalid
    'https://youtube.com/watch?v=M7lc1UVf-VE&feature=youtube_gdata_player',
    'https://youtube.com/watch?vi=M7lc1UVf-VE&feature=youtube_gdata_player',
    'https://youtu.be/M7lc1UVf-VE?feature=youtube_gdata_player'
];

var my_key = '<YOUR_KEY>';

function getUri(uri){
    $.get('https://www.googleapis.com/youtube/v3/search/?key='+my_key+'&part=snippet&q='+uri, function(data) {
        if(data.items.length !== 0)
            console.log(data.items[0].snippet.publishedAt);
        else
            console.warn('no items for',uri)
    });
}

for (i = 0; i < urls.length; ++i) {
    getUri(urls[i]);
}