如何在使用pytube的Discord机器人时修复’YouTube’对象没有’streams’属性的错误?

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

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

问题

我正在使用pytube编写自己的Discord机器人,用于播放YouTube音乐。下面的代码行给我一个错误:

代码行: video_stream = video.streams.filter(only_audio=True).first()

错误: 'YouTube'对象没有'streams'属性

我已经尝试重新安装pytube,将其更新到最新版本,但都没有帮助。我使用PyCharm。尽管代码应该可以工作,但我仍然遇到错误。我该如何修复它?

这是使用此代码行的代码部分:

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()

完整代码在这里:https://paste.disnake.dev/?id=1685211680864320

英文:

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

答案1

得分: 0

问题在于您调用了名为Youtube的本地类,而不是pyTube中的一个类来获取具有流的Youtube对象。因此出现了"no attribue error"。

您应该将本地的Youtube类重命名。

英文:

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

huangapple
  • 本文由 发表于 2023年5月28日 02:28:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348418.html
匿名

发表评论

匿名网友

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

确定