对象没有属性“视频”

时间:2019-01-26 20:51:51

标签: python pyqt5 pytube

我正在学习PYQT5,所以这是我的第一次尝试。为了学习PYQT5,我开始使用pyqt5和pytube制作youtube视频下载器,但无法处理进度条(播放列表)。当我运行代码时,它会给出一个AttributeError: 'mywindow' object has no attribute 'video'

你能给我建议吗?

from PyQt5 import QtWidgets

from pytube import YouTube, Playlist

from mydesign import Ui_MainWindow  # importing our generated file

import sys

class mywindow(QtWidgets.QMainWindow):

    def __init__(self):

        super(mywindow, self).__init__()

        self.ui = Ui_MainWindow()

        self.ui.setupUi(self)
        self.ui.action_IKI.setShortcut("Ctrl+Q")
        self.ui.pushButton.clicked.connect(self.click)
        self.ui.action_IKI.triggered.connect(self.quit)


    def quit(self):
        QtWidgets.QApplication.quit()



    def click(self):
        self.completed = 0
        if self.ui.comboBox.currentIndex() == 0:
            while self.completed < 100:
                self.completed += 0.0001
                self.ui.progressBar_2.setValue(self.completed)
        elif self.ui.comboBox.currentIndex() == 1:
            link = self.ui.lineEdit.text()
            yt = YouTube(link, on_progress_callback=self.progress_func)
            video = yt.streams.filter(progressive=True, file_extension='mp4').first()
            video.download()


        elif self.ui.comboBox.currentIndex() == 2:
            pass

        elif self.ui.comboBox.currentIndex() == 3:
            pl = Playlist(self.ui.lineEdit.text())
            pl.populate_video_urls()
            for i in pl.video_urls:
                yt = YouTube(i, on_progress_callback=self.progress_func)
                video = yt.streams.filter(progressive=True, file_extension="mp4").first()
                video.download()

    def progress_func(self, stream, chunk, file_handle, bytes_remaining):
        size = self.video.filesize
        self.progress = (float(abs(bytes_remaining-size)/size))*float(100)
        self.ui.progressBar_2.setValue(self.progress)         

2 个答案:

答案 0 :(得分:0)

这可能会有所帮助:

self.video = yt.streams.filter(progressive=True, file_extension='mp4').first()
self.video.download()

有4个地方可以添加self.video

答案 1 :(得分:0)

给你一个例子,成功了。

yt = YouTube(YouTube_url,on_progress_callback=self.on_progress)
self.video = yt.streams.first()
self.video.download(self.datapath + '/video/', video_id)
def on_progress(self, chunk, file_handler, remaining):
    contentSize = self.video.filesize
    size = contentSize - remaining
    # print('已下载%.2f' % (percent))
    print('\r' + '[Download progress]:[%s%s]%.2f%%' % (
        '█' * int(size * 20 / contentSize), ' ' * (20 - int(size * 20 / contentSize)),
        float(size / contentSize * 100)), end='')

结果:

result image