simple-youtube-api icon indicating copy to clipboard operation
simple-youtube-api copied to clipboard

Videos from playlists have no duration object

Open ggtylerr opened this issue 4 years ago • 4 comments

Explanation

Any video from a playlist does not include the duration object or the durationSeconds variable.

Reproduction

  1. Get a playlist
  2. Get its videos
  3. Try and get duration
  4. Returns undefined (or -1 if using durationSeconds)

Example

const YouTube = require('simple-youtube-api');
const yt = new YouTube(process.env.yt);

yt.getVideo('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
  .then(vid => {
    console.log(`This video's duration is ${vid.duration.minutes}:${vid.duration.seconds}, which is ${vid.durationSeconds} in seconds.`);
  })
  .catch(console.error);

yt.getPlaylist('https://www.youtube.com/playlist?list=PLFMaGYddBw_Majo82Cdhc_QKFlxXDyL8E')
  .then(pl => {
    pl.getVideos()
      .then(vids => {
        const vid = vids[0];
        console.log(`This playlist video's duration is ${vid.duration}, which is ${vid.durationSeconds} in seconds.`);
      })
      .catch(console.error);
  })
  .catch(console.error);

Running this will return:

This video's duration is 3:33, which is 213 in seconds.
This playlist video's duration is undefined, which is -1 in seconds.

ggtylerr avatar Jun 22 '21 23:06 ggtylerr

can you check what vids[0].full is equal to? i suspect that this is because you don't have a full video object

mgray-online avatar Jun 23 '21 04:06 mgray-online

Returns false. ...why does it return without the full object, is that a limit of the API?

ggtylerr avatar Jun 23 '21 19:06 ggtylerr

I believe this is just to save on data usage, but i could be wrong. To get the duration you need to do vids[0].fetch() then get the duration from the result of that

mgray-online avatar Jun 24 '21 00:06 mgray-online

That's the current workaround I'm thinking of right now. What I'm trying to do though (get all videos from a playlist and save them to a queue, for a discord music bot) would require fetching every single video individually all at once, which will obviously hit the rate limit pretty quickly. Having this in the playlist object would very easily fix this problem.

ggtylerr avatar Jun 24 '21 03:06 ggtylerr