-
Notifications
You must be signed in to change notification settings - Fork 5
/
video_viewer.py
124 lines (94 loc) · 4.43 KB
/
video_viewer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from PySide6.QtWidgets import (QHBoxLayout,
QPushButton, QSlider, QStyle, QVBoxLayout, QWidget)
from PySide6.QtGui import QShortcut
from PySide6.QtCore import Qt,QUrl
from PySide6.QtMultimedia import QMediaPlayer,QAudioOutput
from PySide6.QtMultimediaWidgets import QVideoWidget
class VideoViewer(QWidget):
def __init__(self, parent=None):
super(VideoViewer, self).__init__(parent)
self.setFocus()
self.media_player = QMediaPlayer()
self.videoWidget = QVideoWidget()
self.current_file = None
#play button
self.play_button = QPushButton()
self.play_button.setFixedHeight(24)
self.play_button.setIcon(self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPlay))
self.play_button.pressed.connect(self.play)
#audio output
self.audio_output = QAudioOutput()
self.media_player.setAudioOutput(self.audio_output)
#position slider
self.position_slider = QSlider(Qt.Orientation.Horizontal)
self.position_slider.setRange(0, 0)
#in order to avoid glitch the media is paused when the slider is moved
self.position_slider.sliderPressed.connect(lambda: self.media_player.pause())
self.position_slider.sliderReleased.connect(self.setPosition)
controls_layout = QHBoxLayout()
controls_layout.setContentsMargins(0, 0, 0, 0)
controls_layout.addWidget(self.play_button)
controls_layout.addWidget(self.position_slider)
main_layout = QVBoxLayout()
main_layout.addWidget(self.videoWidget)
main_layout.addLayout(controls_layout)
self.setLayout(main_layout)
self.media_player.setVideoOutput(self.videoWidget)
self.media_player.playbackStateChanged.connect(self.playbackStateChanged)
self.media_player.positionChanged.connect(self.positionChanged)
self.media_player.durationChanged.connect(self.durationChanged)
self.set_shortcut()
def open(self,path):
self.current_file = path
self.videoWidget.setFocus()
self.media_player.setSource(QUrl.fromLocalFile(path))
self.play()
def play(self):
if self.media_player.playbackState() == QMediaPlayer.PlaybackState.PlayingState:
self.media_player.pause()
elif self.media_player.playbackState() == QMediaPlayer.PlaybackState.PausedState:
self.media_player.play()
elif self.media_player.playbackState() == QMediaPlayer.PlaybackState.StoppedState:
self.media_player.setSource(QUrl.fromLocalFile(self.current_file))
self.media_player.play()
def playbackStateChanged(self, state):
if state == QMediaPlayer.PlaybackState.PlayingState:
self.play_button.setIcon(
self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPause))
else:
self.play_button.setIcon(
self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPlay))
def positionChanged(self, position):
self.position_slider.setValue(position)
def durationChanged(self, duration):
self.position_slider.setRange(0, duration)
def setPosition(self):
self.media_player.setPosition(self.position_slider.value())
self.play()
def time_back(self):
#milliseconds: 10 sec
mseconds_back = 10000
current_pos = self.media_player.position()
if current_pos - mseconds_back > 0:
self.media_player.setPosition(current_pos-mseconds_back)
if self.media_player.playbackState() == QMediaPlayer.PlaybackState.StoppedState:
self.play()
else:
self.media_player.setPosition(0)
def time_forward(self):
total_time = self.media_player.duration()
#milliseconds: 10 sec
mseconds_forward= 10000
current_pos = self.media_player.position()
if mseconds_forward + current_pos < total_time:
self.media_player.setPosition( mseconds_forward + current_pos)
else:
# reaches end of video.
# -100 is used to trigger the "playbackStateChanged" signal.
self.media_player.setPosition(total_time-100)
def set_shortcut(self):
QShortcut(Qt.Key.Key_Down, self).activated.connect(self.time_back)
QShortcut(Qt.Key.Key_Up, self).activated.connect(self.time_forward)
def hide(self) -> None:
self.media_player.stop()
return super().hide()