【发布时间】:2019-04-21 11:45:11
【问题描述】:
我正在尝试开发一个下载歌曲的小应用程序
来自 YouTube,youtube_dl 和 kivy。
但是,当我按下下载按钮时,屏幕冻结,而youtube_dl 完成下载。只有下载完成后,屏幕才会重新“活”起来。
有没有办法解决这个问题?我尝试了在pytube 上工作的线程,但在youtube_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()
【问题讨论】:
-
提供的代码只显示了一个空白窗口,即没有按钮等。为了帮助您,请提供最少的代码,以便我们可以重现问题。
-
我有 kv 文件,所有按钮都在哪里
-
@GiorgiImerlishvili 你解决过这个问题吗?
-
@C.Gibbons 是的,我做到了
标签: python-3.x kivy kivy-language