在 discord.py 中使用 ffmpeg 库时出现 OpusError

问题描述

在播放本地文件或 YouTube 音频时,我收到 OpusError: invalid argument 错误

播放命令如下所示:

# bot.py
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
from models import *
from discord.voice_client import VoiceClient
import logging
from config import *
import youtube_dl
from youtube_dl import YoutubedL

@bot.command()
async def play(ctx):
    user=ctx.author
    voice_channel=user.voice.channel
    channel=None
    # only play music if user is in a voice channel
    if voice_channel:
        # grab user's voice channel
        channel=voice_channel.name
        await ctx.send('User is in channel: '+ channel)
        # create StreamPlayer
        vc = await voice_channel.connect()
        url="https://www.youtube.com/watch?v=AOeY-nDp7hI"
        YDL_OPTIONS = {'format': 'bestaudio','noplaylist':'True'}
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options': '-vn'}
        voice = discord.utils.get(bot.voice_clients,guild=ctx.guild)
        print(0)
        if not voice.is_playing():
            print("1")
            with YoutubedL(YDL_OPTIONS) as ydl:
                info = ydl.extract_info(url,download=False)
                print(2)
            URL = info['formats'][0]['url']
            print(3)
            voice.play(discord.FFmpegPCMAudio(URL,**FFMPEG_OPTIONS))
            print(4)
            voice.is_playing()
            print("Strated playing")
        else:
            await ctx.send("Already playing song")
            return
    else:
        await ctx.send('User is not in a channel.')

我的错误

0
1
[youtube] AOeY-nDp7hI: Downloading webpage
[youtube] Downloading just video AOeY-nDp7hI because of --no-playlist
2
3
Ignoring exception in command play:
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.9/site-packages/discord/ext/commands/core.py",line 85,in wrapped
    ret = await coro(*args,**kwargs)
  File "/Users/mohit/programming/discord/area-51-helper/main.py",line 115,in play
    voice.play(discord.FFmpegPCMAudio(URL,**FFMPEG_OPTIONS))
  File "/opt/homebrew/lib/python3.9/site-packages/discord/voice_client.py",line 564,in play
    self.encoder = opus.Encoder()
  File "/opt/homebrew/lib/python3.9/site-packages/discord/opus.py",line 291,in __init__
    self.set_fec(True)
  File "/opt/homebrew/lib/python3.9/site-packages/discord/opus.py",line 326,in set_fec
    _lib.opus_encoder_ctl(self._state,CTL_SET_FEC,1 if enabled else 0)
  File "/opt/homebrew/lib/python3.9/site-packages/discord/opus.py",line 92,in _err_lt
    raise OpusError(result)
discord.opus.OpusError: invalid argument

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.9/site-packages/discord/ext/commands/bot.py",line 939,in invoke
    await ctx.command.invoke(ctx)
  File "/opt/homebrew/lib/python3.9/site-packages/discord/ext/commands/core.py",line 863,in invoke
    await injected(*ctx.args,**ctx.kwargs)
  File "/opt/homebrew/lib/python3.9/site-packages/discord/ext/commands/core.py",line 94,in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: OpusError: invalid argument

我正在使用这些版本

discord.py==1.0.1
ffmpeg==4.3.2
youtube_dl==2021.4.17

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...