museofficial / muse

🎧 a self-hosted midwestern Discord music bot that doesn't suck
MIT License
863 stars 224 forks source link

Age Restricted Youtube Video Support #997

Open bencorryp opened 7 months ago

bencorryp commented 7 months ago

Is it possible to allow the bot to search (and play) age-restricted content on youtube? Could not find a setting on the generated API key to allow for age restricted content to be viewed.

codetheweb commented 7 months ago

No, but happy to accept a PR for this.

jogerj commented 4 months ago

Afaik there's no way of directly accessing restricted videos from the YouTube API without being logged in. In other words, you'll have to pass a YouTube account credentials by exporting your browser cookies and passing it to the client. This incidentally also allows accessing private/membership required videos/streams as long as the credentials allows it.

Quick search through code tells me you need to pass the cookie as a header here https://github.com/codetheweb/muse/blob/cc1f18bc196fe1b5acb6023dc23ab42f43b41ba7/src/services/youtube-api.ts#L67-L73

and the generated header should look something like this

Happyllama25 commented 4 months ago

What about the ytdl library? I've used ytdlp (Python fork) in some personal projects and age-restricted video downloads work as intended. Granted, I'm not sure how they do it, but it's probably not account credentials.

jogerj commented 4 months ago

@Happyllama25 I've worked with yt-dlp before. Passing cookies to the client is the way. It's straight forward in code too, but since this project is written in typescript and doesn't rely on yt-dlp at all, we'll have to do on our own. But the proof of concept above should work.

Edit: you may have been on to something. It does work, sort-of by spoofing a TV client (?) Discussion on reddit here but apparently it's not foolproof if the owner of the video disables the option

Happyllama25 commented 4 months ago

It does work, sort-of

Yes, it works for me on my bot, something I did notice though is it seems to take longer to download an age-restricted video than a non-restricted one - not 100% sure, I have not done a 1:1 test of this.

image