使用QProcess输出youtube-dl

时间:2012-10-31 02:56:05

标签: python ubuntu qt4 qprocess youtube-dl

我是python的新手,正在学习这种强大的语言。我设法编写以下脚本。它确实得到了部分输出(只有两行)我不知道出了什么问题!请帮帮我。

#!/usr/bin/env python
#-*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import (QProcess,QRect,SIGNAL,SLOT,QString,QStringList,qDebug)
from PyQt4.QtGui import (QMainWindow,QWidget,QPushButton,QTextEdit,QApplication)

class YTDL (QtGui.QMainWindow):
    def __init__ (self,parent=None):
        super(YTDL,self).__init__(parent)

        self.resize(400,300)
        self.setWindowTitle("Youtube-dl output using QProcess")

        self.__init_Components()
        self.__ui_event_handler()

    def __init_Components(self):
        self.proc = QProcess()
        self.cw = QWidget(self)

        self.btn = QPushButton(self.cw)
        self.btn.setText("Run")
        self.btn.setGeometry(QRect(270,10,110,27))

        self.te = QTextEdit(self.cw)
        self.te.setReadOnly(True)
        self.te.setOverwriteMode(False)
        self.te.setGeometry(QRect(10,40,380,170))

        self.setCentralWidget(self.cw)

    def __ui_event_handler(self):
        self.connect(self.btn, SIGNAL('clicked()'),self.Button_Clicked)


    def Button_Clicked(self):
        args = '-ct -f 18 --extract-audio --audio-quality 320k --audio-format mp3 -k http://www.youtube.com/watch?v=OiPO_TAAZPc'
        cmd = 'youtube-dl'
        self.proc.setWorkingDirectory("~/Videos/Folder1")
        self.connect(self.proc, SIGNAL('readyRead()'),self._read)
        self.proc.setOpenMode(self.proc.ReadWrite)
        self.proc.start(cmd ,args)
        if not self.proc.waitForStarted():
            exit(1)

    def _read(self):
        s = self.proc.readAllStandardOutput()
        qDebug (s)
        print (s)
        self.te.append(QString(s)) 



def main():
    import sys
    app = QApplication(sys.argv)
    ytdl = YTDL()
    ytdl.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

我想我自己弄清楚了。这是下面的全部改进版本。如果有什么需要改进的话,我会欢迎任何建议。

类YTDL(QtGui.QMainWindow):     def init (self,parent = None):         超级(YTDL,自我)。的初始化(父)

    self.resize(400,350)
    self.setWindowTitle("Youtube-dl output using QProcess")

    self.__init_Components()
    self.__ui_event_handler()

def __init_Components(self):
    self.proc = QProcess()
    self.cw = QWidget(self)

    self.btn = QPushButton(self.cw)
    self.btn.setText("Run")
    self.btn.setGeometry(QRect(270,10,110,27))

    self.te = QTextEdit(self.cw)
    self.te.setReadOnly(True)
    self.te.setOverwriteMode(False)
    self.te.setGeometry(QRect(10,40,380,170))

    self.progbar = QProgressBar(self.cw)
    self.progbar.setGeometry(QRect(10,220,380,18))
    self.progbar.setRange(0,100)
    self.progbar.setValue(0)
    self.progbar.show()

    self.setCentralWidget(self.cw)

def __ui_event_handler(self):
    self.connect(self.btn, SIGNAL('clicked()'),self.Button_Clicked)
    self.connect(self.proc, SIGNAL('readyReadStandardOutput()'),self._read)
    self.connect(self.proc, SIGNAL('readyReadStandardError()'),self._readError)


def Button_Clicked(self):

    args = "-ct -f 18 --extract-audio --audio-quality 320k --audio-format mp3 -k http://www.youtube.com/watch?v=SjUrib_Gh0Y"
    cmd = "youtube-dl"
    cmd = cmd + " " + args 
    print (cmd)
    self.proc.setWorkingDirectory("~/Videos/Folder1")
    self.proc.setOpenMode(self.proc.ReadWrite)
    self.proc.start(cmd)
    self.proc.waitForStarted()


def _read(self):
    s = str(self.proc.readAllStandardOutput())

    download_progress_exp = re.compile(r'.\d+\.\d+\%', re.MULTILINE)
    progbarresult = download_progress_exp.findall(s)
    i = 0
    if progbarresult != []:
        for i in range(0,len(progbarresult)):
            self.progbar.setValue(float(progbarresult[i].strip("%")))
            self.te.append(QString(s))



def _readError(self):
    self.te.append(str(self.proc.readAllStandardError()))