Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I get the total duration of the video? #60

Open
VigneshVaidyanathan opened this issue Jul 12, 2019 · 2 comments
Open

Can I get the total duration of the video? #60

VigneshVaidyanathan opened this issue Jul 12, 2019 · 2 comments

Comments

@VigneshVaidyanathan
Copy link

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?

@liquidvisual
Copy link

liquidvisual commented Aug 29, 2019

@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.

@liquidvisual
Copy link

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants