YDL stuck at downloading, discord.py

Viewed 214

I've been trying to get this part of my code to work for like 2 days as of now. I've tried to search up for similar problems but none of the answers worked.

@commands.command()
async def play(self, ctx, url):
    ctx.voice_client.stop()
    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': 'vn'}
    YDL_OPTIONS = {'format': 'bestaudio', 'extractaudio': True, 'audioformat' : 'mp3'}
    vc = ctx.voice_client

    with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
        info = ydl.extract_info(url, download = False)
        url2 = info['formats'][0]['url']
        source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
        vc.play(source)
2 Answers

Hey there I see you are just trying to play the song, here is some code I found that may work. Try it!

async def play(self, ctx, url):
  ffmpeg_options = {
      'options': '-vn',
      "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"
  }
  ytdlopts = {
      'format': 'bestaudio/best',
      'outtmpl': 'downloads/%(extractor)s-%(id)s-%(title)s.%(ext)s',
      'restrictfilenames': True,
      'noplaylist': True,
      'nocheckcertificate': True,
      'ignoreerrors': False,
      'logtostderr': False,
      'quiet': True,
      'no_warnings': True,
      'default_search': 'auto',
      'source_address': '0.0.0.0'
  }
  vc = ctx.voice_client
  with youtube_dl.YoutubeDL(ytdlopts) as ydl:
    info = ydl.extract_info(url, download = False)
    url2 = info['formats'][0]['url']
    source = discord.FFmpegPCMAudio(url2, **ffmpeg_options)
    vc.play(source)

You might have to fix a couple things but this should work

YouTube DL is not the best for a Music Disocrd bot. I recommend using Wavelink. You can create your own commands (in or not in a Cog) or you can use dismusic

The below example if for using wavelink with a pre-made music cog named dismusic

# 1 - Install ffmpeg (and add to path)
#        Debian/Ubuntu -
#           apt install ffmpeg -y   - On Ubuntu
#        Windows -
#           Download ffmpeg, add to PATH or copy the ffmpeg.exe to System32 Folder

# 2 - Install discord, wavelink, lavalink, PyNacl and dismusic with the command below
#        Debian/Ubuntu -
#           pip3 install lavalink wavelink discord PyNacl dismusic
#        Windows -
#           pip install lavalink wavelink discord PyNacl dismusic

# Code for a basic discord bot
from discord.ext import commands
client = commands.Bot(command_prefix='--')

@client.event
async def on_ready():
    print('+ Logged in as {0.user}'.format(bot))

# Lavalink node to connect to
client.lava_nodes = [
    {
        # a free public node
        # ----------------------
        # 'host': "lava.link", 
        # 'port': 80,
        # 'rest_uri': f'http://lava.link:80',
        # 'password': 'anyPassword',

        # for hosting your own Lavalink locally (More stable)
        # Follow the steps below this code snippet only if you are doing this
        # 
        # ----------------------
        'host': "127.0.0.1",
        'port': 2333,
        'rest_uri': f'http://127.0.0.1:2333',
        'password': 'youshallnotpass',

        # Other Settings
        # ----------------------
        'identifier': 'MAIN',
        'region': 'singapore'
    }
]

# loading the installed, pre-made music cog to the bot
client.load_extension('dismusic')

# Running the bot
bot.run("DoNotShareYourTOKEN")

If you chose to select the free public lava.link server (http://lava.link:80), you can skip steps below

You can run the commands below, line by line, to start your own Lavalink server if you use Debain/Ubuntu

sudo apt install wget curl default-jdk -y
mkdir Lavalink && cd Lavalink # Optional
wget "https://github.com/freyacodes/Lavalink/releases/download/3.4/Lavalink.jar"
curl "https://raw.githubusercontent.com/hirusha-adi/Near/main/others/application.yml" >> "application.yml"
java -jar ./Lavalink.jar

If you are using windows to host the discord (which is not the best idea, but still, if you want to), follow the steps written bel

  1. Install java and add to PATH
  2. Download Lavalink.jar and application.yml
  3. Put both downloaded files to a same folder
  4. Open CMD or Powershell in that folder and run java -jar Lavalink.jar

Available Commands -
--play - Play a song or playlist
--pause - Pause player
--connect - Connect to vc
--seek - Seek player
--nowplaying - Now playing
--queue - See queue
--equalizer - Set equalizer
--volume - Set volume
--resume - Resume player
--loop - Loop song/playlist

Related