Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Kivy屏幕在试图用youtube\u dl从youtube下载视频时冻结_Python 3.x_Kivy_Kivy Language - Fatal编程技术网

Python 3.x Kivy屏幕在试图用youtube\u dl从youtube下载视频时冻结

Python 3.x Kivy屏幕在试图用youtube\u dl从youtube下载视频时冻结,python-3.x,kivy,kivy-language,Python 3.x,Kivy,Kivy Language,我正在尝试开发一个下载歌曲的小应用程序 从YouTube,使用YouTube\u dl和kivy 但是,当我按下下载按钮时,屏幕冻结,而youtube\u dl完成下载。只有在下载完成后,屏幕才会再次变得“活跃” 有什么办法解决这个问题吗?我尝试了在pytube上运行的线程,但在youtube\u dl上运行不正常 from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.textinput i

我正在尝试开发一个下载歌曲的小应用程序 从YouTube,使用
YouTube\u dl
kivy

但是,当我按下下载按钮时,屏幕冻结,而
youtube\u dl
完成下载。只有在下载完成后,屏幕才会再次变得“活跃”

有什么办法解决这个问题吗?我尝试了在
pytube
上运行的线程,但在
youtube\u dl
上运行不正常

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.button import Button
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.image import AsyncImage
from kivy.uix.behaviors import ButtonBehavior
from kivy.properties import ObjectProperty, StringProperty
import requests
import pytube
from kivy.uix.popup import Popup
from kivy.uix.image import Image
from time import sleep
import youtube_dl
import threading
import os
from kivy.utils import get_color_from_hex
from kivy.clock import Clock

Window.fullscreen = False

Config.set('graphics', 'resizable', 1)
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '700')
Config.write()

class YoutubeDownloader(BoxLayout):
    link_ = ObjectProperty()  # This is objectProperty which == Url TextInput
    male = ObjectProperty()

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        checkbox_is_active = ObjectProperty(False)
        self.chck_state = 'normal'  # Down if checked Normal if Uncheck
        self.title = '' # Music title
        self.thumbnail_url = '' #Music Image thumbnail url
        self.playlist_urls = [] # This List conaints All Music Urls from playlist
        self.playlist_size = 0
        self.single_url = []
        self.convert_return_value = 0 # if this variable = 1 it means convert finished succesfully
        self.is_playlist = False

    def checkbox_is_clicked(self):
        '''This function Check state of checkbox'''
        x = self.ids.chck_box.state
        self.chck_state = x
        return 1

    def convert(self):
        main_url = self.link_.text
        if self.chck_state == 'normal':
            try:
                self.remove_widget(self.ids.music_name)
                self.remove_widget(self.ids.img_button)
                yt = pytube.YouTube(main_url)
                self.title = yt.title
                self.thumbnail_url = yt.thumbnail_url
                self.single_url = main_url

                music_name_label = self.ids.music_name
                music_asyncImage = self.ids.img_button
                music_asyncImage.source = self.thumbnail_url
                music_name_label.text = self.title
                self.convert_return_value = 1
                self.is_playlist = False

            except:
                popwind = Popup(
                title='Error!', content=Label(
                    text='Please Enter Valid Url or if its playlist\nplease click playlist checkkbutton\nand convert'), size_hint=(
                    0.8, 0.2))
                popwind.open()
                self.convert_return_value = 0

        elif self.chck_state == 'down':
            try:
                self.ids.music_name.text = ''
                self.ids.img_button.source = ''
                playlist = pytube.Playlist(main_url)
                playlist.populate_video_urls()

                self.playlist_urls = playlist.video_urls

                self.playlist_size = len(playlist.video_urls)

                self.ids.music_name.text = 'Playlist size: {}'.format(self.playlist_size)
                music_asyncImage = self.ids.img_button
                music_asyncImage.source = pytube.YouTube(self.playlist_urls[0]).thumbnail_url
                self.convert_return_value = 1
                self.is_playlist = True

            except:
                popwind = Popup(
                title='Error!', content=Label(
                    text='Please Enter Valid Url or if its Single song\nplease unclick playlist checkkbutton\nand convert'), size_hint=(
                    0.8, 0.2))
                popwind.open()
                self.convert_return_value = 0

    def download_mp3(self):
        if self.convert_return_value == 1 and self.is_playlist == False:
            self.ids.music_name.text = 'Download In Progress Please Wait'
            self.ids.img_button.source = ''
            self.ids.img_button.anim_delay = 0
            self.ids.img_button.source = 'download.gif'
            try:
                download = threading.Thread(target=self.download_video_mp3_mp4(self.link_.text, self.title))
                download.start()
            except:
                pass

            # try:
            #     download = threading.Thread(target=self.download_video_mp3_mp4(self.link_.text, 'test download'))
            #     download.start()
            # except:
            #     pass
            self.ids.music_name.text = 'Download Finished succesfully'
            self.ids.img_button.source = 'down_complete.png'

        elif self.convert_return_value == 0:
            popwind = Popup(
            title='Error!', content=Label(
                text='Download is Not Ready!!\nPlease corectly fill all Fields and CheckBoxes'), size_hint=(
                0.8, 0.2))
            popwind.open()

    def download_video_mp3_mp4(self, song_url, song_title): # For single song
        # yt = pytube.YouTube(self.link_.text)
        # yt.streams.first().download()
        #
        # self.ids.music_name.text = 'Download Finished succesfully'
        # self.ids.img_button.source = 'down_complete.png'
        """
        Download a song using youtube url and song title
        """

        outtmpl = song_title + '.%(ext)s'
        ydl_opts = {
            'format': 'bestaudio/best',
            'outtmpl': outtmpl,
            'postprocessors': [
                {'key': 'FFmpegExtractAudio','preferredcodec': 'mp3',
                 'preferredquality': '192',
                },
                {'key': 'FFmpegMetadata'},
            ],
        }

        youtube_dl.YoutubeDL(ydl_opts).extract_info(song_url, download=True)
            # self.ids.music_name.text = 'Download Finished succesfully'
            # self.ids.img_button.source = 'down_complete.png'

    def download_video(self):
        if self.convert_return_value == 1 and self.is_playlist == False:
            pass
        elif self.convert_return_value == 0:
            popwind = Popup(
            title='Error!', content=Label(
                text='Download is Not Ready!!\nPlease corectly fill all Fields and CheckBoxes'), size_hint=(
                0.8, 0.2))
            popwind.open()

class KivyApp(App):
    def build(self):
        self.title = 'Youtube Downloader'

        return YoutubeDownloader()

您可以在Kivy中使用
Clock.schedule\u一次
功能。它基本上会将进程延迟到下一帧。您可以在下载功能中使用它,并在下载时显示一些进度条或其他内容。您可以查看更多详细信息

也许您可以使用(也可以查看)提供的代码只显示一个空白窗口,即没有按钮等。为了让我们帮助您,请提供最少的代码,以便我们可以重新创建问题。我有kv文件,所有代码都在哪里buttons@GiorgiImerlishvili你解决过这个问题吗?@C.Gibbons是的,我解决了