0

I made a discord music bot, it was working perfectly fine until something happened with youtube_dl and it fails streaming a video with the error "Unable to extract uploader id". I decided to use yt-dlp instead. But now when i try to play a video it connects to the voice channel, but does not play anything and leave instantly with the error "[out#0/s16le @ 000001ad295ae800] Output file does not contain any stream" in the console. Can anybody share a solution to make it work with youtube_dl or yt-dlp both are ok for me if they work. By the way sorry i'm new and i don't really do clean questions. Here's my bot's code:

import discord
from discord.ext import commands
import time
import yt_dlp
import asyncio
from requests import get
import datetime

start_time = time.time()
default_intents = discord.Intents.all()
activity = discord.Activity(type=discord.ActivityType.listening, name="@philipou")
bot = commands.Bot(command_prefix=".", intents=default_intents, activity=activity, status=discord.Status.online)
musics = {}
ytdl = yt_dlp.YoutubeDL()
queue = []


@bot.event
async def on_ready():
    print("The bot is ready !")


class Video:
    def __init__(self, arg):
        try:
            get(arg)
        except:
            video = ytdl.extract_info(f"ytsearch:{arg}", download=False)["entries"][0]
        else:
            video = ytdl.extract_info(arg, download=False)
        video_format = video["requested_formats"][0]
        self.url = video["webpage_url"]
        self.stream_url = video_format["url"]


@bot.command(name="stop", help="Stops the current track.", )
async def stop(ctx):
    global queue
    client = ctx.guild.voice_client
    musics[ctx.guild] = []
    if client:
        await client.disconnect()
        await ctx.send("Disconnected from the voice channel.")
    else:
        await ctx.send("I'm not in a voice channel !")


@bot.command(name="resume", help="Resume the current paused track.")
async def resume(ctx):
    client = ctx.guild.voice_client
    if client.is_paused():
        client.resume()
        await ctx.send("Resuming the current track.")
    else:
        await ctx.send("I'm not paused !")


@bot.command(name="pause", help="Pause the current playing track.")
async def pause(ctx):
    client = ctx.guild.voice_client
    if not client.is_paused():
        client.pause()
        await ctx.send("Pausing the current playing track.")
    else:
        await ctx.send("I'm already paused !")


async def play_song(client, queue, song):
    source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(song.stream_url,
                                                                 before_options="-reconnect 1 -reconnect_streamed 1 "
                                                                                "-reconnect_delay_max 5 "))

    # noinspection PyTypeChecker
    def next(_):
        global queue
        if "loop" in queue:
            asyncio.run_coroutine_threadsafe(play_song(client, queue, song), bot.loop)
        elif len(queue) > 0:
            new_song = queue[0]
            del queue
            queue = [0]
            asyncio.run_coroutine_threadsafe(play_song(client, queue, new_song), bot.loop)
        else:
            asyncio.run_coroutine_threadsafe(client.disconnect(), bot.loop)

    client.play(source, after=next)


@bot.command(name="play", help="Play a song from a search query or url.")
async def play(ctx, *, url):
    client = ctx.guild.voice_client
    video = Video(url)
    musics[ctx.guild] = []
    channel = ctx.author.voice.channel
    if client and client.channel:
        await client.disconnect()
        time.sleep(1)
        client = await channel.connect()
        await ctx.send(f"Playing : {video.url}")
        await play_song(client, musics[ctx.guild], video)
    else:
        await ctx.send(f"Playing : {video.url}")
        client = await channel.connect()
        await play_song(client, musics[ctx.guild], video)


@bot.command(name="ping", help="Check the bot's ping/latency.")
async def bot_ping(ctx):
    await ctx.channel.send(f"Bot have {round(bot.latency * 1000)} ms of ping")


@bot.command(name="uptime", help="Check the bot's uptime")
async def bot_uptime(ctx):
    current_time = time.time()
    difference = int(round(current_time - start_time))
    text = str(datetime.timedelta(seconds=difference))
    await ctx.channel.send("Bot have " + text + " of uptime")


@bot.command(name="about", help="Give information about the bot.")
async def about(ctx):
    await ctx.send("Hey ! I'm a nice bot made to listen to music from youtube (and i also have some other "
                   "funtionalities), for help type `.help`, for support contact `Philipou#6977`")


@bot.command(name="loop", help="Toggle loop for the current track.")
async def loop(ctx):
    client = ctx.guild.voice_client
    try:
        if not client.is_playing():
            return
    except AttributeError:
        await ctx.send("I'm not playing anything !")
        return

    if "loop" in queue:
        queue.remove("loop")
        await ctx.send("Looping disabled for the current track.")
    else:
        queue.append("loop")
        await ctx.send("Looping enabled for the current track.")


bot.run("token :)")

I tried this and with both youtube_dl and yt-dlp

  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jul 16 '23 at 17:58

1 Answers1

0

Instead of:

self.stream_url = video_format["url"]

try just:

self.stream_url = video["url"]

and delete the video_format variable.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
titemov
  • 1
  • 1