vue-youtube-embed
vue-youtube-embed copied to clipboard
Can I get the total duration of the video?
I want to get the total duration of the video given in the Id. I dont see any method right now to get the total duration, is there any way to get it?
@VigneshVaidyanathan I ended up grabbing this separately from a fetch request to the YouTube API. The following returns an object with a lot of information you can use, including thumbnails and stats:
https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics,status&key=[YOUR_KEY_HERE]&id=[VIDEO_ID]
Note: video_id can also be a comma separated list of ids if you want to grab multiple.
Also when accessing the response object, response.contentDetails.duration will give you a string like this PT3H10M9S etc
I wrote this function to make use of it like this: "3:10:09"
function duration(response) {
const str = response.contentDetails.duration;
const h = bt(str, 'PT', 'H');
const m = bt(str, 'PT', 'M') || bt(str, 'H', 'M') || '00';
const s = bt(str, 'M', 'S') || bt(str, 'H', 'S') || bt(str, 'PT', 'S') || '00';
let final = '';
if (h) final += `${h}:`;
if (m) final += `${m}:`;
final += ('00'+s).slice(-2);
function bt(str, start, end) { // getValBetween
const tmpStr = str.match(new RegExp(start + "(.*)" + end));
return tmpStr && tmpStr[1];
}
return final;
}