2012-10-31 6 views
1

저는 python에 익숙하지 않으며이 강력한 언어를 배우는 중입니다. 나는 다음 스크립트를 쓸 수 있었다. 그것은 무엇이 잘못 될지 알지 못하는 부분적인 출력 (그것의 단지 두 줄)을 얻습니다! 도와주세요.QProcess를 사용하여 youtube-dl을 출력하십시오.

#!/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() 
+0

초기화 (부모). 또한 스크립트에서 수행하고자하는 작업과 수행중인 작업은 무엇입니까? –

+0

필자가 작성한대로 명령을 실행하고 QtextEdit 상자에 youtube-dl 명령의 출력을 표시하려고합니다. – user1787326

+0

출력 내용은 다음과 같습니다. [youtube] 설정 언어 [youtube] - : 다운로드 비디오 웹 페이지 – user1787326

답변

0

내가 직접 알아 냈다고 생각합니다. 아래에 모든 개선 된 버전이 있습니다. 개선해야 할 부분이 있으면 권장 사항을 환영 할 것입니다.

클래스 YTDL (QtGui.QMainWindow) : 데프 초기화 (자기 부모 = 없음) : 슈퍼 (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()))