|
import tkinter
from tkinter import *
from pytube import YouTube
from tkinter import messagebox, filedialog
# (定义createWidgets()函数以创建必要的tkinter小部件)
def CreateWidgets ():
linkLabel = Label(root, text="输入视频链接:", bg="skyblue")
linkLabel.grid(row=1, column=0, pady=5, padx=5)
root.linkText = Entry(root, width=60)
root.linkText.grid(row=1, column=1, pady=5,padx=5, columnspan=2)
destinationLabel = Label(root, text="保存路径:", bg="skyblue")
destinationLabel.grid(row=2, column=0, pady=5, padx=5)
root.destinationText = Entry(root, width=38)
root.destinationText.grid(row=2, column=1, pady=5, padx=5)
# 调用浏览函数
browseButton = Button(root, text="浏览", command=Browse, width=15)
browseButton.grid(row=2, column=2, pady=5, padx=5)
# 调用下载函数
dwldButton =Button(root,text="下载",command=Download,width=30)
dwldButton.grid(row=3,column=1,pady=5,padx=5)
# (定义Browse()以选择保存视频的目标文件夹)
def Browse ():
# Retrieving the user-input destination directory(检索用户输入的目标目录)
root.destinationDIR = filedialog.askdirectory(initialdir="./")
# Displaying the directory in the directory textbox(在目录文本框中显示目录)
root.destinationText.insert('1', root.destinationDIR)
# (定义Download()以下载视频)
def Download ():
# (使用用户输入链接作为参数创建Youtube()的对象)
getVideo = YouTube(root.linkText.get())
# (从可用的流列表中检索第一个流)
videoStream = getVideo.streams.first()
# (将视频下载到用户输入目标目录)
videoStream.download(root.destinationDIR)
# (显示消息)
messagebox.showinfo("SUCCESS", "VIDEO DOWNLOADED AND SAVE IN\n" + root.destinationDIR)
# (创建tk类的对象)
root = tkinter.Tk()
# 设置tkinter窗口的标题,背景颜色和大小,并禁用调整大小属性
root.geometry("530x110")
root.title("PyVideoDownloader")
root.resizable(False,False)
root.config(background="skyblue")
# (调用CreateWidgets()函数)
CreateWidgets()
# (定义无限循环来运行应用程序)
root.mainloop()
# https://youtu.be/nJehDtCuI-M
|
|