Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 250
Frank Visaggio Avatar asked Apr 08 '12 21:04

Frank Visaggio


3 Answers

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
like image 111
Alexufo Avatar answered Oct 17 '22 05:10

Alexufo


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"
        }
    }
}
like image 38
stewe Avatar answered Oct 17 '22 06:10

stewe


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:

  • Youtube Video title with API v3 without API key?
  • Response for JSONp request to youtube oembed call giving "invalid label" error
like image 20
rsp Avatar answered Oct 17 '22 06:10

rsp