class VideoPlayer(QWidget): def __init__(self, aPath, parent=None): super(VideoPlayer, self).__init__(parent) self.setAttribute(Qt.WA_NoSystemBackground, True) self.setAcceptDrops(True) self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.StreamPlayback) self.mediaPlayer.mediaStatusChanged.connect(self.printMediaData) self.mediaPlayer.setVolume(80) self.videoWidget = QVideoWidget(self) self.lbl = QLineEdit('00:00:00') self.lbl.setReadOnly(True) self.lbl.setFixedWidth(70) self.lbl.setUpdatesEnabled(True) self.lbl.setStyleSheet(stylesheet(self)) self.lbl.selectionChanged.connect(lambda: self.lbl.setSelection(0, 0)) self.elbl = QLineEdit('00:00:00') self.elbl.setReadOnly(True) self.elbl.setFixedWidth(70) self.elbl.setUpdatesEnabled(True) self.elbl.setStyleSheet(stylesheet(self)) self.elbl.selectionChanged.connect( lambda: self.elbl.setSelection(0, 0)) self.playButton = QPushButton() self.playButton.setEnabled(False) self.playButton.setFixedWidth(32) self.playButton.setStyleSheet("background-color: black") self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay)) self.playButton.clicked.connect(self.play) self.positionSlider = QSlider(Qt.Horizontal, self) self.positionSlider.setStyleSheet(stylesheet(self)) self.positionSlider.setRange(0, 100) self.positionSlider.sliderMoved.connect(self.setPosition) self.positionSlider.setSingleStep(2) self.positionSlider.setPageStep(20) self.positionSlider.setAttribute(Qt.WA_TranslucentBackground, True) self.clip = QApplication.clipboard() self.process = QProcess(self) self.process.readyRead.connect(self.dataReady) self.process.finished.connect(self.playFromURL) self.myurl = "" controlLayout = QHBoxLayout() controlLayout.setContentsMargins(5, 0, 5, 0) controlLayout.addWidget(self.playButton) controlLayout.addWidget(self.lbl) controlLayout.addWidget(self.positionSlider) controlLayout.addWidget(self.elbl) layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(self.videoWidget) layout.addLayout(controlLayout) self.setLayout(layout) self.myinfo = "©2016\nAxel Schneider\n\nMouse Wheel = Zoom\nUP = Volume Up\nDOWN = Volume Down\n" + \ "LEFT = < 1 Minute\nRIGHT = > 1 Minute\n" + \ "SHIFT+LEFT = < 10 Minutes\nSHIFT+RIGHT = > 10 Minutes" self.widescreen = True #### shortcuts #### self.shortcut = QShortcut(QKeySequence("q"), self) self.shortcut.activated.connect(self.handleQuit) self.shortcut = QShortcut(QKeySequence("u"), self) self.shortcut.activated.connect(self.playFromURL) self.shortcut = QShortcut(QKeySequence("y"), self) self.shortcut.activated.connect(self.getYTUrl) self.shortcut = QShortcut(QKeySequence("o"), self) self.shortcut.activated.connect(self.openFile) self.shortcut = QShortcut(QKeySequence(" "), self) self.shortcut.activated.connect(self.play) self.shortcut = QShortcut(QKeySequence("f"), self) self.shortcut.activated.connect(self.handleFullscreen) self.shortcut = QShortcut(QKeySequence("i"), self) self.shortcut.activated.connect(self.handleInfo) self.shortcut = QShortcut(QKeySequence("s"), self) self.shortcut.activated.connect(self.toggleSlider) self.shortcut = QShortcut(QKeySequence(Qt.Key_Right), self) self.shortcut.activated.connect(self.forwardSlider) self.shortcut = QShortcut(QKeySequence(Qt.Key_Left), self) self.shortcut.activated.connect(self.backSlider) self.shortcut = QShortcut(QKeySequence(Qt.Key_Up), self) self.shortcut.activated.connect(self.volumeUp) self.shortcut = QShortcut(QKeySequence(Qt.Key_Down), self) self.shortcut.activated.connect(self.volumeDown) self.shortcut = QShortcut( QKeySequence(Qt.ShiftModifier + Qt.Key_Right), self) self.shortcut.activated.connect(self.forwardSlider10) self.shortcut = QShortcut(QKeySequence(Qt.ShiftModifier + Qt.Key_Left), self) self.shortcut.activated.connect(self.backSlider10) self.mediaPlayer.setVideoOutput(self.videoWidget) self.mediaPlayer.stateChanged.connect(self.mediaStateChanged) self.mediaPlayer.positionChanged.connect(self.positionChanged) self.mediaPlayer.durationChanged.connect(self.durationChanged) self.mediaPlayer.error.connect(self.handleError) print("QT5 Player started") print("press 'o' to open file (see context menu for more)") self.suspend_screensaver() def mouseDoubleClickEvent(self, event): self.handleFullscreen() def playFromURL(self): self.mediaPlayer.pause() self.myurl = self.clip.text() self.mediaPlayer.setMedia(QMediaContent(QUrl(self.myurl))) self.playButton.setEnabled(True) self.mediaPlayer.play() self.hideSlider() print(self.myurl) def getYTUrl(self): cmd = "youtube-dl -g -f best " + self.clip.text() print("grabbing YouTube URL") self.process.start(cmd) def dataReady(self): self.myurl = str(self.process.readAll(), encoding='utf8').rstrip() ### self.myurl = self.myurl.partition("\n")[0] print(self.myurl) self.clip.setText(self.myurl) self.playFromURL() def suspend_screensaver(self): 'suspend linux screensaver' proc = subprocess.Popen( 'gsettings set org.gnome.desktop.screensaver idle-activation-enabled false', shell=True) proc.wait() def resume_screensaver(self): 'resume linux screensaver' proc = subprocess.Popen( 'gsettings set org.gnome.desktop.screensaver idle-activation-enabled true', shell=True) proc.wait() def openFile(self): fileName, _ = QFileDialog.getOpenFileName( self, "Open Movie", QDir.homePath() + "/Videos", "Media (*.webm *.mp4 *.ts *.avi *.mpeg *.mpg *.mkv *.VOB *.m4v *.3gp *.mp3 *.m4a *.wav *.ogg *.flac *.m3u *.m3u8)" ) if fileName != '': self.loadFilm(fileName) print("File loaded") def play(self): if self.mediaPlayer.state() == QMediaPlayer.PlayingState: self.mediaPlayer.pause() else: self.mediaPlayer.play() def mediaStateChanged(self, state): if self.mediaPlayer.state() == QMediaPlayer.PlayingState: self.playButton.setIcon(self.style().standardIcon( QStyle.SP_MediaPause)) else: self.playButton.setIcon(self.style().standardIcon( QStyle.SP_MediaPlay)) def positionChanged(self, position): self.positionSlider.setValue(position) mtime = QTime(0, 0, 0, 0) mtime = mtime.addMSecs(self.mediaPlayer.position()) self.lbl.setText(mtime.toString()) def durationChanged(self, duration): self.positionSlider.setRange(0, duration) mtime = QTime(0, 0, 0, 0) mtime = mtime.addMSecs(self.mediaPlayer.duration()) self.elbl.setText(mtime.toString()) def setPosition(self, position): self.mediaPlayer.setPosition(position) def handleError(self): self.playButton.setEnabled(False) print("Error: ", self.mediaPlayer.errorString()) def handleQuit(self): self.mediaPlayer.stop() self.resume_screensaver() print("Goodbye ...") app.quit() def contextMenuRequested(self, point): menu = QMenu() actionFile = menu.addAction(QIcon.fromTheme("video-x-generic"), "open File (o)") actionclipboard = menu.addSeparator() actionURL = menu.addAction(QIcon.fromTheme("browser"), "URL from Clipboard (u)") actionclipboard = menu.addSeparator() actionYTurl = menu.addAction(QIcon.fromTheme("youtube"), "URL from YouTube (y)") actionclipboard = menu.addSeparator() actionToggle = menu.addAction(QIcon.fromTheme("next"), "show / hide Slider (s)") actionFull = menu.addAction(QIcon.fromTheme("view-fullscreen"), "Fullscreen (f)") action169 = menu.addAction(QIcon.fromTheme("tv-symbolic"), "16 : 9") action43 = menu.addAction(QIcon.fromTheme("tv-symbolic"), "4 : 3") actionSep = menu.addSeparator() actionInfo = menu.addAction(QIcon.fromTheme("help-about"), "Info (i)") action5 = menu.addSeparator() actionQuit = menu.addAction(QIcon.fromTheme("application-exit"), "Exit (q)") actionFile.triggered.connect(self.openFile) actionQuit.triggered.connect(self.handleQuit) actionFull.triggered.connect(self.handleFullscreen) actionInfo.triggered.connect(self.handleInfo) actionToggle.triggered.connect(self.toggleSlider) actionURL.triggered.connect(self.playFromURL) actionYTurl.triggered.connect(self.getYTUrl) action169.triggered.connect(self.screen169) action43.triggered.connect(self.screen43) menu.exec_(self.mapToGlobal(point)) def wheelEvent(self, event): mwidth = self.frameGeometry().width() mheight = self.frameGeometry().height() mleft = self.frameGeometry().left() mtop = self.frameGeometry().top() mscale = event.angleDelta().y() / 5 if self.widescreen == True: self.setGeometry(mleft, mtop, mwidth + mscale, round((mwidth + mscale) / 1.778)) else: self.setGeometry(mleft, mtop, mwidth + mscale, round((mwidth + mscale) / 1.33)) #elif self.positionSlider.hasFocus(): # self.positionSlider.value = self.positionSlider.value + 5 def screen169(self): self.widescreen = True mwidth = self.frameGeometry().width() mheight = self.frameGeometry().height() mleft = self.frameGeometry().left() mtop = self.frameGeometry().top() mratio = 1.778 self.setGeometry(mleft, mtop, mwidth, round(mwidth / mratio)) def screen43(self): self.widescreen = False mwidth = self.frameGeometry().width() mheight = self.frameGeometry().height() mleft = self.frameGeometry().left() mtop = self.frameGeometry().top() mratio = 1.33 self.setGeometry(mleft, mtop, mwidth, round(mwidth / mratio)) def handleFullscreen(self): if self.windowState() & Qt.WindowFullScreen: QApplication.setOverrideCursor(Qt.ArrowCursor) self.showNormal() print("no Fullscreen") else: self.showFullScreen() QApplication.setOverrideCursor(Qt.BlankCursor) print("Fullscreen entered") def handleInfo(self): msg = QMessageBox.about(self, "QT5 Player", self.myinfo) def toggleSlider(self): if self.positionSlider.isVisible(): self.hideSlider() else: self.showSlider() def hideSlider(self): self.playButton.hide() self.lbl.hide() self.positionSlider.hide() self.elbl.hide() mwidth = self.frameGeometry().width() mheight = self.frameGeometry().height() mleft = self.frameGeometry().left() mtop = self.frameGeometry().top() if self.widescreen == True: self.setGeometry(mleft, mtop, mwidth, round(mwidth / 1.778)) else: self.setGeometry(mleft, mtop, mwidth, round(mwidth / 1.33)) def showSlider(self): self.playButton.show() self.lbl.show() self.positionSlider.show() self.elbl.show() mwidth = self.frameGeometry().width() mheight = self.frameGeometry().height() mleft = self.frameGeometry().left() mtop = self.frameGeometry().top() if self.widescreen == True: self.setGeometry(mleft, mtop, mwidth, round(mwidth / 1.55)) else: self.setGeometry(mleft, mtop, mwidth, round(mwidth / 1.33)) def forwardSlider(self): self.mediaPlayer.setPosition(self.mediaPlayer.position() + 1000 * 60) def forwardSlider10(self): self.mediaPlayer.setPosition(self.mediaPlayer.position() + 10000 * 60) def backSlider(self): self.mediaPlayer.setPosition(self.mediaPlayer.position() - 1000 * 60) def backSlider10(self): self.mediaPlayer.setPosition(self.mediaPlayer.position() - 10000 * 60) def volumeUp(self): self.mediaPlayer.setVolume(self.mediaPlayer.volume() + 10) print("Volume: " + str(self.mediaPlayer.volume())) def volumeDown(self): self.mediaPlayer.setVolume(self.mediaPlayer.volume() - 10) print("Volume: " + str(self.mediaPlayer.volume())) def mousePressEvent(self, evt): self.oldPos = evt.globalPos() def mouseMoveEvent(self, evt): delta = QPoint(evt.globalPos() - self.oldPos) self.move(self.x() + delta.x(), self.y() + delta.y()) self.oldPos = evt.globalPos() def dragEnterEvent(self, event): if event.mimeData().hasUrls(): event.accept() elif event.mimeData().hasText(): event.accept() else: event.ignore() def dropEvent(self, event): print("drop") if event.mimeData().hasUrls(): url = event.mimeData().urls()[0].toString() print("url = ", url) self.mediaPlayer.stop() self.mediaPlayer.setMedia(QMediaContent(QUrl(url))) self.playButton.setEnabled(True) self.mediaPlayer.play() elif event.mimeData().hasText(): mydrop = event.mimeData().text() ### YouTube url if "youtube" in mydrop: print("is YouTube", mydrop) self.clip.setText(mydrop) self.getYTUrl() else: ### normal url print("generic url = ", mydrop) self.mediaPlayer.setMedia(QMediaContent(QUrl(mydrop))) self.playButton.setEnabled(True) self.mediaPlayer.play() self.hideSlider() def loadFilm(self, f): self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(f))) self.playButton.setEnabled(True) self.mediaPlayer.play() def printMediaData(self): if self.mediaPlayer.mediaStatus() == 6: if self.mediaPlayer.isMetaDataAvailable(): res = str(self.mediaPlayer.metaData("Resolution")).partition( "PyQt5.QtCore.QSize(")[2].replace(", ", "x").replace(")", "") print("%s%s" % ("Video Resolution = ", res)) if int(res.partition("x")[0]) / int( res.partition("x")[2]) < 1.5: self.screen43() else: self.screen169() else: print("no metaData available") def openFileAtStart(self, filelist): matching = [s for s in filelist if ".myformat" in s] if len(matching) > 0: self.loadFilm(matching)
class Player(QWidget): media_loaded = Signal(str) stopped = Signal(str) playlist_size_changed = Signal(int) handle_double_click = Slot() def __init__(self, parent=None): super(Player, self).__init__(parent) self.duration = 0 self.volume = 50 self.player = QMediaPlayer() self.playlist = Playlist(self) self.videoWidget = VideoWidget() self.next_url = QUrl() self.context_menu = QMenu(self) self.display_splitter = QSplitter(Qt.Horizontal) self.repeat_control = RepeatControl(parent=self) self.repeat_control.get_player_position = self.player.position self.repeat_control.set_position_to_player = self.player.setPosition self.player.positionChanged.connect(self.repeat_control.set_pos) self.setAcceptDrops(True) std_icon = self.style().standardIcon self.play_button = create_flat_button(std_icon(QStyle.SP_MediaPlay)) self.stopButton = create_flat_button(std_icon(QStyle.SP_MediaStop), '') self.backwardButton = create_flat_button( std_icon(QStyle.SP_MediaSkipBackward), '') self.forwardButton = create_flat_button( std_icon(QStyle.SP_MediaSkipForward), '') self.order_list = self.repeat_control.menu() self.order_list.setFixedWidth(115) self.playback_rate_menu = QComboBox() self.playback_rate_menu.addItems( ('0.5x', '0.75x', '0.9x', '1.0x', '1.1x', '1.25x', '1.5x')) self.playback_rate_menu.setCurrentText('1.0x') self.muteButton = create_flat_button( std_icon(QStyle.SP_MediaVolume if not self.player.isMuted() else QStyle.SP_MediaVolumeMuted)) self.volumeBar = QSlider(Qt.Horizontal) self.volumeBar.setRange(0, 100) self.volumeBar.setValue(self.volume) self.labelVolume = QLabel(str(self.volume)) self.labelVolume.setMinimumWidth(24) self.statusInfoLabel = QLabel() self.seekBar = QSlider(Qt.Horizontal) self.seekBar.setRange(0, self.player.duration() / 1000) self.labelTotalTime = QLabel('00:00') self.labelCurrentTime = QLabel('00:00') self.create_layout() self.create_connections() self.player.setVideoOutput(self.videoWidget) self.videoWidget.show() def create_layout(self): seekBarLayout = QHBoxLayout() seekBarLayout.addWidget(self.labelCurrentTime) seekBarLayout.addWidget(self.seekBar) seekBarLayout.addWidget(self.labelTotalTime) controlWithoutSeekBarLayout = QHBoxLayout() controlWithoutSeekBarLayout.setSpacing(1) controlWithoutSeekBarLayout.addWidget(self.play_button) controlWithoutSeekBarLayout.addWidget(self.stopButton) controlWithoutSeekBarLayout.addWidget(self.backwardButton) controlWithoutSeekBarLayout.addWidget(self.forwardButton) controlWithoutSeekBarLayout.addWidget(self.order_list) controlWithoutSeekBarLayout.addWidget(self.playback_rate_menu) controlWithoutSeekBarLayout.addStretch(stretch=2) controlWithoutSeekBarLayout.addWidget(self.muteButton) controlWithoutSeekBarLayout.addWidget(self.volumeBar) controlWithoutSeekBarLayout.addWidget(self.labelVolume, alignment=Qt.AlignRight) controlLayout = QVBoxLayout() controlLayout.addLayout(seekBarLayout) controlLayout.addLayout(controlWithoutSeekBarLayout) self.display_splitter.setOpaqueResize(False) self.display_splitter.addWidget(self.videoWidget) self.display_splitter.addWidget(self.playlist.widget) self.display_splitter.setSizes([300, 200]) layout = QVBoxLayout() layout.setContentsMargins(11, 0, 11, 0) layout.addWidget(self.display_splitter, 1) layout.addLayout(controlLayout) layout.addWidget(self.repeat_control.ab_repeat_widget) layout.addWidget(self.statusInfoLabel) self.setLayout(layout) def create_connections(self): self.play_button.clicked.connect(self.optimal_play) self.stopButton.clicked.connect(self.stop) self.backwardButton.clicked.connect(self.skip_backward) self.forwardButton.clicked.connect(self.skip_forward) self.muteButton.clicked.connect(self.toggleMute) self.playback_rate_menu.currentTextChanged.connect( self.set_playback_rate) self.player.stateChanged.connect(self.playerStateChanged) self.player.mediaStatusChanged.connect(self.mediaStatusChanged) self.player.durationChanged.connect(self.durationChanged) self.player.positionChanged.connect(self.positionChanged) self.player.error.connect(self.handleError) self.volumeBar.sliderMoved.connect(self.setVolume) self.volumeBar.sliderReleased.connect(self.setVolume) self.volumeBar.valueChanged.connect(self.volumeChanged) self.seekBar.sliderMoved.connect(self.seek) self.seekBar.sliderReleased.connect(self.seekBarClicked) self.repeat_control.pos_exceeded.connect(self.seek) self.player.currentMediaChanged.connect(self.repeat_control.reset) self.playlist.double_clicked.connect(self.load_and_play) self.videoWidget.double_clicked.connect(self.no_future) def contextMenuEvent(self, event): self.context_menu.exec_(event.globalPos()) def read_settings(self): settings = QSettings() settings.beginGroup('player') self.order_list.setCurrentIndex(settings.value('order_list', 0)) self.display_splitter.restoreState( QByteArray(settings.value('splitter_sizes'))) settings.endGroup() self.playlist.read_settings() def no_future(self): self.display_splitter.moveSplitter(0, 1) def autoplay(self): """メディアを読み込み、再生する。 order_listに応じて、次に何を再生するかを決める。 """ i = self.order_list.currentIndex() if i == 1: # self.repeat_track() return elif i == 2: self.repeat_all() else: self.next_track() self.play() def optimal_play(self): if self.player.state() == QMediaPlayer.StoppedState: self.load_and_play() else: self.play() def load_and_play(self): self.load(self.playlist.get_new_one()) self.play() def load(self, file_url: QUrl): if file_url is None: return None if file_url.isValid(): c = QMediaContent(file_url) self.player.setMedia(c) self.media_loaded.emit(self.playlist.current_title()) self.enableInterface() def play(self): if self.player.state() == QMediaPlayer.PlayingState: self.player.pause() return if self.player.mediaStatus() == QMediaPlayer.LoadingMedia or\ self.player.mediaStatus() == QMediaPlayer.StalledMedia: QTimer.singleShot(600, self.player.play) self.player.play() self.playlist.update_listview() def stop(self): if not self.player.state() == QMediaPlayer.StoppedState: self.seek(0) self.player.stop() self.setStatusInfo('Stopped') self.stopped.emit('') def playerStateChanged(self, state): if state == QMediaPlayer.PlayingState: self.play_button.setIcon(self.style().standardIcon( QStyle.SP_MediaPause)) else: self.play_button.setIcon(self.style().standardIcon( QStyle.SP_MediaPlay)) def durationChanged(self, duration): self.repeat_control.set_duration(duration) duration /= 1000 self.duration = duration totalTime = QTime((duration / 3600) % 60, (duration / 60) % 60, (duration % 60), (duration * 1000) % 1000) format = 'hh:mm:ss' if duration > 3600 else 'mm:ss' totalTimeStr = totalTime.toString(format) self.labelTotalTime.setText(totalTimeStr) self.seekBar.setMaximum(duration) def positionChanged(self, progress): progress /= 1000 self.updateCurrentTime(progress) self.seekBar.setValue(progress) def updateCurrentTime(self, currentInfo): if currentInfo: currentTime = QTime((currentInfo / 3600) % 60, (currentInfo / 60) % 60, currentInfo % 60, (currentInfo * 1000) % 1000) format = 'hh:mm:ss' if self.duration > 3600 else 'mm:ss' currentTimeStr = currentTime.toString(format) else: currentTimeStr = '00:00' self.labelCurrentTime.setText(currentTimeStr) def repeat_track(self): QTimer.singleShot(50, self.play) def repeat_all(self): if self.playlist.count() - 1 == self.playlist.current_row(): url = self.playlist.first() self.load(url) else: self.next_track() def setVolume(self): self.player.setVolume(self.volumeBar.sliderPosition() * 2) def volumeChanged(self): self.labelVolume.setText(str(self.volumeBar.sliderPosition())) self.volume = self.volumeBar.sliderPosition() def seekBarClicked(self): self.seek(self.seekBar.sliderPosition()) def seek(self, seconds): self.player.setPosition(seconds * 1000) def set_playback_rate(self, rate_text): self.player.setPlaybackRate(float(rate_text[:-1])) def toggleMute(self): if self.player.isMuted(): self.player.setMuted(False) self.muteButton.setIcon(self.style().standardIcon( QStyle.SP_MediaVolume)) else: self.player.setMuted(True) self.muteButton.setIcon(self.style().standardIcon( QStyle.SP_MediaVolumeMuted)) def disableInterface(self): self.play_button.setEnabled(False) self.stopButton.setEnabled(False) self.backwardButton.setEnabled(False) self.forwardButton.setEnabled(False) self.labelCurrentTime.setText('00:00') self.labelTotalTime.setText('00:00') def enableInterface(self): self.play_button.setEnabled(True) self.stopButton.setEnabled(True) self.backwardButton.setEnabled(True) self.forwardButton.setEnabled(True) def mediaStatusChanged(self, status): if status == QMediaPlayer.LoadingMedia: self.setStatusInfo('Loading...') elif status == QMediaPlayer.BufferingMedia: self.setStatusInfo('Buffering') elif status == QMediaPlayer.EndOfMedia: # self.player.stop() self.autoplay() elif status == QMediaPlayer.InvalidMedia: self.handleError() #TODO: Step Forward を割り当てる def clearStatusInfo(self): self.statusInfoLabel.setText("") def handleError(self): self.disableInterface() self.setStatusInfo('Error: ' + self.player.errorString()) def setStatusInfo(self, message, seconds=5): if not message == '': self.statusInfoLabel.setText(message) QTimer.singleShot(seconds * 1000, self.clearStatusInfo) def next_track(self): url = self.playlist.next() if url is None: return None else: self.load(url) def previous_track(self): url = self.playlist.previous() if url is None: return None else: self.load(url) def skip_forward(self): self.next_track() self.play() def skip_backward(self): if self.seekBar.sliderPosition() > 2: self.seek(0) else: self.previous_track() self.play() def forward(self, seconds): currentPosition = self.seekBar.sliderPosition() if currentPosition + seconds < self.duration: self.seek(currentPosition + seconds) else: self.seek(self.duration - 1) def backward(self, seconds): self.forward(-seconds) def forward_short(self): self.forward(SeekStep.SHORT) def forward_medium(self): self.forward(SeekStep.MEDIUM) def forward_long(self): self.forward(SeekStep.LONG) def forward_verylong(self): self.forward(SeekStep.VERYLONG) def backward_short(self): self.backward(SeekStep.SHORT) def backward_medium(self): self.backward(SeekStep.MEDIUM) def backward_long(self): self.backward(SeekStep.LONG) def backward_verylong(self): self.backward(SeekStep.VERYLONG) def dragEnterEvent(self, event): if event.mimeData().hasUrls(): event.accept() def dragMoveEvent(self, event): if event.mimeData().hasUrls(): event.accept() def dropEvent(self, event): if event.mimeData().hasUrls(): urls = event.mimeData().urls() self.load(urls[0]) # self.stop() self.play()
class VideoWindow(QMainWindow): def __init__(self, parent=None): super(VideoWindow, self).__init__(parent) self.setWindowTitle( "PyQt Video Player Widget Example - pythonprogramminglanguage.com") self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface) videoWidget = QVideoWidget() self.playButton = QPushButton() self.playButton.setEnabled(False) self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay)) self.playButton.clicked.connect(self.play) self.positionSlider = QSlider(Qt.Horizontal) self.positionSlider.setRange(0, 0) self.positionSlider.sliderMoved.connect(self.setPosition) self.errorLabel = QLabel() self.errorLabel.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum) # Create new action openAction = QAction(QIcon('open.png'), '&Open', self) openAction.setShortcut('Ctrl+O') openAction.setStatusTip('Open movie') openAction.triggered.connect(self.openFile) # Create exit action exitAction = QAction(QIcon('exit.png'), '&Exit', self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit application') exitAction.triggered.connect(self.exitCall) # Create menu bar and add action menuBar = self.menuBar() fileMenu = menuBar.addMenu('&File') #fileMenu.addAction(newAction) fileMenu.addAction(openAction) fileMenu.addAction(exitAction) # Create a widget for window contents wid = QWidget(self) self.setCentralWidget(wid) # Create layouts to place inside widget controlLayout = QHBoxLayout() controlLayout.setContentsMargins(0, 0, 0, 0) controlLayout.addWidget(self.playButton) controlLayout.addWidget(self.positionSlider) layout = QVBoxLayout() layout.addWidget(videoWidget) layout.addLayout(controlLayout) layout.addWidget(self.errorLabel) # Set widget to contain window contents wid.setLayout(layout) self.mediaPlayer.setVideoOutput(videoWidget) self.mediaPlayer.stateChanged.connect(self.mediaStateChanged) self.mediaPlayer.positionChanged.connect(self.positionChanged) self.mediaPlayer.durationChanged.connect(self.durationChanged) self.mediaPlayer.error.connect(self.handleError) def openFile(self): fileName, _ = QFileDialog.getOpenFileName(self, "Open Movie", QDir.homePath()) if fileName != '': self.mediaPlayer.setMedia( QMediaContent(QUrl.fromLocalFile(fileName))) self.playButton.setEnabled(True) def exitCall(self): sys.exit(app.exec_()) def play(self): if self.mediaPlayer.state() == QMediaPlayer.PlayingState: self.mediaPlayer.pause() else: self.mediaPlayer.play() def mediaStateChanged(self, state): if self.mediaPlayer.state() == QMediaPlayer.PlayingState: self.playButton.setIcon(self.style().standardIcon( QStyle.SP_MediaPause)) else: self.playButton.setIcon(self.style().standardIcon( QStyle.SP_MediaPlay)) def positionChanged(self, position): self.positionSlider.setValue(position) def durationChanged(self, duration): self.positionSlider.setRange(0, duration) def setPosition(self, position): self.mediaPlayer.setPosition(position) def handleError(self): self.playButton.setEnabled(False) self.errorLabel.setText("Error: " + self.mediaPlayer.errorString())