【发布时间】:2022-11-16 04:11:46
【问题描述】:
我正在尝试制作一个应用程序,为我的第一个 kivy 项目下载 YouTube 视频。该程序在 Windows 10 上运行良好,但不适用于 Android。我相信我的问题归结为我是否使用了正确的 pytube 库(无论是“pytube”还是“pytube3”)。另外,有人知道如何为指定的导入正确设置 buildozer.spec 文件吗?
附言如何将 DownloadedVideos 文件夹包含到 buildozer.spec 中?
这是一个测试模型 BTW,所以代码改进来了 ;)
from pytube import YouTube
import pytube
import os
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
import certifi
import os
# Here's all the magic !
os.environ['SSL_CERT_FILE'] = certifi.where()
class MyGrid(GridLayout):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.cols = 1
self.add_widget(Label(text="Paste your YouTube link into the first box on the right. \nOnce you have done so,"
" hit the submit button. \nThe submit button will allow the app to search for the"
" video and confirm its existence. \nThen, a download button with options"
" will appear. \nSelect the options you wish to have downloaded (keeping in mind"
" that audio files need 'kbps' and '.mp3'), and they will be downloaded.",
halign="left", valign="top", font_size=12))
self.inside = GridLayout()
self.inside.cols = 2
self.inside.add_widget(Label(text="Paste YouTube Link:"))
self.link = TextInput(multiline=False)
self.inside.add_widget(self.link)
self.button1 = Button(text="Submit", on_press=self.callback)
self.button1.bind(on_press=self.callback)
self.inside.add_widget(self.button1)
self.outText = TextInput(multiline=True, readonly=True)
self.inside.add_widget(self.outText)
self.inside.add_widget(Label(text="Media:"))
self.ytTypes = TextInput(multiline=True, readonly=True)
self.inside.add_widget(self.ytTypes)
self.add_widget(self.inside)
def downloadMedia(self, instance):
global stream
print(f"Downloading... {stream}")
self.ytTypes.text = f"Downloading... {stream}"
out_file = stream.download("DownloadedVideos")
print(f"Downloaded {stream}")
global yt
self.ytTypes.text = f"Downloaded \n{yt.title}!"
try:
print("here")
self.remove_widget(self.download)
except:
print("Error")
def callback(self, instance):
youtubeLink = self.link.text
print("pressed", youtubeLink)
try:
global yt
yt = YouTube(youtubeLink)
print("Views: ", yt.title)
print("Length: ", yt.length)
print("Views", yt.views)
#Rounds the length of the YouTube video in minutes down to 2 decimal places
res = "{:.2f}".format(yt.length/60)
self.outText.text = f"Title: {yt.title}, \nLength: {res} minutes, \nViews: {yt.views}"
#Grabs video itags and displays them
global stream
stream = yt.streams.filter(progressive=True).last()
print(stream)
print("pass")
print(stream.itag)
self.ytTypes.text = f"Streams: {stream}"
self.download = Button(text="Download:")
self.download.bind(on_press=self.downloadMedia)
self.add_widget(self.download)
except:
print("error")
self.outText.text = "ERR"
self.link.text = ""
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == "__main__":
MyApp().run()
【问题讨论】:
-
只是跟进任何感兴趣的人。仍在寻找解决方案:
标签: python android kivy buildozer pytube