播放本地文件或 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