给定视频网址,如何从 Youtube API 获取 Youtube 视频 ID? [英] How can I get a Youtube video ID from the Youtube API given the video url?

查看:55
本文介绍了给定视频网址,如何从 Youtube API 获取 Youtube 视频 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a page where users can paste a Youtube video url and then I need to extract the video's ID in order to get the video's thumbnail image. The hard part is to get a reliable way of extracting the video ID from the video url. There are a number of regex solutions here but for me none of these are 100% reliable. Here's a few:

  1. JavaScript 正则表达式:如何从 URL 获取 YouTube 视频 ID?
  2. Youtube API - 提取视频 ID

这就像一场关于谁拥有更长正则表达式的比赛.我觉得正确的方法是从 Youtube 的 api 中获取此信息,但查看它,似乎此选项不可用:

It's like a contest of who has a longer regex. I feel like the right way would be to get this information from Youtube's api, but looking at it, seems like this option is not available:

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

如您所见,一切都基于视频 ID.我觉得我需要实际的视频 ID 来获取有关视频的任何信息是荒谬的,因为现实世界中的用户甚至都不知道视频 ID 是什么.Vimeo 在其 API 中内置了此功能.下面是一个例子:

As you can see everything is based on the video id. I find it ridiculous that I need the actual video ID to get any information about the video since no real world user will ever even know what the video id is. Vimeo has this feature built into their API. Here is an example:

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

有没有人有一个不涉及一些正则表达式的解决方案?

Does anyone have a solution for this that does not involve some regular expression?

推荐答案

做一个搜索使用 API 适用于不同的 YouTube 网址类型.URL 作为查询词 q 传递.

Doing a search with the API works with different YouTube URL types. The URL is passed as the query term q.

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

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

Maybe some case could result in more than one item, but the normal search result is just one match:

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

我使用这个答案中的一些 URL 变体进行了测试,并且大部分都有效:

I tested with some of the URL variations from this answer and most worked:

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

这篇关于给定视频网址,如何从 Youtube API 获取 Youtube 视频 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆