使用yt_dlp在discord.py中播放一首歌。

huangapple go评论103阅读模式
英文:

Using yt_dlp in discord.py to play a song

问题

Youtube-dl突然停止工作,并出现了上传者ID错误,所以在阅读了一些文章后,我决定使用yt-dlp作为替代,但当我尝试运行下面的命令时,它只给我一个"输出文件#0不包含任何流"的错误。这是我的代码:

  1. @commands.command()
  2. async def rick(self, ctx):
  3. voice_channel = ctx.author.voice.channel
  4. if ctx.voice_client is None:
  5. await voice_channel.connect()
  6. if ctx.voice_client.is_playing():
  7. await ctx.send("something is currently playing...")
  8. return
  9. FFMPEG_OPTIONS = {
  10. 'before_options':
  11. '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -probesize 200M',
  12. 'options': '-vn'
  13. }
  14. YDL_OPTIONS = { 'format': 'm4a/bestaudio/best',
  15. 'postprocessors': [{
  16. 'key': 'FFmpegExtractAudio',
  17. 'preferredcodec': 'm4a',}]
  18. }
  19. with yt_dlp.YoutubeDL(YDL_OPTIONS) as ydl:
  20. secret = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
  21. info = ydl.extract_info(secret, download=False)
  22. url2 = ydl.sanitize_info(info)
  23. url2 = info['formats'][0]['url']
  24. source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
  25. vc = ctx.voice_client
  26. vc.play(source)

我尝试更改了代码,但没有获得任何结果。阅读yt-dlp文档后,我尝试添加了"sanitize_info"模块,但仍然收到相同的错误。感谢帮助!

英文:

Youtube-dl suddenly stopped working and was giving me uploader id errors so after reading a few articles I decided to use yt-dlp as an alternative but as I try to run the command below it just gives me an "Output file #0 does not contain any stream" error. Here's my code:

  1. @commands.command()
  2. async def rick(self, ctx):
  3. voice_channel = ctx.author.voice.channel
  4. if ctx.voice_client is None:
  5. await voice_channel.connect()
  6. if ctx.voice_client.is_playing():
  7. await ctx.send("something is currently playing...")
  8. return
  9. FFMPEG_OPTIONS = {
  10. 'before_options':
  11. '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -probesize 200M',
  12. 'options': '-vn'
  13. }
  14. YDL_OPTIONS = { 'format': 'm4a/bestaudio/best',
  15. 'postprocessors': [{
  16. 'key': 'FFmpegExtractAudio',
  17. 'preferredcodec': 'm4a',}]
  18. }
  19. with yt_dlp.YoutubeDL(YDL_OPTIONS) as ydl:
  20. secret = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
  21. info = ydl.extract_info(secret, download=False)
  22. url2 = ydl.sanitize_info(info)
  23. url2= info['formats'][0]['url']
  24. source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
  25. vc = ctx.voice_client
  26. vc.play(source)

I've tried changing the code but I wasn't getting any results. Reading the yt-dlp documentation I tried adding the sanitize info module but i was still getting the same error. Thanks for the help!

答案1

得分: 1

已解决!只需更改我的ydl选项。

  1. @commands.command()
  2. async def rick(self, ctx):
  3. voice_channel = ctx.author.voice.channel
  4. if ctx.voice_client is None:
  5. await voice_channel.connect()
  6. if ctx.voice_client.is_playing():
  7. await ctx.send("something is currently playing...")
  8. return
  9. FFMPEG_OPTIONS = {
  10. 'before_options':
  11. '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -probesize 200M',
  12. 'options': '-vn'
  13. }
  14. ydl_opts = {
  15. 'format': 'bestaudio/best',
  16. 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
  17. 'quiet': True,
  18. 'postprocessors': [{
  19. 'key': 'FFmpegExtractAudio',
  20. 'preferredcodec': 'mp3',
  21. 'preferredquality': '192',
  22. }],
  23. }
  24. with yt_dlp.YoutubeDL(ydl_opts) as ydl:
  25. secret = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
  26. info = ydl.extract_info(secret, download=False)
  27. url2 = info['url']
  28. print(url2)
  29. source = discord.FFmpegPCMAudio(url2)
  30. vc = ctx.voice_client
  31. vc.play(source)
英文:

Solved it!
just had to change my ydl options

  1. @commands.command()
  2. async def rick(self, ctx):
  3. voice_channel = ctx.author.voice.channel
  4. if ctx.voice_client is None:
  5. await voice_channel.connect()
  6. if ctx.voice_client.is_playing():
  7. await ctx.send("something is currently playing...")
  8. return
  9. FFMPEG_OPTIONS = {
  10. 'before_options':
  11. '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -probesize 200M',
  12. 'options': '-vn'
  13. }
  14. ydl_opts = {
  15. 'format': 'bestaudio/best',
  16. 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
  17. 'quiet': True,
  18. 'postprocessors': [{
  19. 'key': 'FFmpegExtractAudio',
  20. 'preferredcodec': 'mp3',
  21. 'preferredquality': '192',
  22. }],
  23. }
  24. with yt_dlp.YoutubeDL(ydl_opts) as ydl:
  25. secret = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
  26. info = ydl.extract_info(secret, download=False)
  27. url2 = info['url']
  28. print(url2)
  29. source = discord.FFmpegPCMAudio(url2)
  30. vc = ctx.voice_client
  31. vc.play(source)

huangapple
  • 本文由 发表于 2023年3月9日 14:08:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定