如何从YouTube将视频发送到Discord Bot Python

问题描述

我正在使用python discord bot。我需要从YouTube发送视频。我只有这个影片的网址。请帮帮我。

解决方法

假定不符合8mb的文件限制。然后,您想先下载youtube视频,然后在不和谐频道上发送它。您将需要pytube。

pip install pytube
from pytube import YouTube 
  
#where to save 
SAVE_PATH = "E:/" #to_do 
  
#link of the video to be downloaded 
link="https://www.youtube.com/"
  
try: 
    #object creation using YouTube which was imported in the beginning 
    yt = YouTube(link) 
except: 
    print("Connection Error") #to handle exception 
  
#filters out all the files with "mp4" extension 
mp4files = yt.filter('mp4') 
  
yt.set_filename('trial') #to set the name of the file 
  
#get the video with the extension and resolution passed in the get() function 
d_video = yt.get(mp4files[-1].extension,mp4files[-1].resolution) 
try: 
    #downloading the video 
    d_video.download(SAVE_PATH) 
except: 
    print("Some Error!") 
print('Task Completed!')

和您的discord.py文件上:

@bot.command(pass_context=True)
async def send(ctx):
    area=ctx.message.channel
    await bot.send_file(area,r"c:\location\of\the_file_to\send.png",filename="Trial",content="Message test")