如何一个接一个地播放视频的多个片段

时间:2019-01-29 08:06:42

标签: python pyqt pyqt5 qmediaplayer

我有一个视频,按时间间隔从中获取一些剪辑,我想要执行以下操作:

  • 让片段依次播放-因此先播放片段,然后完成,然后再播放,依此类推...但是目前,这很好,但它只是在这两个剪辑,我不希望这样做。

我该怎么做?

我尝试使用例如Toolspause等……只是所有内容,但没有一个起作用。

我的python版本:3.6.0

PyQt:5.6

视频播放器文件(由于太长而导致无法复制的链接):

https://repl.it/repls/SuperBrownSoftware

(只需将其复制到您的IDE中)

这是我应运行的代码:

stop

更新:

我现在得到了ekhumoro的回答,所以我正在使用此结构的设置:

from PyQt5 import QtCore, QtGui, QtWidgets, QtMultimedia
from PyQt5.QtWidgets import *
import sys
from pyqtvideo2_copy import *
app = QtWidgets.QApplication(sys.argv)

video = VideoWidget()
w = QtWidgets.QWidget()
w.fr = QtWidgets.QGridLayout(w)
w.bt = QtWidgets.QPushButton()
w.fr.addWidget(w.bt)
w.player_=Player(sys.argv[1:])
w.fr.addWidget(w.player_)
print(w)
video.activateWindow()

def clicked():
    l=[[2000,4000],[10000,15000]]
    for i in l:
        w.player_.setPosition(i[0])
        w.player_.player.pause()
        w.player_.player.play()
        w._end=i[1]
        w.player_.player.positionChanged.connect(on_positionChanged)

def on_positionChanged(position):
    if w.player_.player.state() == QtMultimedia.QMediaPlayer.PlayingState:
        if position > w._end:
            w.player_.player.stop()

def except_hook(cls, exception, traceback):
    sys.__excepthook__(cls, exception, traceback)

w.bt.clicked.connect(clicked)
w.show()
sys.excepthook=except_hook
sys.exit(app.exec_())

我将ekhumoro的答案函数复制到了我的代码中,然后添加了两行:class Ui_MainWindow(QMainWindow): def setupUi(self, MainWindow): ... def retranslateUi(self, MainWindow): ... self.player.positionChanged.connect(self.handlePositionChanged) self.player.mediaStatusChanged.connect(self.handleMediaStateChanged) ... def videoclips(self): self.w=QWidget() g=QGridLayout(self.w) g.setContentsMargins(0,0,0,0) d=TableWidget(self.df2,self.clicked) g.addWidget(d) self.w.show() def clicked(self,item): self.w.close() self.addMedia(ast.literal_eval(item.text())) def addMedia(self, clips): self._index = -1 self._clips = clips def playNext(self): self.player.player.pause() self._index += 1 if 0 <= self._index < len(self._clips): self.player.player.setPosition(self._clips[self._index][0]) self.player.player.play() def handlePositionChanged(self, pos): if (0 <= self._index < len(self._clips) and pos > self._clips[self._index][1] and self.player.player.state() == QtMultimedia.QMediaPlayer.PlayingState): self.playNext() def handleMediaStateChanged(self, state): if state == QtMultimedia.QMediaPlayer.LoadedMedia: self.playNext() self.player.positionChanged.connect(self.handlePositionChanged)self.player.mediaStatusChanged.connect(self.handleMediaStateChanged)

但这似乎没有用,我觉得我犯了一些错误。

给我一​​个错误:

retranslateUi

1 个答案:

答案 0 :(得分:4)

一种方法是创建一个简单的队列,然后等待每个剪辑完成,然后再移至队列中的下一个项目。下面是显示如何实现此目标的简单演示。希望应该很容易看到如何使它适应您自己的代码。

import sys
from PyQt5 import QtCore, QtWidgets, QtMultimedia, QtMultimediaWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.video = QtMultimediaWidgets.QVideoWidget()
        self.player = QtMultimedia.QMediaPlayer()
        self.player.setVideoOutput(self.video)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.video)
        self.player.positionChanged.connect(self.handlePositionChanged)
        self.player.mediaStatusChanged.connect(self.handleMediaStateChanged)

    def addMedia(self, path, clips):
        self._index = -1
        self._clips = clips
        self.player.setMedia(
            QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile(path)))

    def playNext(self):
        self.player.pause()
        self._index += 1
        if 0 <= self._index < len(self._clips):
            self.player.setPosition(self._clips[self._index][0])
            self.player.play()

    def handlePositionChanged(self, pos):
        if (0 <= self._index < len(self._clips) and
            pos > self._clips[self._index][1] and
            self.player.state() == QtMultimedia.QMediaPlayer.PlayingState):
            self.playNext()

    def handleMediaStateChanged(self, state):
        if state == QtMultimedia.QMediaPlayer.LoadedMedia:
            self.playNext()

app = QtWidgets.QApplication(sys.argv)
window = Window()
window.addMedia(sys.argv[1], [[2000, 4000], [10000, 15000]])
# window.addMedia(sys.argv[1], [[10000, 20000], [40000, 50000]])
window.show()
sys.exit(app.exec_())