javascript 如何获得 YouTube 视频纵横比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5010495/
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
How to get YouTube video aspect ratio
提问by infojunkie
I'd like to get the aspect ratio of a YouTube video, to resize the player accordingly. I'm programming the YT player using JavaScript.
我想获得 YouTube 视频的纵横比,以相应地调整播放器的大小。我正在使用 JavaScript 对 YT 播放器进行编程。
回答by huesforalice
I would suggest hitting the oembed url:
我建议点击 oembed 网址:
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={videoID}&format=json
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={videoID}&format=json
This gives you the exact video dimensions for videos that are public. I'm not sure about private videos though. It will also return thumbnail dimensions, which seem to be different im some cases, so just be sure to not mix them up.
这为您提供了公开视频的确切视频尺寸。不过,我不确定私人视频。它还将返回缩略图尺寸,在某些情况下似乎有所不同,因此请确保不要将它们混淆。
回答by Jeff Posnick
The only place that exact video dimensions are exposed in a Data API call is when you make a videos.list(part=fileDetails, id=VIDEO_ID)call using the v3 API, while authenticated as the owner of the video. It's returned in the video.fileDetails.videoStreams[].aspectRatioproperty. This isn't particularly useful, since you need to be authenticated as the video's owner in order to get that info.
在数据 API 调用中显示确切视频尺寸的唯一地方是当您使用 v3 API 进行视频列表(part=fileDetails, id=VIDEO_ID)调用时,同时验证为视频的所有者。它在video.fileDetails.videoStreams[].aspectRatio属性中返回。这不是特别有用,因为您需要通过视频所有者的身份验证才能获得该信息。
If you just have a webpage, and want to make a JSONPcall to get a hint about whether a given video is 16:9 or 4:3, you can do that via something like
如果您只有一个网页,并且想要进行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/VIDEO_ID?v=2&alt=jsonc&callback=myCallback
E.g.
例如
http://gdata.youtube.com/feeds/api/videos/F1IVb2_FYxQ?v=2&alt=jsonc&callback=myCallback
http://gdata.youtube.com/feeds/api/videos/F1IVb2_FYxQ?v=2&alt=jsonc&callback=myCallback
has "aspectRatio":"widescreen"set in its response, which is a hint that the video is 16:9 (or close to 16:9).
已"aspectRatio":"widescreen"在其响应中设置,暗示视频为 16:9(或接近 16:9)。
http://gdata.youtube.com/feeds/api/videos/u1zgFlCw8Aw?v=2&alt=jsonc&callback=myCallback
http://gdata.youtube.com/feeds/api/videos/u1zgFlCw8Aw?v=2&alt=jsonc&callback=myCallback
does not have aspectRatioset at all, which means that the videos is 4:3 (or close to 4:3). It's not always the exact aspect ration, but it's close enough for the vast majority of videos to be useful.
根本没有aspectRatio设置,这意味着视频是 4:3(或接近 4:3)。它并不总是准确的纵横比,但它足够接近,足以让绝大多数视频有用。
回答by TDave00
Here is how I do it. I get the aspect ratio from the youtube image.
这是我如何做到的。我从 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>
I pull all the video information from the youtube api and then store all the video information in a database beforehand, so if you are doing this on the fly, you might have to hide the image on the page and then get the aspect ratio that way.
我从 youtube api 中提取所有视频信息,然后预先将所有视频信息存储在数据库中,因此如果您正在执行此操作,则可能必须隐藏页面上的图像,然后以这种方式获取纵横比.
edit** Another option, and probably the best, would be to use youtube's api.
Search for a video, and check if the data->items->aspectRatio is set. I don't think it's set on 4:3 video, but on 16:9 it is set to widescreen. Should be as simple as if (data->items->aspectRatio) {ratio= "16:9"} else {ratio="4:3"}
编辑** 另一种选择,可能也是最好的,是使用youtube 的 api。搜索一段视频,查看是否设置了data->items->aspectRatio。我不认为它设置为 4:3 视频,但在 16:9 设置为宽屏。应该很简单if (data->items->aspectRatio) {ratio= "16:9"} else {ratio="4:3"}
回答by Shaz
Aspect ratio apparently depends on the quality level. Taken from the YouTube Docs:
纵横比显然取决于质量水平。取自YouTube 文档:
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.
回答by expert
My goal was to get aspect ratio for any video, not only for those for which I'm owner.
我的目标是获得任何视频的纵横比,而不仅仅是我拥有的视频。
Thus the trick is to use https://developers.google.com/youtube/v3/docs/videos/listwith playerprovided in partsand then parsing widthand heightof returned embed html.
因此,关键是要使用https://developers.google.com/youtube/v3/docs/videos/list与player在提供parts,然后解析width和height返回嵌入HTML的。
回答by Nick Rice
Maybe not a good answer, but there seems to be an assumption amongst other answers that YouTube videos are either 16:9 or 4:3.
也许不是一个好的答案,但在其他答案中似乎有一个假设,即 YouTube 视频是 16:9 或 4:3。
But they can have a pretty much arbitrary aspect ratio, and with portrait phone videos having become quite common, it's becoming less of a rarity for a video on YouTube to be something different.
但是它们可以具有几乎任意的纵横比,并且随着纵向手机视频变得非常普遍,YouTube 上的视频变得不那么罕见了。
For these non-standard aspect ratios, as a quick manual fudge, I've resorted to playing them in full screen, doing a screen capture, and cropping the image down.
对于这些非标准的纵横比,作为一种快速的手动软糖,我采用了全屏播放、截屏和裁剪图像的方法。
I've put a couple of examples of arbitrary aspect videos at http://youtube-aspect-ratios.xtra.ink.
我在http://youtube-aspect-ratios.xtra.ink 上放了几个任意方面视频的例子。

