Get Youtube information via JSON for single video (not feed) in Javascript
Solution 1
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"
}
}
}
Solution 2
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
Solution 3
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
Solution 4
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);
Related videos on Youtube
Frank Visaggio
Sr. Staff Engineer, Human Factors + User Experience Design. Formerly Software Developer / Cloud Architect with a passion for Human-Computer Interaction.
Updated on July 05, 2020Comments
-
Frank Visaggio almost 4 years
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
-
DG3 about 12 yearspost your jquery code you have tried
-
Frank Visaggio over 11 yearshey jquery wasnt my issue i just couldnt figure out the right url or pattern to get the json data. The answer below pointed me in the right direction
-
-
Frank Visaggio about 12 yearsurl = "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
-
Tolen over 10 yearsI 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!!
-
Nishan about 9 yearsSadly oembed does not support jsonp (i.e. with callback), so using it cross-domain in the browser will be a problem.
-
Alexufo almost 9 years@BlaM i can load it with jsonp learn.jquery.com/ajax/working-with-jsonp but jQuery cant parse response. :-(
-
Nishan almost 9 yearsThat's what I meant. The YouTube server does not support to send JSONP for those oembeds.
-
Jonathan almost 9 yearsdoesn'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
-
rsp over 8 years@BobSinclar Thanks. It also works for Vimeo and other sites, see: noembed.com/#supported-sites
-
Jasom Dotnet over 8 yearsNot supported anymore
"error":{"code":410,"message":"No longer available"
-
Ken Sharp about 8 years@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.
-
rsp about 8 years@KenSharp If the first party can't be trusted (API v3 doesn't support JSONP and API v2 is deprecated) 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.
-
jakob.j over 7 yearsIs it known how Noembed does it?
-
yckart about 7 years@jakob.j It's open source. So, yes.
-
machineaddict over 5 years@Alexufo you can use your website backend to proxy that connection. this way it will work
-
EricZhao over 4 yearsHowever there is no duration info