-
-
Save ramonvictor/5629626 to your computer and use it in GitHub Desktop.
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 ); | |
}); | |
} |
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.
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!
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"
}