首页 > 解决方案 > 为什么我在 kivy 应用程序中使用 pytube 下载时音频听不到?

问题描述

我正在开发基于 kivy 的应用程序,它可以使用 pytube Api 下载 youtube 视频和音频。如果我更改为任何其他分辨率,它会被下载但没有音频。请帮助我如何解决问题

我的代码只有 Pytube

from pytube import YouTube

url = input('Enter the link to Link download')
type1 = int(input('1.Video\n2.Audio'))
mime_type = ''
if type1 == 1:
    mime_type = 'video'
elif type1 == 2:
    mime_type = 'audio'
else:
    print('Please select appropriatly')
    exit()

type2 = int(input('1.mp4\n2.webm'))

if type2 == 1:
    mime_type += '/mp4'
elif type2 == 2:
    mime_type += '/webm'
else:
    print('Please select appropriatly')
    exit()

name = input('Name Of the file? : ')

try:
    yt = YouTube(url)
except:
    print('Couldnt connect')
    exit()

if type1 == 2:
    stream = yt.streams.filter(mime_type=mime_type).first()
    stream.download(filename=name)
    print("Download successfull")
    exit()


def resolution():
    res_query = int(input('Select Resolution\n1.144p\n2.240p\n3.360p\n4.480p\n5.720p\n6.1080p'))
    res = ''
    if res_query == 1:
        res = '144p'
    elif res_query == 2:
        res = '240p'

    elif res_query == 3:
        res = '360p'
    elif res_query == 4:
        res = '480p'

    elif res_query == 5:
        res = '720p'

    elif res_query == 6:
        res = '1080p'
    else:
        print('Enter appropriate resolution')
        exit()


res = resolution()
stream = yt.streams.filter(mime_type=mime_type, res=res)
if stream == []:
    print('Resolution of {} is not availible for download\nPlease try with different resolution')
    res = resolution()

else:
    stream.first().download(filename=name)
    print("Download successfull")

我的 Youtube 下载器使用 kivy Gui


from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.properties import ObjectProperty
from pytube import YouTube

class Option(Screen):

    def on_pre_enter(self, *args):
        Window.clearcolor = (0.15, 0.15, 1, 1)


class Video(Screen):
    url = ObjectProperty(None)
    res=ObjectProperty(None)
    filename = ObjectProperty(None)

    def on_pre_enter(self, *args):
        Window.clearcolor = (1, 1, 1, 1)

    def Download(self):
        print(self.url.text)
        print(self.res.text)
        mime_type = 'video/mp4'

        yt=YouTube(self.url.text)
        stream = yt.streams.filter(mime_type=mime_type, res=self.res.text).first()
        stream.download(filename=self.filename.text)
        print('Download Done')




class Audio(Screen):
    url = ObjectProperty(None)
    filename=ObjectProperty(None)



    def on_pre_enter(self, *args):
        Window.clearcolor = (1, 1, 1, 1)



    def Download(self):
        print(self.url.text)

        mime_type = 'audio/mp4'
        yt = YouTube(self.url.text)
        stream = yt.streams.filter(mime_type=mime_type).first()
        stream.download(filename=self.filename.text)


class Manager(ScreenManager):
    pass


sm = ScreenManager()
kv = Builder.load_file('You.kv')


class YouApp(App):
    def build(self):
        Window.size = (360, 600)
        self.icon = r'icon.png'
        sm.add_widget(Option())
        sm.add_widget(Video())
        sm.add_widget(Audio())
        return sm


YouApp().run()

我的kv文件

<Manager>:
    Option:
        id:option
        name:'option'
    Audio:
        id:audio
        name:'audio'
    Video:
        id:video
        name:'video'

<Option>
    name:'option'


    Widget:
        size_hint:[0.3,0.2]
        pos_hint:{'center_x':0.5,'top':0.92}

        canvas:
            Ellipse:
                size:self.size
                pos:self.pos
                source:'icon.png'


    GridLayout:
        rows:2
        size_hint:[0.8,0.27]
        pos_hint:{'center_x':0.5,'center_y':0.5}


        Button:
            text:" Download Video"
            background_color:204/255,229/255,1,1
            on_press:
                app.root.transition.direction='left'
                app.root.current='video'

        Button:
            text:"Download Audio"
            background_color:204/255,229/255,1,1
            on_press:
                app.root.transition.direction='right'
                app.root.current='audio'



<Video>
    name:'video'
    res:res
    url:url
    filename:filename
    Widget:
        size_hint:[0.3,0.2]
        pos_hint:{'center_x':0.5,'top':0.92}

        canvas:
            Ellipse:
                size:self.size
                pos:self.pos
                source:'icon.png'
    GridLayout:
        cols:2
        spacing:15
        size_hint:[0.8,0.25]
        pos_hint:{'center_x':0.5,'top':0.6}

        Label:
            text:'Url : '
            color:0,0,0,1

        TextInput:
            id :url
            multiline :False
            font_size:20

        Label:
            text:'Resolution : '
            color:0,0,0,1

        TextInput:
            id :res
            text:'360p'
            multiline :False
            font_size:20

        Label:
            text:'Video Name '
            color:0,0,0,1

        TextInput:
            id :filename
            multiline :False
            font_size:20




    Button:
        text:'Download'
        size_hint:[0.8,0.1]
        pos_hint:{'center_x':0.5,'top':0.3}
        on_press:root.Download()

    Button:
        text:'Back'
        size_hint:[0.8,0.1]
        pos_hint:{'center_x':0.5,'top':0.18}
        on_press:app.root.current='option'

<Audio>
    name:'audio'
    url:url
    filename:filename


    Widget:
        size_hint:[0.3,0.2]
        pos_hint:{'center_x':0.5,'top':0.92}

        canvas:
            Ellipse:
                size:self.size
                pos:self.pos
                source:'icon.png'
    GridLayout:
        cols:2
        spacing:15
        size_hint:[0.8,0.2]
        pos_hint:{'center_x':0.5,'top':0.6}

        Label:
            text:'Url : '
            color:0,0,0,1

        TextInput:
            id :url
            multiline :False
            font_size:20

        Label:
            text:'Audio Name '
            color:0,0,0,1

        TextInput:
            id :filename
            multiline :False
            font_size:20




    Button:
        text:'Download'
        size_hint:[0.8,0.1]
        pos_hint:{'center_x':0.5,'top':0.35}
        on_press:root.Download()

    Button:
        text:'Back'
        size_hint:[0.8,0.1]
        pos_hint:{'center_x':0.5,'top':0.18}
        on_press:app.root.current='option'

我不明白我哪里出错了请帮忙。谢谢你

标签: pythonkivypytube

解决方案


推荐阅读