47

So I am trying to get information from a single youtube video via in the JSON format. Like title description category, ect whatever I can get besides the comments. I am trying to do this in Javascript. I noticed the link below but all of their examples are how to get video information from feeds. I would like to get the information from a single video assuming i know its ID.

https://developers.google.com/youtube/2.0/developers_guide_json

I was also looking at this Stackoverflow Question but I have an issue with the get request it says "test.js (line 10) GET http://gdata.youtube.com/feeds/api/videos/VA770w...v=2&alt=json-in-script&callback=listInfo

200 OK 9ms"

In brief, if i have a single youtube videos id like VA770wpLX-Q, what would the url look like to get that videos information in JSON?

Thank you

Community
  • 1
  • 1
Frank Visaggio
  • 3,642
  • 9
  • 34
  • 71

4 Answers4

71

UPDATED 2018

API v2 deprecated. New youtube api v3 works only with developer token and has limitation for free connections.

You can get JSON without API:

http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=ojCkgU5XGdg&format=json

Or xml

http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=ojCkgU5XGdg&format=xml



new 2018 json response has

{
  "html": "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/ojCkgU5XGdg?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe>",
  "title": "Creativity and Drugs (Eng Sub)",
  "thumbnail_height": 360,
  "provider_name": "YouTube",
  "author_url": "https://www.youtube.com/user/serebniti",
  "thumbnail_width": 480,
  "height": 270,
  "provider_url": "https://www.youtube.com/",
  "type": "video",
  "width": 480,
  "version": "1.0",
  "author_name": "serebniti",
  "thumbnail_url": "https://i.ytimg.com/vi/ojCkgU5XGdg/hqdefault.jpg"
}

Thumbs:

hqdefault.jpg has less quality but always exist.

http://img.youtube.com/vi/ojCkgU5XGdg/hqdefault.jpg

http://img.youtube.com/vi/ojCkgU5XGdg/sddefault.jpg

Max size

https://i.ytimg.com/vi/ojCkgU5XGdg/maxresdefault.jpg

Mini thumbs:

http://img.youtube.com/vi/ojCkgU5XGdg/0.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/1.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/2.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/3.jpg

Annotations

http://www.youtube.com/annotations_invideo?cap_hist=1&video_id=ojCkgU5XGdg

parse mobile page 16kb

https://m.youtube.com/watch?v=ojCkgU5XGdg

don't forget change user agent to iOS / Safari 7

also

http://www.youtube.com/get_video_info?html5=1&video_id=ojCkgU5XGdg

also how embed youtube live

https://www.youtube.com/embed/live_stream?channel=UCkA21M22vGK9GtAvq3DvSlA

Where UCkA21M22vGK9GtAvq3DvSlA is your channel id. You can find it inside youtube account on "My Channel" link.

Live thumb

https://i.ytimg.com/vi/W-fSCPrYSL8/hqdefault_live.jpg
Alexufo
  • 1,723
  • 1
  • 15
  • 30
38

UPDATE MAY/2015:

This solution doesn't work properly, YouTube API v2 is in process to be discontinued soon.

More info at: https://www.youtube.com/devicesupport


Try something like this:

var video_id='VA770wpLX-Q';

$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){
    alert(data.data.title);
    // data contains the JSON-Object below
});

Demo: http://jsfiddle.net/wqwxg/

The returned JSON looks like this:

{
    "apiVersion": "2.1",
    "data": {
        "id": "VA770wpLX-Q",
        "uploaded": "2011-02-24T22:31:02.000Z",
        "updated": "2012-04-08T21:37:06.000Z",
        "uploader": "drdrevevo",
        "category": "Music",
        "title": "Dr. Dre - I Need A Doctor (Explicit) ft. Eminem, Skylar Grey",
        "description": "Music video by Dr. Dre performing I Need A Doctor featuring Eminem and Skylar Grey (Explicit). © 2011 Aftermath Records",
        "tags": ["Dr", "Dre", "Eminem", "New", "Song", "Skylar", "Grey", "GRAMMYs", "Dr.", "Need", "Doctor", "video", "Eazy", "N.W.A.", "NWA", "easy", "drdre", "and", "em"],
        "thumbnail": {
            "sqDefault": "http://i.ytimg.com/vi/VA770wpLX-Q/default.jpg",
            "hqDefault": "http://i.ytimg.com/vi/VA770wpLX-Q/hqdefault.jpg"
        },
        "player": {
            "default": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player"
        },
        "content": {
            "5": "http://www.youtube.com/v/VA770wpLX-Q?version=3&f=videos&app=youtube_gdata"
        },
        "duration": 457,
        "aspectRatio": "widescreen",
        "rating": 4.902695,
        "likeCount": "430519",
        "ratingCount": 441253,
        "viewCount": 88270796,
        "favoriteCount": 306556,
        "commentCount": 270597,
        "status": {
            "value": "restricted",
            "reason": "requesterRegion"
        },
        "restrictions": [{
            "type": "country",
            "relationship": "deny",
            "countries": "DE"
        }],
        "accessControl": {
            "comment": "allowed",
            "commentVote": "allowed",
            "videoRespond": "allowed",
            "rate": "allowed",
            "embed": "allowed",
            "list": "allowed",
            "autoPlay": "denied",
            "syndicate": "allowed"
        }
    }
}
Rafael Moni
  • 656
  • 1
  • 10
  • 22
stewe
  • 41,820
  • 13
  • 79
  • 75
  • 1
    url = "http://gdata.youtube.com/feeds/api/videos/NWHfY_lvKIQ?v=2&alt=json" thats how you do it. NWHfY_lvKIQ is the ID of the video. after that you can do a $.getJSON(url, function(data){ and get the json from there – Frank Visaggio Apr 08 '12 at 21:52
  • I want to ask how to get the duration in format HH:MM:ss because in json file it is in the Seconds ,is there any function can do this already!! – Tolen Aug 29 '13 at 13:22
  • 8
    Not supported anymore `"error":{"code":410,"message":"No longer available"` – Jasom Dotnet Oct 28 '15 at 13:55
20

Problem

YouTube API doesn't support JSONP as it should - see Issue 4329: oEmbed callback for JSONP. Also, YouTube Data API v2 is deprecated.

Solution

You can use the Noembed service to get oEmbed data with JSONP for YouTube videos.

Bonus

  • no API keys are needed
  • no server-side proxy is required

Example

For your VA770wpLX-Q video, you can try a link like this:

https://noembed.com/embed?url=http://www.youtube.com/watch?v=VA770wpLX-Q

Or this for JSONP:

https://noembed.com/embed?callback=example&url=http://www.youtube.com/watch?v=VA770wpLX-Q

Those links have a standard URL of a YouTube video passed as the url parameter. It works not only with YouTube but also with Vimeo and other sites with URLs like:

https://noembed.com/embed?url=https://vimeo.com/45196609

Demo

Here is a simple example using jQuery:

var id = 'VA770wpLX-Q';
var url = 'https://www.youtube.com/watch?v=' + id;

$.getJSON('https://noembed.com/embed',
    {format: 'json', url: url}, function (data) {
    alert(data.title);
});

See: DEMO on JS Bin.

Other options

  • Embedly (commercial service, free up to 5000 URLs/month)
  • Oohembed (update: now bought by Embedly, but the source is available)
  • AutoEmbed (update: seems to be down or discontinued)

More info

See also those questions:

rsp
  • 107,747
  • 29
  • 201
  • 177
  • @BobSinclar Thanks. It also works for Vimeo and other sites, see: https://noembed.com/#supported-sites – rsp Aug 25 '15 at 08:09
  • @KenSharp If the **first** party can't be trusted ([API v3 doesn't support JSONP](https://code.google.com/p/gdata-issues/issues/detail?id=4329) and [API v2 is deprecated](https://developers.google.com/youtube/2.0/deprecation_faq)) then we can either tell the original poster that it can't be done **or** we can use a third party service that works for what was asked in the question. But if you have a better solution then please post an answer. – rsp Feb 25 '16 at 23:56
  • Is it known how Noembed does it? – jakob.j Jan 23 '17 at 18:32
  • @jakob.j It's open source. So, yes. – yckart Feb 20 '17 at 21:07
10

in v3:

$.getJSON('https://www.googleapis.com/youtube/v3/videos?id={videoId}&key={myApiKey}&part=snippet&callback=?',function(data){

 if (typeof(data.items[0]) != "undefined") {
     console.log('video exists ' + data.items[0].snippet.title);
   } else {
     console.log('video not exists');
 }   
});

In response a @Jonathan via server side, using PHP and CURL:

$url = "https://www.googleapis.com/youtube/v3/videos?id=".$videoId."&key=".$miApikey."&part=snippet";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$output=curl_exec($ch);                 
$response = json_decode($output, TRUE);
print_r($response);
  • 2
    doesn't seem a good idea to show your key like that. It is better to call your server side script by ajax that fetchs that url – Jonathan Jun 26 '15 at 21:34
  • 2
    @Jonathan Google provides an API key like this so that you can do it publicly. The restrictions are set Google-side. This is the correct way to do what is needed. The API key cannot be used outside of the domains that you allow via the console. It's all made very clear in the console. – Ken Sharp Feb 22 '16 at 00:41