-
Notifications
You must be signed in to change notification settings - Fork 231
/
buffer.py
96 lines (79 loc) · 3.84 KB
/
buffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Andy Stewart
#
# Author: Andy Stewart <lazycat.manatee@gmail.com>
# Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from PyQt5.QtCore import QSizeF, Qt, QUrl
from PyQt5.QtGui import QBrush, QColor
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QGraphicsVideoItem
from PyQt5.QtWidgets import QWidget, QGraphicsScene, QGraphicsView, QVBoxLayout
from buffer import Buffer
class VideoPlayerBuffer(Buffer):
def __init__(self, buffer_id, url, width, height):
Buffer.__init__(self, buffer_id, url, width, height, True, QColor(0, 0, 0, 255))
self.add_widget(VideoPlayerWidget())
self.buffer_widget.play(url)
self.buffer_widget.video_item.setSize(QSizeF(width, height))
def all_views_hide(self):
# Pause video before all views hdie, otherwise will got error "Internal data stream error".
if self.buffer_widget.media_player.state() == QMediaPlayer.PlayingState:
self.buffer_widget.media_player.pause()
self.buffer_widget.video_need_replay = True
def some_view_show(self):
if self.buffer_widget.video_need_replay == True:
self.buffer_widget.media_player.play()
class VideoPlayerWidget(QWidget):
def __init__(self, parent=None):
super(VideoPlayerWidget, self).__init__(parent)
self.scene = QGraphicsScene(self)
self.scene.setBackgroundBrush(QBrush(QColor(0, 0, 0, 255)))
self.graphics_view = QGraphicsView(self.scene)
self.graphics_view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.graphics_view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.graphics_view.setFrameStyle(0)
self.graphics_view.setStyleSheet("QGraphicsView {background: transparent; border: 3px; outline: none;}")
self.video_item = QGraphicsVideoItem()
self.scene.addItem(self.video_item)
self.layout = QVBoxLayout(self)
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.addWidget(self.graphics_view)
self.media_player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.media_player.setVideoOutput(self.video_item)
self.video_need_replay = False
self.video_seek_durcation = 3000 # in milliseconds
def play(self, url):
self.media_player.setMedia(QMediaContent(QUrl.fromLocalFile(url)))
self.media_player.play()
def seek_forward(self):
video_position = self.media_player.position()
self.media_player.setPosition(video_position + self.video_seek_durcation)
def seek_backward(self):
video_position = self.media_player.position()
self.media_player.setPosition(max(video_position - self.video_seek_durcation, 0))
def keyPressEvent(self, event):
if event.key() == Qt.Key_Space:
if self.media_player.state() == QMediaPlayer.PlayingState:
self.media_player.pause()
self.video_need_replay = False
else:
self.media_player.play()
self.video_need_replay = True
elif event.key() == Qt.Key_H:
self.seek_backward()
elif event.key() == Qt.Key_L:
self.seek_forward()