Return video duration from Youtube API using JSON Return video duration from Youtube API using JSON json json

Return video duration from Youtube API using JSON


The YouTube API only outputs the duration in seconds. But you can create a function to convert seconds into the time format you want.

function formatSecondsAsTime(secs) {    var hr = Math.floor(secs / 3600);    var min = Math.floor((secs - (hr * 3600)) / 60);    var 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;}

Just pass the duration field to the function as so:

var videoTime = formatSecondsAsTime(entries[i].media$group.media$content[0].duration);