在 Javascript 中通过 JSON 获取单个视频(不是提要)的 Youtube 信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10066638/
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-23 23:42:46  来源:igfitidea点击:

Get Youtube information via JSON for single video (not feed) in Javascript

javascriptjqueryjsonyoutube

提问by Frank Visaggio

So I am trying to get information from a single youtube video via in the JSON format. Like title description category, ect whatever I can get besides the comments. I am trying to do this in Javascript. I noticed the link below but all of their examples are how to get video information from feeds. I would like to get the information from a single video assuming i know its ID.

所以我试图通过 JSON 格式从单个 youtube 视频中获取信息。就像标题描述类别一样,除了评论之外,我还能得到什么。我正在尝试在 Javascript 中执行此操作。我注意到下面的链接,但他们所有的例子都是如何从提要中获取视频信息。假设我知道它的 ID,我想从单个视频中获取信息。

https://developers.google.com/youtube/2.0/developers_guide_json

https://developers.google.com/youtube/2.0/developers_guide_json

I was also looking at this Stackoverflow Questionbut I have an issue with the get request it says "test.js (line 10) GET http://gdata.youtube.com/feeds/api/videos/VA770w...v=2&alt=json-in-script&callback=listInfo

我也在看这个Stackoverflow 问题,但我对 get 请求有一个问题,它说“test.js(第 10 行)GET http://gdata.youtube.com/feeds/api/videos/VA770w...v= 2&alt=json-in-script&callback=listInfo

200 OK 9ms"

200 OK 9ms"

In brief, if i have a single youtube videos id like VA770wpLX-Q, what would the url look like to get that videos information in JSON?

简而言之,如果我有一个像 VA770wpLX-Q 这样的 youtube 视频 ID,那么在 JSON 中获取该视频信息的 URL 会是什么样子?

Thank you

谢谢

采纳答案by stewe

UPDATE MAY/2015:

2015 年 5 月更新:

This solution doesn't work properly, YouTube API v2 is in process to be discontinued soon.

此解决方案无法正常工作,YouTube API v2 正在处理中,即将停止使用。

More info at: https://www.youtube.com/devicesupport

更多信息请访问:https: //www.youtube.com/devicesupport



Try something like this:

尝试这样的事情:

var video_id='VA770wpLX-Q';

$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){
    alert(data.data.title);
    // data contains the JSON-Object below
});

Demo: http://jsfiddle.net/wqwxg/

演示:http: //jsfiddle.net/wqwxg/

The returned JSON looks like this:

返回的 JSON 如下所示:

{
    "apiVersion": "2.1",
    "data": {
        "id": "VA770wpLX-Q",
        "uploaded": "2011-02-24T22:31:02.000Z",
        "updated": "2012-04-08T21:37:06.000Z",
        "uploader": "drdrevevo",
        "category": "Music",
        "title": "Dr. Dre - I Need A Doctor (Explicit) ft. Eminem, Skylar Grey",
        "description": "Music video by Dr. Dre performing I Need A Doctor featuring Eminem and Skylar Grey (Explicit). ? 2011 Aftermath Records",
        "tags": ["Dr", "Dre", "Eminem", "New", "Song", "Skylar", "Grey", "GRAMMYs", "Dr.", "Need", "Doctor", "video", "Eazy", "N.W.A.", "NWA", "easy", "drdre", "and", "em"],
        "thumbnail": {
            "sqDefault": "http://i.ytimg.com/vi/VA770wpLX-Q/default.jpg",
            "hqDefault": "http://i.ytimg.com/vi/VA770wpLX-Q/hqdefault.jpg"
        },
        "player": {
            "default": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player"
        },
        "content": {
            "5": "http://www.youtube.com/v/VA770wpLX-Q?version=3&f=videos&app=youtube_gdata"
        },
        "duration": 457,
        "aspectRatio": "widescreen",
        "rating": 4.902695,
        "likeCount": "430519",
        "ratingCount": 441253,
        "viewCount": 88270796,
        "favoriteCount": 306556,
        "commentCount": 270597,
        "status": {
            "value": "restricted",
            "reason": "requesterRegion"
        },
        "restrictions": [{
            "type": "country",
            "relationship": "deny",
            "countries": "DE"
        }],
        "accessControl": {
            "comment": "allowed",
            "commentVote": "allowed",
            "videoRespond": "allowed",
            "rate": "allowed",
            "embed": "allowed",
            "list": "allowed",
            "autoPlay": "denied",
            "syndicate": "allowed"
        }
    }
}

回答by Alexufo

UPDATED 2018

2018 年更新

API v2 deprecated. New youtube api v3 works only with developer token and has limitation for free connections.

API v2 已弃用。新的 youtube api v3 仅适用于开发人员令牌,并且对免费连接有限制。

You can get JSON without API:

您可以在没有 API 的情况下获取 JSON:

http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=ojCkgU5XGdg&format=json

Or xml

或 xml

http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=ojCkgU5XGdg&format=xml





new 2018 json response has

新的 2018 json 响应有

{
  "html": "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/ojCkgU5XGdg?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe>",
  "title": "Creativity and Drugs (Eng Sub)",
  "thumbnail_height": 360,
  "provider_name": "YouTube",
  "author_url": "https://www.youtube.com/user/serebniti",
  "thumbnail_width": 480,
  "height": 270,
  "provider_url": "https://www.youtube.com/",
  "type": "video",
  "width": 480,
  "version": "1.0",
  "author_name": "serebniti",
  "thumbnail_url": "https://i.ytimg.com/vi/ojCkgU5XGdg/hqdefault.jpg"
}

Thumbs:

大拇指:

hqdefault.jpghas less quality but always exist.

hqdefault.jpg质量较差,但始终存在。

http://img.youtube.com/vi/ojCkgU5XGdg/hqdefault.jpg

http://img.youtube.com/vi/ojCkgU5XGdg/hqdefault.jpg

http://img.youtube.com/vi/ojCkgU5XGdg/sddefault.jpg

http://img.youtube.com/vi/ojCkgU5XGdg/sddefault.jpg

Max size

最大尺寸

https://i.ytimg.com/vi/ojCkgU5XGdg/maxresdefault.jpg

Mini thumbs:

小拇指:

http://img.youtube.com/vi/ojCkgU5XGdg/0.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/1.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/2.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/3.jpg

Annotations

注释

http://www.youtube.com/annotations_invideo?cap_hist=1&video_id=ojCkgU5XGdg

parse mobile page 16kb

解析移动页面 16kb

https://m.youtube.com/watch?v=ojCkgU5XGdg

don't forget change user agent to iOS / Safari 7

不要忘记将用户代理更改为 iOS / Safari 7

also

http://www.youtube.com/get_video_info?html5=1&video_id=ojCkgU5XGdg

also how embed youtube live

还有如何嵌入youtube live

https://www.youtube.com/embed/live_stream?channel=UCkA21M22vGK9GtAvq3DvSlA

Where UCkA21M22vGK9GtAvq3DvSlA is your channel id. You can find it inside youtube account on "My Channel" link.

其中 UCkA21M22vGK9GtAvq3DvSlA 是您的频道 ID。您可以在 YouTube 帐户中的“我的频道”链接中找到它。

Live thumb

活拇指

https://i.ytimg.com/vi/W-fSCPrYSL8/hqdefault_live.jpg

回答by rsp

Problem

问题

YouTube API doesn't support JSONP as it should - see Issue 4329: oEmbed callback for JSONP. Also, YouTube Data API v2 is deprecated.

YouTube API 不支持 JSONP,因为它应该支持 - 请参阅问题 4329: JSONP 的 oEmbed 回调。此外,不推荐使用 YouTube 数据 API v2

Solution

解决方案

You can use the Noembedservice to get oEmbeddata with JSONPfor YouTube videos.

您可以使用Noembed服务通过JSONP获取 YouTube 视频的oEmbed数据。

Bonus

奖金

  • no API keys are needed
  • no server-side proxy is required
  • 不需要 API 密钥
  • 不需要服务器端代理

Example

例子

For your VA770wpLX-Qvideo, you can try a link like this:

对于您的VA770wpLX-Q视频,您可以尝试使用以下链接:

https://noembed.com/embed?url=http://www.youtube.com/watch?v=VA770wpLX-Q

https://noembed.com/embed?url=http://www.youtube.com/watch?v=VA770wpLX-Q

Or this for JSONP:

或者这对于 JSONP:

https://noembed.com/embed?callback=example&url=http://www.youtube.com/watch?v=VA770wpLX-Q

https://noembed.com/embed?callback=example&url=http://www.youtube.com/watch?v=VA770wpLX-Q

Those links have a standard URL of a YouTube video passed as the urlparameter. It works not only with YouTube but also with Vimeo and other siteswith URLs like:

这些链接具有作为url参数传递的 YouTube 视频的标准 URL 。它不仅适用于 YouTube,还适用于 Vimeo 和其他具有以下 URL 的网站

https://noembed.com/embed?url=https://vimeo.com/45196609

https://noembed.com/embed?url=https://vimeo.com/45196609

Demo

演示

Here is a simple example using jQuery:

这是一个使用 jQuery 的简单示例:

var id = 'VA770wpLX-Q';
var url = 'https://www.youtube.com/watch?v=' + id;

$.getJSON('https://noembed.com/embed',
    {format: 'json', url: url}, function (data) {
    alert(data.title);
});

See: DEMOon JS Bin.

请参阅:JS Bin 上的演示

Other options

其他选项

  • Embedly(commercial service, free up to 5000 URLs/month)
  • Oohembed(update: now bought by Embedly, but the sourceis available)
  • AutoEmbed(update: seems to be down or discontinued)
  • Embedly(商业服务,每月最多免费 5000 个 URL)
  • Oohembed(更新:现在由Embedly购买,但来源可用)
  • AutoEmbed(更新:似乎已关闭或已停产)

More info

更多信息

See also those questions:

另见这些问题:

回答by Mario Gonzales Flores

in v3:

在 v3 中:

$.getJSON('https://www.googleapis.com/youtube/v3/videos?id={videoId}&key={myApiKey}&part=snippet&callback=?',function(data){

 if (typeof(data.items[0]) != "undefined") {
     console.log('video exists ' + data.items[0].snippet.title);
   } else {
     console.log('video not exists');
 }   
});

In response a @Jonathan via server side, using PHP and CURL:

通过服务器端响应@Jonathan,使用 PHP 和 CURL:

$url = "https://www.googleapis.com/youtube/v3/videos?id=".$videoId."&key=".$miApikey."&part=snippet";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$output=curl_exec($ch);                 
$response = json_decode($output, TRUE);
print_r($response);