YDL 卡在下载中,discord.py

YDL stuck at downloading, discord.py

到目前为止,我一直在努力使我的这部分代码能够正常工作 2 天。我试图搜索类似的问题,但 none 的答案有效。

@commands.command()
async def play(self, ctx, url):
    ctx.voice_client.stop()
    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': 'vn'}
    YDL_OPTIONS = {'format': 'bestaudio', 'extractaudio': True, 'audioformat' : 'mp3'}
    vc = ctx.voice_client

    with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
        info = ydl.extract_info(url, download = False)
        url2 = info['formats'][0]['url']
        source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
        vc.play(source)

嘿,我看到你只是想播放这首歌,这是我发现的一些可能有用的代码。试试吧!

async def play(self, ctx, url):
  ffmpeg_options = {
      'options': '-vn',
      "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"
  }
  ytdlopts = {
      'format': 'bestaudio/best',
      'outtmpl': 'downloads/%(extractor)s-%(id)s-%(title)s.%(ext)s',
      'restrictfilenames': True,
      'noplaylist': True,
      'nocheckcertificate': True,
      'ignoreerrors': False,
      'logtostderr': False,
      'quiet': True,
      'no_warnings': True,
      'default_search': 'auto',
      'source_address': '0.0.0.0'
  }
  vc = ctx.voice_client
  with youtube_dl.YoutubeDL(ytdlopts) as ydl:
    info = ydl.extract_info(url, download = False)
    url2 = info['formats'][0]['url']
    source = discord.FFmpegPCMAudio(url2, **ffmpeg_options)
    vc.play(source)

您可能需要解决一些问题,但这应该可行

YouTube DL 不是 Music Disocrd 机器人的最佳选择。我建议使用 Wavelink. You can create your own commands (in or not in a Cog) or you can use dismusic

以下示例将 wavelink 与名为 dismusic

的预制音乐齿轮一起使用
# 1 - Install ffmpeg (and add to path)
#        Debian/Ubuntu -
#           apt install ffmpeg -y   - On Ubuntu
#        Windows -
#           Download ffmpeg, add to PATH or copy the ffmpeg.exe to System32 Folder

# 2 - Install discord, wavelink, lavalink, PyNacl and dismusic with the command below
#        Debian/Ubuntu -
#           pip3 install lavalink wavelink discord PyNacl dismusic
#        Windows -
#           pip install lavalink wavelink discord PyNacl dismusic

# Code for a basic discord bot
from discord.ext import commands
client = commands.Bot(command_prefix='--')

@client.event
async def on_ready():
    print('+ Logged in as {0.user}'.format(bot))

# Lavalink node to connect to
client.lava_nodes = [
    {
        # a free public node
        # ----------------------
        # 'host': "lava.link", 
        # 'port': 80,
        # 'rest_uri': f'http://lava.link:80',
        # 'password': 'anyPassword',

        # for hosting your own Lavalink locally (More stable)
        # Follow the steps below this code snippet only if you are doing this
        # 
        # ----------------------
        'host': "127.0.0.1",
        'port': 2333,
        'rest_uri': f'http://127.0.0.1:2333',
        'password': 'youshallnotpass',

        # Other Settings
        # ----------------------
        'identifier': 'MAIN',
        'region': 'singapore'
    }
]

# loading the installed, pre-made music cog to the bot
client.load_extension('dismusic')

# Running the bot
bot.run("DoNotShareYourTOKEN")

如果您选择 select 免费 public lava.link 服务器 (http://lava.link:80),您可以跳过下面的步骤

如果你使用 Debain/Ubuntu

,你可以 运行 下面的命令逐行启动你自己的 Lavalink 服务器
sudo apt install wget curl default-jdk -y
mkdir Lavalink && cd Lavalink # Optional
wget "https://github.com/freyacodes/Lavalink/releases/download/3.4/Lavalink.jar"
curl "https://raw.githubusercontent.com/hirusha-adi/Near/main/others/application.yml" >> "application.yml"
java -jar ./Lavalink.jar

如果您使用 windows 来托管 discord(这不是最好的主意,但如果您愿意的话),请按照 bel

中的步骤操作
  1. 安装 java 并添加到 PATH
  2. 下载Lavalink.jar and application.yml
  3. 将两个下载的文件放到同一个文件夹
  4. 在该文件夹中打开 CMD 或 Powershell,然后 运行 java -jar Lavalink.jar

可用命令 -
--play - 播放歌曲或播放列表
--pause - 暂停播放器
--connect - 连接到 vc
--seek - 寻找玩家
--nowplaying - 正在播放
--queue - 查看队列
--equalizer - 设置均衡器
--volume - 设置音量
--resume - 恢复播放器
--loop - 循环 song/playlist