【发布时间】:2011-06-27 23:38:52
【问题描述】:
我想获取 YouTube 视频的宽高比,以相应地调整播放器的大小。我正在使用 JavaScript 对 YT 播放器进行编程。
【问题讨论】:
标签: javascript youtube
我想获取 YouTube 视频的宽高比,以相应地调整播放器的大小。我正在使用 JavaScript 对 YT 播放器进行编程。
【问题讨论】:
标签: javascript youtube
我建议点击 oembed 网址:
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={videoID}&format=json
这将为您提供公开视频的确切视频尺寸。不过,我不确定私人视频。它还会返回缩略图尺寸,在某些情况下看起来会有所不同,因此请确保不要混淆它们。
【讨论】:
在数据 API 调用中唯一会显示确切视频尺寸的地方是,当您使用 v3 API 进行 videos.list(part=fileDetails, id=VIDEO_ID) 调用时,同时验证为视频的所有者。它在 video.fileDetails.videoStreams[].aspectRatio 属性中返回。这并不是特别有用,因为您需要验证为视频的所有者才能获取该信息。
如果您只有一个网页,并且想要拨打JSONP 以获取有关给定视频是 16:9 还是 4:3 的提示,您可以通过类似的方式来做到这一点
http://gdata.youtube.com/feeds/api/videos/VIDEO_ID?v=2&alt=jsonc&callback=myCallback
例如
http://gdata.youtube.com/feeds/api/videos/F1IVb2_FYxQ?v=2&alt=jsonc&callback=myCallback
在其响应中设置了"aspectRatio":"widescreen",这暗示视频是 16:9(或接近 16:9)。
http://gdata.youtube.com/feeds/api/videos/u1zgFlCw8Aw?v=2&alt=jsonc&callback=myCallback
根本没有设置aspectRatio,这意味着视频是4:3(或接近4:3)。它并不总是精确的宽高比,但对于绝大多数视频来说,它已经足够接近了。
【讨论】:
这是我的做法。我从 youtube 图像中获取纵横比。
<img id"nnS7G3Y-IDc-img" src="http://i.ytimg.com/vi/nnS7G3Y-IDc/default.jpg" />
<script>
//using jquery
var height = $('#nnS7G3Y-IDc-img').css('height');
var width = $('#nnS7G3Y-IDc-img').css('width');
height = height.replace('px', '');
width = width.replace('px', '');
var arB = height / 3;
var arT = width / arB;
if (arT == 4) {
//do what you need to with the aspect ratio info from here
//just demonstrating with an alert
alert ("4:3");
}
else {alert ("16:9");}
</script>
我从 youtube api 中提取所有视频信息,然后将所有视频信息预先存储在数据库中,因此如果您正在执行此操作,您可能需要隐藏页面上的图像,然后获取方面比例那样。
edit** 另一个可能也是最好的选择是使用youtube's api。
搜索视频,检查是否设置了 data->items->aspectRatio。我不认为它设置在 4:3 视频上,但在 16:9 上设置为宽屏。应该像if (data->items->aspectRatio) {ratio= "16:9"} else {ratio="4:3"}一样简单
【讨论】:
iframe 视频中放置了黑条。
纵横比显然取决于质量水平。取自YouTube Docs:
Quality level small: Player height is 240px, and player dimensions are at least 320px by 240px for 4:3 aspect ratio.
Quality level medium: Player height is 360px, and player dimensions are 640px by 360px (for 16:9 aspect ratio) or 480px by 360px (for 4:3 aspect ratio).
Quality level large: Player height is 480px, and player dimensions are 853px by 480px (for 16:9 aspect ratio) or 640px by 480px (for 4:3 aspect ratio).
Quality level hd720: Player height is 720px, and player dimensions are 1280px by 720px (for 16:9 aspect ratio) or 960px by 720px (for 4:3 aspect ratio).
Quality level hd1080: Player height is 1080px, and player dimensions are 1920px by 1080px (for 16:9 aspect ratio) or 1440px by 1080px (for 4:3 aspect ratio).
Quality level highres: Player height is greater than 1080px, which means that the player's aspect ratio is greater than 1920px by 1080px.
【讨论】:
我的目标是获得任何视频的宽高比,而不仅仅是我拥有的视频。
因此诀窍是使用https://developers.google.com/youtube/v3/docs/videos/list 和parts 中提供的player,然后解析返回的嵌入html 的width 和height。
【讨论】:
也许不是一个好的答案,但在其他答案中似乎有一个假设,即 YouTube 视频是 16:9 或 4:3。
但它们可以具有几乎任意的纵横比,并且随着纵向手机视频变得相当普遍,YouTube 上的视频变得与众不同的情况越来越少。
对于这些非标准的宽高比,作为一种快速的手动软糖,我采取了全屏播放、截屏和裁剪图像的方式。
我在http://youtube-aspect-ratios.xtra.ink 放了几个任意方面视频的示例。
【讨论】: