18

I’m trying to get the size of a YouTube video. I’m using a Gdata API call to retrieve the basic informations (title, URLs, thumbnails and categories) but I can’t find the video dimensions.

I’m embedding some videos on a website using YouTube Data API server-side calls like this: http://gdata.youtube.com/feeds/api/videos/z_AbfPXTKms?0=v&1=2&alt=json. Unfortunately, there is no reliable information on video dimensions (all the preview images are in 4/3 rate even with widescreen videos).

What I am trying to accomplish is to fit the video exactly into the player; the player width is fixed, so I just need the original dimensions or at least the proportion.

Is there any way to retrieve this kind of data with the Youtube API?

(My fallback plan is to set the player size to 4/3 and never watch back, but any help is appreciated!)

TRiG
  • 10,148
  • 7
  • 57
  • 107
Minkiele
  • 1,260
  • 2
  • 19
  • 32

5 Answers5

12

You can also get it thanks to the oEmbed implementation of YouTube. Example:

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

format=json is also accepted.

VC.One
  • 14,790
  • 4
  • 25
  • 57
Isra
  • 602
  • 1
  • 6
  • 14
  • 6
    This video is 1080p but still returns 480x270: http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v%3DucivXRBrP_0&format=json So I'd go with Israel's answer – Antoine Apr 29 '13 at 14:15
  • 1
    Thanks for reporting it! YouTube staff seems to care not so much about their oEmbed API :-( – Isra May 13 '13 at 13:26
  • 1
    No offense but is this the highest rated and accepted answer although it does not work? Or am I wrong? – jakob.j May 14 '15 at 16:03
  • 1
    @jakob.j You're right, this no longer works. See [my answer](http://stackoverflow.com/questions/9514635/get-youtube-video-dimensions-width-height/41730155#41730155) with a solution that works (as of 2017). – rsp Jan 18 '17 at 22:05
11

Update 2017 - The accepted answer no longer works

If you want to get dimensions for the example video from the question:

Then try this URL, using Noembed:

It will return the following 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"
}

It also supports JSONP for cross-origin requests:

For more info see this answer:

Demo

A simple demo using 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);
});

See DEMO on JSBin.

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
  • This video is 300x250 https://www.youtube.com/watch?v=DAvyF2CKfNg I don't get the correct size, or proportion even. – Jakob Sternberg Mar 10 '17 at 11:12
  • It's strange, for some videos it's correct (for example, https://www.youtube.com/watch?v=tsJEmLkphrI) but for others it's incorrect.. I suppose it's a youtube bug or videos really changed they size while were uploading to youtube.. – starikovs Dec 06 '17 at 14:51
  • 7
    unfortunately it adds a dependency on a third party site. Who runs that? Will it be online forever? ... – rupps Jul 22 '18 at 17:51
9

There is a solution right now: if you scrape the video public page:

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

you can see the open graph width and height tags, for example:

<meta property="og:video:width" content="1920">
<meta property="og:video:height" content="1080">

So you can get the dimensions there ;-)

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Isra
  • 602
  • 1
  • 6
  • 14
  • Is there a possibility to get this information without loading the complete html code of the site? If one wants to get the resolution of a few videos at once, it seems to be a relevant efficiency overhead. – jakob.j May 14 '15 at 16:01
  • 3
    There are no such tags. – catamphetamine Sep 23 '18 at 09:43
  • 1
    what is that answer? there are no such tags. Where should it be, in the source page code, or generated one? – serge Oct 03 '18 at 16:42
3

If you load the youtube video like this, you can trigger a javascript function only after the video has loaded.

<script src="https://www.youtube.com/iframe_api"></script>
    <center>
        <div class="videoWrapper">
           <div id="player"></div>
        </div>
    </center>
<script>
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
            videoId:'xxxxxxxxxxx',playerVars: {  controls:0,autoplay:0,disablekb:1,enablejsapi:1,iv_load_policy:3,modestbranding:1,showinfo:0,rel:0,theme:'light' }
        } );
        resizeHeroVideo();
    }
</script>

resizeHeroVideo is called after the video is loaded.

var player = null;
$( document ).ready(function() {
    resizeHeroVideo();
} );

$(window).resize(function() {
    resizeHeroVideo();
});

function resizeHeroVideo() {
    var content = $('#hero');
    var contentH = viewportSize.getHeight();
    contentH -= 158;
    content.css('height',contentH);

    if(player != null) {
        var iframe = $('.videoWrapper iframe');
        var iframeH = contentH - 150;
        if (isMobile) {
            iframeH = 163; 
        }
        iframe.css('height',iframeH);
        var iframeW = iframeH/9 * 16;
        iframe.css('width',iframeW);
    }
}

and then we calculate the sizes to maintain it's position in the center of the page adhering to the appropriate 16:9 aspect ratio.

Complete gist here. Live example here.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
davidcondrey
  • 34,416
  • 17
  • 114
  • 136
3

Following on from Isra's solution. Yes it does seem that youtube hasn't developed their oEmbed API code as the videoFrameSize returns 470 x 270 px. But least it gives you something to GRAB hold of dynamically!

Using the suck it ans see method that 'Lee taylor' put forward is a KISS way of achieving this, but not ideal if you need a dynamic content.

OK - bearing in mind the inaccurate dimensions provided by oEmbed - it does provide a solution.

I am currently developing a strusturedData generator for video content on a gallery all linked to youTube videos.

Here is an inaccurate way of getting a Dimension from youtube, but least its data!

How to get the video size from youtube.

Stage 1: Call the oEmbed data from youtube::

$video_id = "your youtube videos unique ID number ";

$oEmbed         = simplexml_load_file('http://www.youtube.com/oembed?url=http%3A//www.youtube.com/watch?v=' . $video_id . '&format=xml');

Stage 2:

    $data['height'] = $oEmbed->height;
    $data['width']  = $oEmbed->width;

    $data['videoFrameSize'] = $oEmbed->height . "x" . $oEmbed->width . "px";

Stage 3:

use the variables in your coding as you feel best to use::

madesignUK
  • 71
  • 2
  • It does not work. If i upload a video that is 300x250 px i get: {"type":"video","html":"\u003ciframe .... "height":344,"width":459...} No where does it say 300x250, and 459x344 is not the same proportion (width/height ratio) as 300x250 – Jakob Sternberg Mar 09 '17 at 14:15