Get YouTube Video dimensions (width/height)

前端 未结 5 936
孤街浪徒
孤街浪徒 2020-12-06 01:08

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 vi

5条回答
  •  独厮守ぢ
    2020-12-06 01:41

    Update 2017 - The accepted answer no longer works

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

    • https://www.youtube.com/watch?v=z_AbfPXTKms

    Then try this URL, using Noembed:

    • https://noembed.com/embed?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dz_AbfPXTKms

    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\n",
      "thumbnail_url": "https://i.ytimg.com/vi/z_AbfPXTKms/hqdefault.jpg"
    }
    

    It also supports JSONP for cross-origin requests:

    • https://noembed.com/embed?callback=example&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dz_AbfPXTKms

    For more info see this answer:

    • Get Youtube information via JSON for single video (not feed) in Javascript

    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.

提交回复
热议问题