YouTube视频下载器

时间:2020-08-30 12:00:59

标签: python tkinter video pytube

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from pytube import YouTube
FolderName = ''
fileSizeInBytes = 0
MaxFileSize = 0


def openDirectory():
    global FolderName
    FolderName = filedialog.askdirectory()
    if (len(FolderName)> 1):
        fileLocationLabelError.config(text=FolderName,
                                      fg='green')
    else:
        fileLocationLabelError.config(text='Please Choose Folder',
                                      fg='red')



def DownloadFile():
    global MaxFileSize,fileSizeInBytes


    choice = youtubeChoices.get()
    video = youtubeEntry.get()


    if (len(video)>1):
        youtubeEntryError.config(text='')
        print(video,'at',FolderName)
        yt = YouTube(video,on_progress_callback=progress)
        print('video name is:\n\n',yt.title)

        if (choice == downloadChoices[0]):
            print('1080p video is downloading...')
            loadingLabel.config(text='1080p video file downloading...')
            selectVideo =yt.streams.filter(progressive=True).first()

        elif (choice == downloadChoices[1]):
            print('720p video is downloading...')
            loadingLabel.config(text='720p video file downloading...')
            selectVideo =yt.streams.filter(progressive=True).first()

        elif (choice == downloadChoices[2]):
            print('480p video is downloading...')
            loadingLabel.config(text='480p video file downloading...')
            selectVideo = yt.streams.filter(progressive=True,
                                            file_extension='mp4').last
        elif (choice == downloadChoices[3]):
            print('3gp file is downloading...')
            selectVideo = yt.streams.filter(file_extension='3gp').first()

        elif (choice == downloadChoices[4]):
            print('Audio file is downloading...')
            selectVideo = yt.streams.filter(only_audio=True).first()

        fileSizeInBytes = selectVideo.filesize
        MaxFileSize = fileSizeInBytes / 1024000
        MB = str(MaxFileSize) + 'MB'
        print('File size = : {:00.00f}'.format(MaxFileSize))
        #download
        selectVideo.download(FolderName)
        print('Downloaded on : {}'.format(FolderName))
        complete()
    else:
        youtubeEntryError.config(text='Please paste youtube link',
                              fg='red')

def progress(stream= None, chunk=None, file_handle = None, remaining=None):
    percent = (100 * (fileSizeInBytes - remaining)) / fileSizeInBytes
    print('{00.0f}% downloaded'.format(percent))

def complete():
    loadingLabel.config(text='Download completed')




root = Tk()
root.title('YouTube video Downloader')
root.grid_columnconfigure(0,weight=1)
youtubeLinkLabel = Label(root,text='Please paste youtube link here..',
                         fg='blue',font=('Agency FB',30))
youtubeLinkLabel.grid()
youtubeEntryvar = StringVar()
youtubeEntry = Entry(root,width=50, textvariable=youtubeEntryvar)
youtubeEntry.grid(pady=(0,20))
youtubeEntryError = Label(root,fg='red', text='',font=('Agency FB',20))
youtubeEntryError.grid(pady=(0,10))
SaveLabel = Label(root,text='Where to download file?',fg='blue',font=('Agency FB',20,'bold'))
SaveLabel.grid()
SaveEntry = Button(root,width=20,bg='green',fg='white',
                   text='Choose folder',font=('Arial',15),
                   command=openDirectory)
SaveEntry.grid()
fileLocationLabelError = Label(root,text='',font=('Agency FB',20))
fileLocationLabelError.grid(pady=(0,0))
youtubeChooseLabel = Label(root,text='Please choose what to download:',
                           font=('Agency FB',20))
youtubeChooseLabel.grid()
downloadChoices = ['Mp4 1080p',
                   'Mp4 720p',
                   'Mp4 480p',
                   'Video 3gp',
                   'song mp3']
youtubeChoices = ttk.Combobox(root,values=downloadChoices)
youtubeChoices.grid()
downloadButton = Button(root,text='Download',width=15,bg='green',
                        command=DownloadFile)
downloadButton.grid()


loadingLabel = ttk.Label(root,text='App developed by |> CID',
                         font=('Agency FB',20))
loadingLabel.grid()
root.mainloop()

我这里有一个错误

我应该在变量中添加一些标记

如果您看到此问题,可以对它进行基本解释

因为我的英语不好

谢谢

出现类型错误

我是Tkinter的新手

我再次检查了代码,但我认为没有问题

感谢所有StackOverflow社区

下面是错误

 Video downloader app । using Python and Pytube
1080p video is downloading...
File size = : 97
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Toshiba\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/Toshiba/Desktop/python_temelleri/youtube video downloader.py", line 64, in DownloadFile
    selectVideo.download(FolderName)
  File "C:\Users\Toshiba\Desktop\python_temelleri\venv\lib\site-packages\pytube\streams.py", line 241, in download
    self.on_progress(chunk, fh, bytes_remaining)
  File "C:\Users\Toshiba\Desktop\python_temelleri\venv\lib\site-packages\pytube\streams.py", line 302, in on_progress
    self._monostate.on_progress(self, chunk, bytes_remaining)
  File "C:/Users/Toshiba/Desktop/python_temelleri/youtube video downloader.py", line 72, in progress
    percent = (100 * (fileSizeInBytes - remaining)) / fileSizeInBytes
TypeError: unsupported operand type(s) for -: 'int' and 'NoneType'

1 个答案:

答案 0 :(得分:2)

传递给remaining方法的progress参数是None