pythondiscordattributeerrorpytubedisnake

How to fix 'YouTube' object has no attribute 'streams' error while using pytube in a discord bot?


I am using pytube to write my own discord bot that will play youtube music. The line below gives me an error:

Line: video_stream = video.streams.filter(only_audio=True).first()

Error: 'YouTube' object has no attribute 'streams'

I have tried reinstalling pytube, updating it to the latest version. Nothing helped. I use PyCharm. Despite the fact that the code should be working, I get an error. How can I fix it?

Part of the code where this line is used:

import disnake
from disnake.ext import commands
from pytube import YouTube
from youtubesearchpython import VideosSearch
import asyncio
import os

async def play_next_song(voice_client):
    next_url = music_queue.get_next_url()

    if next_url:
        try:
            video = YouTube(next_url)
            video_stream = video.streams.filter(only_audio=True).first()
            video_stream.download(output_path="temp")
            video_title = video.title.replace(" ", "_").replace("\\", "").replace("|", "")

            original_path = os.path.join("temp", video_stream.default_filename)
            renamed_path = os.path.join("temp", video_title + ".mp4")
            os.rename(original_path, renamed_path)

        except Exception as e:
            print(e)
            await play_next_song(voice_client)
            return

        mp4_path = renamed_path
        mp3_path = os.path.join("temp", video_title + ".mp3")
        os.system(f"ffmpeg -i {mp4_path} -vn -ar 44100 -ac 2 -b:a 192k {mp3_path}")

        voice_client.play(disnake.FFmpegPCMAudio(executable="C:/ffmpeg/bin/ffmpeg.exe", source=mp3_path))

        while voice_client.is_playing():
            if music_queue.is_paused:
                await asyncio.sleep(1)
            else:
                await asyncio.sleep(1)

        os.remove(mp4_path)
        os.remove(mp3_path)

        await play_next_song(voice_client)
    else:
        await voice_client.disconnect()

Full code here: https://paste.disnake.dev/?id=1685211680864320


Solution

  • The issue is that you are calling the local class named Youtube instead of the one of pyTube to get the Youtube Object that has a stream. Hence the no attribue error

    You should rename that local Youtube class