如何解决在 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 举报,一经查实,本站将立刻删除。