Skip to content

Instantly share code, notes, and snippets.

@ramonvictor
Last active December 9, 2020 15:43
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ramonvictor/5629626 to your computer and use it in GitHub Desktop.
Save ramonvictor/5629626 to your computer and use it in GitHub Desktop.
Javascript/jQuery function to get youtube video information by passing video url.
var getYoutubeIdByUrl = function( url ){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
if(match&&match[7].length==11){
return match[7];
}
return false;
};
var formatSecondsAsTime = function( secs ) {
var hr = Math.floor(secs / 3600),
min = Math.floor((secs - (hr * 3600)) / 60),
sec = Math.floor(secs - (hr * 3600) - (min * 60));
if (hr < 10) {
hr = "0" + hr;
}
if (min < 10) {
min = "0" + min;
}
if (sec < 10) {
sec = "0" + sec;
}
if (hr) {
hr = "00";
}
return hr + ':' + min + ':' + sec;
};
/*
* using 'video_url' (dynamic information) to get youtube video basic informations (title, duration).
*/
var yt_video_id = getYoutubeIdByUrl( video_url );
if( yt_video_id ){
$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+ yt_video_id +'?v=2&alt=jsonc', function(data,status,xhr){
var yt_response = data.data, // If you need more video informations, take a look on this response: data.data
yt_title = yt_response.title,
yt_duration = formatSecondsAsTime( yt_response.duration );
});
}
@ashrafreda
Copy link

how can I output the yt_duration out of $.json method

@nazikus
Copy link

nazikus commented Jun 26, 2016

this api is no longer available :(
{"apiVersion":"2.1","error":{"code":410,"message":"No longer available","errors":[{"domain":"GData","code":"NoLongerAvailableException","internalReason":"No longer available"}]}}

is it still possible to get video info without a key required in v3 api?

@chadsteele
Copy link

You'll need an API key for most everything in V3. Sorry. And you'll need to parse the duration. In this example, PT8M18S stands for 8 minutes and 18 seconds

e.g.
https://www.googleapis.com/youtube/v3/videos?part=contentDetails&key=YOUR-API-KEY&id=YOUR-VID

"contentDetails": {
"duration": "PT8M18S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": true,
"projection": "rectangular"
}

@walalm
Copy link

walalm commented Apr 12, 2020

You'll need an API key for most everything in V3. Sorry. And you'll need to parse the duration. In this example, PT8M18S stands for 8 minutes and 18 seconds

e.g.
https://www.googleapis.com/youtube/v3/videos?part=contentDetails&key=YOUR-API-KEY&id=YOUR-VID

"contentDetails": {
"duration": "PT8M18S",
"dimension": "2d",
"definition": "hd",
"caption": "false",
"licensedContent": true,
"projection": "rectangular"
}

Thank you, I was trying to solve a problem and with this info was possible.

@jojo2357
Copy link

jojo2357 commented Dec 3, 2020

Please correct me if I am wrong but I don't think that formatSecondsAsTime works as intended.

  if (hr) {
      hr = "00";
  }

should be omitted as it will always return zero hours. It could be changed to if (!hr) but it will always be defined and then i would ask why minutes doesnt also have the same feature.

cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment