I'm creating a bot in Discord.JS (it's a library for Node.JS) and I would like to add a music module to it. Discord.JS can play videos in formats like .wav, .mp4 etc. in voice channels. I would like to add a possibility for users to just type a YouTube url of a video to play it. Is there any possibility to get a video's url from an YouTube url by an API or any other way?
问题:
回答1:
You used to be able to get it pretty easily with youtube.com/get_video?video_id=...
but that was like 5+ years ago, now I think it uses some weird stream thing.
I don't think Youtube's official API lets you download videos or get a URL. Also, you should probably take a look at section 5B of the YouTube TOS
You shall not download any Content unless you see a “download” or similar link displayed by YouTube on the Service for that Content.
But anyway, one method I know is youtube-dl, but I'm pretty sure it's only for python. There seem to be a few node wrappers online though (ex.)
I've also found a node.js youtube downloader, ytdl. Last update was 9 days ago so it should probably be working fine with the current system. You should be able to use -o filename
or --output filename
to download it, or --print-url
to get a direct video url.
回答2:
You could use FFMPEG to download video and then play that file