3

我想要一个简单的 youtube 客户端,播放 youtube 视频并列出按观看次数排序的视频,使用 Python 和 Qt4 声子。
我还安装了youtube-dl来获取视频的 URL。

我找到了PyQt4.phonon 的一个很好的例子,通过这个例子,我可以播放来自我的高清和其他网站的视频,但不能播放来自 youtube 的视频(我在播放器上看不到任何内容,但在终端中也看不到任何内容)。

我正在使用Python2.7.6/Python3.4.0PyQt4youtube_dl-2014.05.19

我错过了播放 youtube 视频的内​​容吗?还是我的代码有问题?

提前致谢。

我的代码:

#!/usr/bin/python

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.phonon import Phonon
from PyQt4 import phonon
import sys
import youtube_dl

def get_video_url(url):
    ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'})
    ydl.add_default_info_extractors()

    result = ydl.extract_info(url, download=False)
    if 'entries' in result:
        video = result['entries'][0]
    else:
        video = result
    return video['url']


class QPlayer(QWidget):
    def __init__(self):
        super(QPlayer, self).__init__()
        self.audioOuptut=Phonon.AudioOutput(Phonon.MusicCategory, self)
        self.player=Phonon.MediaObject(self)
        Phonon.createPath(self.player, self.audioOuptut)

        self.videoWidget=Phonon.VideoWidget(self)
        Phonon.createPath(self.player, self.videoWidget)

        self.player.setTickInterval(1000)
        self.connect(self.player, SIGNAL("tick(qint64)"), self.tick)

        self.seekSlider=Phonon.SeekSlider(self.player, self)
        self.volumeSlider=Phonon.VolumeSlider(self.audioOuptut, self)

        self.buildGUI()
        self.setupConnections()

    def buildGUI(self):

        self.fileLabel = QLabel("File")
        self.fileEdit = QLineEdit()
        self.fileLabel.setBuddy(self.fileEdit)

        self.lcdTimer=QLCDNumber()
        self.lcdTimer.display("00:00")

        self.browseButton=QPushButton("Browse")
        self.browseButton.setIcon(QIcon(":/images/folder-music.png"))

        self.playButton=QPushButton("Play")
        self.playButton.setIcon(QIcon(":/images/play.png"))
        self.playButton.setEnabled(False)

        self.pauseButton=QPushButton("Pause")
        self.pauseButton.setIcon(QIcon(":/images/pause.png"))

        self.stopButton=QPushButton("Stop")
        self.stopButton.setIcon(QIcon(":/images/stop.png"))

        upperLayout=QHBoxLayout()
        upperLayout.addWidget(self.fileLabel)
        upperLayout.addWidget(self.fileEdit)
        upperLayout.addWidget(self.browseButton)

        midLayout=QHBoxLayout()
        midLayout.addWidget(self.seekSlider)
        midLayout.addWidget(self.lcdTimer)

        lowerLayout=QHBoxLayout()
        lowerLayout.addWidget(self.playButton)
        lowerLayout.addWidget(self.pauseButton)
        lowerLayout.addWidget(self.stopButton)
        lowerLayout.addWidget(self.volumeSlider)

        layout=QVBoxLayout()
        layout.addLayout(upperLayout)
        layout.addWidget(self.videoWidget)
        layout.addLayout(midLayout)
        layout.addLayout(lowerLayout)

        self.setLayout(layout)
        self.lcdTimer.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.seekSlider.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.volumeSlider.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)


    def setupConnections(self):
        self.connect(self.browseButton, SIGNAL("clicked()"), self.browseClicked)
        self.connect(self.playButton, SIGNAL("clicked()"), self.playClicked)#self.playClicked)
        self.connect(self.pauseButton, SIGNAL("clicked()"), self.pauseClicked)
        self.connect(self.stopButton, SIGNAL("clicked()"), self.stopClicked)
        self.connect(self.fileEdit, SIGNAL("textChanged(const QString&)"), self.checkFileName)

    def tick(self, time):
        displayTime = QTime(0, (time / 60000) % 60, (time / 1000) % 60)
        self.lcdTimer.display(displayTime.toString('mm:ss'))

    def playClicked(self):
        self.player.setCurrentSource(Phonon.MediaSource(self.fileEdit.text()))
        self.player.play()

    def pauseClicked(self):
        self.player.pause()

    def stopClicked(self):
        self.player.stop()
        self.lcdTimer.display("00:00")

    def browseClicked(self):
        f=QFileDialog.getOpenFileName(self)
        if not f.isEmpty():
            self.fileEdit.setText(f)

    def checkFileName(self, s):
        if not s.isEmpty():
            self.playButton.setEnabled(True)
        else:
            self.playButton.setEnabled(False)

app = QApplication(sys.argv)
q = QPlayer()
q.player.setCurrentSource(Phonon.MediaSource(get_video_url("[video URL (https://www.youtube.com/watch?v=...)]")))
q.player.play()
q.show()
app.exec_()

在终端:

~$ python phonon.py
Got bus address:  "unix:abstract=/tmp/dbus-ErjaxLIFi4,guid=7055ff4ff81492c67a9c642153814399" 
Connected to accessibility bus at:  "unix:abstract=/tmp/dbus-ErjaxLIFi4,guid=7055ff4ff81492c67a9c642153814399" 
Registered DEC:  true 
[youtube] Setting language
[youtube] ***: Downloading webpage
[youtube] ***: Downloading video info webpage
[youtube] ***: Extracting video information
Registered event listener change listener:  true 
4

0 回答 0