How to get YouTube video aspect ratio
Asked Answered
R

6

19

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.

Ringmaster answered 15/2, 2011 at 22:53 Comment(0)
D
25

I would suggest hitting the oembed url:

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.

Dromedary answered 28/10, 2015 at 11:11 Comment(2)
Thank you. I was looking for a way to check my YT videos' aspect ratio and your answer solved my problem.Cleo
Warning - this does not work, at least not in 2024. Youtube returns same dimensions for all videos, even shorts which are clearly vertical.Clatter
E
7

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[].aspectRatio property. This isn't particularly useful, since you need to be authenticated as the video's owner in order to get that info.

If you just have a webpage, and want to make a JSONP call to get a hint about whether a given video is 16:9 or 4:3, you can do that via something like

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

has "aspectRatio":"widescreen" set in its response, which is a hint that the video is 16:9 (or close to 16:9).

http://gdata.youtube.com/feeds/api/videos/u1zgFlCw8Aw?v=2&alt=jsonc&callback=myCallback

does not have aspectRatio set 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.

Ethyl answered 9/4, 2013 at 17:15 Comment(2)
Not all videos are 16:9 or 4:3 so assuming a video is 4:3 unless you can tell it's about 16:9 doesn't work. What about videos shot in portrait mode on a phone?Discernment
The URLs in this answer now return "no longer available".Rummel
L
5

Here is how I do it. I get the aspect ratio from the youtube image.

<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.

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

Letterpress answered 18/3, 2013 at 20:59 Comment(3)
Clever! I'll try it when I get the chance. Marking as resolved because this question has been open too long.Ringmaster
I tried the first idea in this answer, but try to look at this image i.ytimg.com/vi/9bZkp7q19f0/default.jpg and compare with this: i.ytimg.com/vi/nnS7G3Y-IDc/default.jpg. Those two pictures have the same dimensions even though their original ratios are different. This might not work at all - Youtube places black bars in top and bottom of the images as well as on embed iframe videos.Rearm
I don't think this is a good approach. Thumbnail images might be of an arbitrary dimension unrelated to the source video, as they sometimes have letter/pillar boxing.Ethyl
A
4

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/list with player provided in parts and then parsing width and height of returned embed html.

Antoniaantonie answered 6/12, 2018 at 10:12 Comment(2)
This is actually the way to do it if you want the aspect ratio of any video. Also, by providing a maxWidth query parameter, the player part contains the embedHeight, from which the aspect ratio can then be calculated. See developers.google.com/youtube/v3/docs/videos#player.embedHeight and developers.google.com/youtube/v3/docs/videos/list#maxWidthGermangermana
This also does not work - player has always same dimensions, no matter what video.Clatter
S
2

Aspect ratio apparently depends on the quality level. Taken from the 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.
Safari answered 15/2, 2011 at 23:0 Comment(3)
This piece of doc says that each quality level can have 16:9 or 4:3 aspect ratio.Ringmaster
Except for the first and last. I couldn't find any other documentation for aspect ratio either. But take a look at the different width's and heights. Surely you could use that to determine the ratio?Safari
It's my code that sets the player's width and height - doesn't make sense to use them to derive aspect ratio.Ringmaster
D
-1

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.

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.

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.

Discernment answered 24/12, 2016 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.