python - YouTube-dl 和 Ffmpeg

标签 python ffmpeg discord.py youtube-dl

我用discord.py制作了一个音乐机器人,但我得到了这个信息,但它不再工作错误[youtube] QxYdBvB8sOY:下载网页985104597242773505 [2022-06-25 07:45:51] [ INFO ] Discord.player:准备终止 ffmpeg 进程 37580。 [2022-06-25 07:45:51] [INFO ] Discord.player:ffmpeg 进程 37580 尚未终止。等待终止... [2022-06-25 07:45:51] [INFO ]discord.player: ffmpeg 进程 37580 应该终止,返回码为 1。 这是我的代码

import discord
import os
import asyncio
import youtube_dl
from discord import *

token = "token is here"
prefix = "j!"
blocked_words = ["blocked words are here"]

voice_clients = {}

yt_dl_opts = {'format': 'bestaudio/best'}
ytdl = youtube_dl.YoutubeDL(yt_dl_opts)

ffmpeg_options = {'options': "-vn"}

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)


programmer_role = "987018590152699964"
            


@client.event
async def on_ready():
    print(f"Bot logged in as {client.user}")

@client.event
async def on_message(msg):
    if msg.author != client.user:
        if msg.content.lower().startswith(f"{prefix}info"):
            await msg.channel.send(f"Hi, Im JoksysBot Made By Joksy!")

        for text in blocked_words:
            if text in str(msg.content.lower()):
                await msg.delete()
                await msg.channel.send("Hey, Dont Say That!")
                return
        if msg.content.startswith(f"{prefix}play"):

            try:
                voice_client = await msg.author.voice.channel.connect()
                voice_clients[voice_client.guild.id] = voice_client
            except:
                print("error")

            try:
                url = msg.content.split()[1]

                loop = asyncio.get_event_loop()
                data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))

                song = data['url']
                player = discord.FFmpegPCMAudio(song, **ffmpeg_options, executable="C:\\Users\\jonas\\Documents\\ffmpeg-2022-06-16-git-5242ede48d-full_build\\ffmpeg-2022-06-16-git-5242ede48d-full_build\\bin\\ffmpeg.exe")

                voice_clients[msg.guild.id].play(player)

            except Exception as err:
                print(err)

        if msg.content.startswith(f"{prefix}pause"):
            try:
                voice_clients[msg.guild.id].pause()
            except Exception as err:
                print(err)

        if msg.content.startswith(f"{prefix}resume"):
            try:
                voice_clients[msg.guild.id].resume()
            except Exception as err:
                print(err)

        if msg.content.startswith(f"{prefix}stop"):
            try:
                voice_clients[msg.guild.id].stop()
                await voice_clients[msg.guild.id].disconnect()
            except Exception as err:
                print(err)

client.run(token)

这很奇怪,因为我的机器人中的所有其他代码都像 !info 命令一样工作正常,所以它一定是 youtube-dl 或 ffmpeg 的错误。但话又说回来,它一开始就没有加入语音通话,所以这可能是错误所在。我将 ffmpeg 添加到路径,但我仍然在这里写了它的路径 player = discord.FFmpegPCMAudio(song, **ffmpeg_options,executable="C:\\Users\\jonas\\Documents\\ffmpeg-2022-06 -16-git-5242ede48d-full_build\\ffmpeg-2022-06-16-git-5242ede48d-full_build\\bin\\ffmpeg.exe”)。我按照这个机器人教程 https://www.youtube.com/watch?v=Q8wxin72h50&t=1040s我做了他所做的一切,但没有成功。我的discord.py版本是2.0.0,我的Python版本是3.10.5,我的youtube_dl版本是2021.12.17,我的ffmpeg下载是ffmpeg-2022-06-16-git-5242ede48d-full_build。我在 discord.py 1.73 上测试了它,效果很好。这是在 intellij 中,而我的主程序是在 Visual Studio Code 中,但我看不出它有什么大的区别,所以它可能是使程序无法工作的意图。我看不到代码中的任何错误,但我对不和谐感到陌生。 py、youtube_dl 和 ffmpeg 的东西,所以除非 Visual Studio 代码告诉我我做错了什么,否则我不会注意到。但我做错了什么以及如何解决它?

最佳答案

对于任何遇到此问题的人来说:问题出在语音通话连接上。 确保您有这些意图

intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True

如果它仍然不起作用请确保您已安装PyNaCl。您可以在终端中使用以下代码pip install pynacl来完成此操作您需要为此安装python。这应该是解决办法

关于python - YouTube-dl 和 Ffmpeg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72751700/

相关文章:

python - Tensorflow 中的加权 Mape

python - 准备用于生产的 Pyramid 应用程序

ffmpeg - 如何使用 ffmpeg 加密 AES-128 HLS m3u8 播放列表?

python - 有没有办法在discord.py中接收其他用户给我的建议

Python-将 channel 添加到类别

python - 使用 youtube_dl 在 Discord.py bot 上播放音乐时收到错误代码 1

python - Pygame (python) 中翻转图像的问题

python - 停止 Python 函数覆盖输入

audio - FFMPEG 改变音调频率(音高音频)

ffmpeg - 如何从 ffmpeg 中的流中检索 HTTP header ?