-
Notifications
You must be signed in to change notification settings - Fork 0
/
avplayer.py
229 lines (195 loc) · 7.08 KB
/
avplayer.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from ctypes import c_void_p
import AVFoundation
from Cocoa import NSURL, NSMakeRect
import CoreMedia
import MediaToolbox
import objc
from PyQt5.QtCore import Qt, pyqtSignal, QTimer
from PyQt5.QtWidgets import QWidget
class VideoWidget(QWidget):
mediaReady = pyqtSignal(bool)
mousePressed = pyqtSignal()
doubleClicked = pyqtSignal()
########################################
#
########################################
def __init__(self, parent=None):
super().__init__()
self.filename = None
self._volume = 1.
self._muted = False
self._player = None
self._playerLayer = None
self._timer = QTimer(self)
self._timer.setInterval(50)
self._timer.timeout.connect(self.__check_ready)
# make window background black
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.black)
self.setPalette(p)
# cast QWidget to NSView
self._view = objc.objc_object(c_void_p=c_void_p(int(self.winId())))
self._view.setWantsLayer_(True)
MediaToolbox.MTRegisterProfessionalVideoWorkflowFormatReaders()
########################################
#
########################################
def __check_ready(self):
status = self._player.currentItem().status()
if status:
self._timer.stop()
if status != 1:
self.filename = None
self.mediaReady.emit(status == 1) # 2 means failed
########################################
#
########################################
def load_media(self, filename: str):
if self._player is not None:
self.close_media()
self.filename = filename
if filename.startswith('http://') or filename.startswith('https://'):
url = NSURL.URLWithString_(filename)
else:
url = NSURL.fileURLWithPath_(filename)
self._player = AVFoundation.AVPlayer.playerWithURL_(url)
# create AVPlayerLayer
self._playerLayer = AVFoundation.AVPlayerLayer.playerLayerWithPlayer_(self._player)
g = self.geometry()
self._playerLayer.setFrame_(NSMakeRect(0, 0, g.width(), g.height()))
self._playerLayer.setAutoresizingMask_(18) # kCALayerWidthSizable=2 | kCALayerHeightSizable=16
self._view.layer().addSublayer_(self._playerLayer)
self._player.setVolume_(0 if self._muted else self._volume)
self._timer.start()
########################################
#
########################################
def close_media(self):
self.filename = None
if self._player:
self._player.setRate_(0.)
self._player = None
if self._playerLayer:
self._playerLayer.removeFromSuperlayer()
self._playerLayer = None
self.repaint()
########################################
#
########################################
def step(self, steps: int=1):
if self._player is None:
return
self._player.currentItem().stepByCount_(steps)
########################################
#
########################################
def get_natural_size(self):
if self._player is None:
return
video_tracks = self._player.currentItem().asset().tracksWithMediaType_(AVFoundation.AVMediaTypeVideo)
if len(video_tracks):
size = video_tracks[0].naturalSize()
return size.width, size.height
########################################
# as seconds (floaz)
########################################
def get_duration(self):
if self._player is None:
return
cm = self._player.currentItem().duration()
return cm.value / cm.timescale if cm.timescale else 0
########################################
#
########################################
def get_fps(self):
if self._player is None:
return
video_tracks = self._player.currentItem().asset().tracksWithMediaType_(AVFoundation.AVMediaTypeVideo)
if len(video_tracks):
return video_tracks[0].nominalFrameRate()
########################################
#
########################################
def has_video(self):
if self._player is None:
return False
return len(self._player.currentItem().asset().tracksWithMediaType_(AVFoundation.AVMediaTypeVideo)) > 0
########################################
#
########################################
def has_audio(self):
if self._player is None:
return False
return len(self._player.currentItem().asset().tracksWithMediaType_(AVFoundation.AVMediaTypeAudio)) > 0
########################################
# 0..1 (float)
########################################
def get_volume(self):
return self._volume
########################################
# 0..1 (float)
########################################
def set_volume(self, volume: float):
self._volume = volume
if self._player is None:
return
if not self._muted:
self._player.setVolume_(float(volume))
########################################
#
########################################
def set_muted(self, flag: bool):
self._muted = flag
if self._player:
self._player.setVolume_(0 if flag else self._volume)
########################################
# as seconds (float)
########################################
def seek_to_time(self, sec: float):
if self._player is None:
return
cm = self._player.currentItem().duration()
cm.value = cm.timescale * sec
self._player.seekToTime_(cm)
########################################
# as seconds (float)
########################################
def get_time(self):
if self._player is None:
return
cm = self._player.currentTime()
return cm.value / cm.timescale if cm.timescale else 0
########################################
#
########################################
def play(self):
if self._player is None:
return
self._player.setRate_(1.0)
########################################
#
########################################
def pause(self):
if self._player is None:
return
self._player.setRate_(0.0)
########################################
# returns is_playing as bool
########################################
def toggle_playback(self):
if self._player is None:
return False
rate = 1 - self._player.rate()
self._player.setRate_(rate)
return rate > 0
########################################
#
########################################
def mousePressEvent(self, e):
self.mousePressed.emit()
########################################
#
########################################
def mouseDoubleClickEvent(self, e):
self.doubleClicked.emit()