为什么在kivy应用程序中使用pytube下载音频时听不到声音?

时间:2020-08-04 19:38:25

标签: python kivy pytube

我正在研究基于kivy的应用程序,可以使用pytube Api下载youtube视频 我只能使用pytube下载所有分辨率的视频,但是当我将此逻辑放入Kivy应用程序时,我只能下载包含视频和音频的360p。 如果我更改为其他分辨率,则会下载该文件,但没有音频。 请帮我解决问题

我的代码仅适用于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")

我使用kivy Gui的Youtube下载器


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'

我不了解我要去哪里 请帮忙。 谢谢

0 个答案:

没有答案