获取YouTube视频尺寸(宽/高)

Min*_*ele 12 video youtube-api

我正在尝试获取YouTube视频的大小.我正在使用Gdata API调用来检索基本信息(标题,网址,缩略图和类别),但我找不到视频尺寸.

我正在使用YouTube Data API服务器端调用在网站上嵌入一些视频,如下所示:http://gdata.youtube.com/feeds/api/videos/z_AbfPXTKms?0 = v&1 = 2 &alt = json.不幸的是,没有关于视频尺寸的可靠信息(即使使用宽屏视频,所有预览图像都是4/3速率).

我想要完成的是将视频完全融入播放器; 玩家宽度是固定的,所以我只需要原始尺寸或至少比例.

有没有办法使用Youtube API检索此类数据?

(我的后备计划是将玩家大小设置为4/3并且永不回头,但任何帮助都会受到赞赏!)

Isr*_*sra 11

你也可以通过YouTube的oEmbed实现来获得它.例:

https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=hUvbb_eUP84&format=xml

format=json 也被接受.

  • 这个视频是1080p,但仍然返回480x270:http://www.youtube.com/oembed?url = http%3A/www.youtube.com/watch?v%3DucivXRBrP_0&format=json所以我会回答以色列的回答 (4认同)

Isr*_*sra 8

现在有一个解决方案:如果你刮掉视频公共页面:

http://www.youtube.com/watch?v=[id]

你可以看到打开的图形宽度和高度标签,例如:

<meta property="og:video:width" content="1920">
<meta property="og:video:height" content="1080">
Run Code Online (Sandbox Code Playgroud)

所以你可以在那里获得尺寸;-)

  • 没有这样的标签. (3认同)

rsp*_*rsp 8

2017年更新 - 接受的答案不再有效

如果您想从问题中获取示例视频的维度:

然后使用Noembed尝试此URL :

它将返回以下JSON:

{
  "version": "1.0",
  "title": "?????",
  "author_name": "mugumogu",
  "provider_url": "https://www.youtube.com/",
  "width": 459,
  "height": 344,
  "thumbnail_height": 360,
  "type": "video",
  "thumbnail_width": 480,
  "provider_name": "YouTube",
  "url": "https://www.youtube.com/watch?v=z_AbfPXTKms",
  "author_url": "https://www.youtube.com/user/mugumogu",
  "html": "\n<iframe width=\" 459\" height=\"344\" src=\"https://www.youtube.com/embed/z_AbfPXTKms?feature=oembed\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"></iframe>\n",
  "thumbnail_url": "https://i.ytimg.com/vi/z_AbfPXTKms/hqdefault.jpg"
}
Run Code Online (Sandbox Code Playgroud)

它还支持JSONP用于跨源请求:

有关更多信息,请参阅此答案:

演示

一个使用jQuery的简单演示:

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

$.getJSON('https://noembed.com/embed',
    {format: 'json', url: url}, function (data) {
    alert('Dimensions: ' + data.width + 'x' + data.height);
});
Run Code Online (Sandbox Code Playgroud)

请参阅JSBin上的DEMO.

  • 不幸的是,它增加了对第三方网站的依赖.谁跑了?它会永远在线吗?... (4认同)