Luuk-Dev / YT-Stream

Create easily readable streams from YouTube video url's
MIT License
23 stars 4 forks source link

Get info about age-restricted videos #14

Closed m-milek closed 4 months ago

m-milek commented 4 months ago

Hello,

Is it possible to work with age-restricted videos? I need to fetch metadata and later download the audio.

When I try to getInfo those videos, I get

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error while getting video url
Sign in to confirm your age".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

I've checked closed issues and there was one very similar to mine. It seems that the previous fix doesn't always work?

Thank you for developing this library, it's working really well so far 👍🏻

Luuk-Dev commented 4 months ago

Hey m-milek!

Unfortunately it is not possible to get the metadata directly from an age-restricted video. There is a work-around though. You can get the metadata by setting a cookie of an account where it's age has been verified. You can set a cookie by using a custom agent. Here is a small example of how you can set a cookie:

const ytstream = require('yt-stream');

const agent = new ytstream.YTStreamAgent([{
    key: 'The key of the cookie',
    value: 'The value of the cookie',
    domain: 'youtube.com', // Do not change the domain
    expires: 'Infinity',
    sameSite: 'lax',
    httpOnly: false,
    hostOnly: false,
    secure: true,
    path: '/'
}], {});

ytstream.setGlobalAgent(agent);

By setting this agent, all requests made to YouTube will include the cookies included inside the agent's first argument. Since the first argument is an array, you can add multiple cookies. To find the cookies of your account, you need to open YouTube in your browser, then open the inspector, select the 'Network' tab, refresh the page, open the first GET request to YouTube and go to the 'Cookies' tab inside the request.

Kind regards,

Luuk