Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Created April 14, 2016 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmcli/2ecd80b65c9ac96439f4e9b12cb9ae9b to your computer and use it in GitHub Desktop.
Save adrianmcli/2ecd80b65c9ac96439f4e9b12cb9ae9b to your computer and use it in GitHub Desktop.
A function that returns the duration of a YouTube video from the YouTube API V3.
function fetchDuration(videoId, apiKey) {
const baseURL = 'https://www.googleapis.com/youtube/v3/videos?';
const url = `${baseURL}id=${videoId}&key=${apiKey}&part=snippet,contentDetails`;
$.ajax({
async: false,
type: 'GET',
url: url,
success: function(data) {
const item = data.items[0];
const result = item ? item.contentDetails.duration : null;
return result;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment