-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracks.py
532 lines (433 loc) · 18.9 KB
/
tracks.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
from PyQt5 import QtWidgets, QtGui, QtCore, QtMultimedia, QtMultimediaWidgets
import os, time, datetime, operator
from pymediainfo import MediaInfo
from urllib.parse import unquote
from math import floor
from alignments import AdjustClipPosDialog
class Clip(QtWidgets.QPushButton):
EMPTY = 0
VIDEO = 1
SEEKSTEP = 1000
def __init__(self, parent=None, url='', sPos=0, name=None):
super().__init__(parent)
self.sPos = sPos
self.mediatype = Clip.EMPTY
self.duration = 0
if url[0] == '/':
self.mrl = "file://" + url
else:
self.mrl = url
mi = MediaInfo.parse(url)
for t in mi.tracks:
if t.track_type == "Video":
self.mediatype = Clip.VIDEO
self.duration = t.duration
break
# calculate the new end position with video length.
self.ePos = self.sPos + self.duration
self.name = name if name else unquote(os.path.basename(self.mrl))
self.setText(self.name)
self.clicked.connect(self.adjustPosDialog)
# print(f'Clip {self.name} has been placed betweed {self.sPos} and {self.ePos}, a duration of {self.durMsStr()}')
def durMsStr(self, timeInMS=None):
if timeInMS == None:
timeInMS = self.duration
return str(datetime.timedelta(seconds=timeInMS/1000))
def adjustPosDialog(self):
msShift, choose = AdjustClipPosDialog.getMsShift(self)
if choose == QtWidgets.QDialog.Accepted:
self.adjustPos(msShift)
self.parent().trackUpdated.emit()
def adjustPos(self, shiftMS):
print (f'Move {self.name} by {shiftMS} ms.')
siblings = self.parent().clips
# Get the index of current clip, as it must has existed.
idx = siblings.index(self)
print (f"Moving the {idx+1} clip of the track...", end="")
if (idx == 0 and self.sPos + shiftMS < 0) or (idx > 0 and self.sPos + shiftMS <= siblings[idx-1].ePos):
msg = QtWidgets.QMessageBox()
msg.setIcon(QtWidgets.QMessageBox.Critical)
msg.setText("Clip can't start before track starts or overlap with the perious one.")
msg.setWindowTitle("Error")
msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg.exec_()
return
while idx < len(siblings):
siblings[idx].sPos += shiftMS
siblings[idx].ePos += shiftMS
idx += 1
# Update the track length.
self.parent().ePos = siblings[idx-1].ePos
print ("done.")
class PlayerWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.track = None
self.videoframe = QtMultimediaWidgets.QVideoWidget()
self.videoframe.setContentsMargins(0,0,0,0)
box = QtWidgets.QVBoxLayout()
box.addWidget(self.videoframe)
self.setLayout(box)
def setTrack(self, trk):
self.track = trk
def keyPressEvent(self, e):
if self.track.player.state() == QtMultimedia.QMediaPlayer.PlayingState:
if e.key() == QtCore.Qt.Key_Up:
vol = self.track.player.volume()
if vol <= 90: self.track.player.setVolume(vol+10)
elif e.key() == QtCore.Qt.Key_Down:
vol = self.track.player.volume()
if vol >= 10: self.track.player.setVolume(vol-10)
elif e.key() == QtCore.Qt.Key_Right:
pos = self.track.player.position()
self.track.player.setPosition( pos + Clip.SEEKSTEP )
self.track.curClip.adjustPos(Clip.SEEKSTEP)
elif e.key() == QtCore.Qt.Key_Left:
pos = self.track.player.position()
self.track.player.setPosition( pos - Clip.SEEKSTEP )
self.track.curClip.adjustPos(Clip.SEEKSTEP * -1)
def mouseReleaseEvent(self, e):
modifier = e.modifiers()
if modifier == QtCore.Qt.ControlModifier:
self.track.setMarker()
elif modifier == QtCore.Qt.ShiftModifier:
self.track.syncClipToMarker()
elif modifier == QtCore.Qt.AltModifier:
self.track.setMarker(False)
else:
pass
class Track(QtWidgets.QLabel):
# Custom Signals
trackUpdated = QtCore.pyqtSignal()
def __init__(self, parent=None, trackNo=1):
super().__init__(parent)
self.mainWindow = parent.mainWindow
self.no = trackNo
# Track has no duration, they share the max duration of tracks.
self.ePos = 0
self.clips = []
self.curClip = None
self.nextClip = None
self.setContentsMargins(0,0,0,0)
self.setText(f"Track {trackNo}")
if (trackNo % 2):
self.setStyleSheet("background-color:#E5E4E2")
else:
self.setStyleSheet("background-color:#C0C0C0")
self.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.setAcceptDrops(True)
# the QT player
self.player = QtMultimedia.QMediaPlayer()
# the Player Window
self.playerW = PlayerWidget()
self.playerW.setWindowTitle(f"Track {self.no}")
qs = QtWidgets.QApplication.primaryScreen()
swidth = qs.availableSize().width()
sheight = qs.availableSize().height()
tw = int(swidth*0.5)
th = int(sheight*0.5)
toTop = 70 * int (trackNo / 2 - 0.5) if trackNo > 2 else 0
if not trackNo % 2:
toRight = tw
else:
toRight = 0
self.playerW.setGeometry(toRight, toTop, tw, th)
# print(f"Resize player window to {tw}x{th}")
self.player.setVideoOutput(self.playerW.videoframe)
# Timer use for play next clip.
self.timer = QtCore.QTimer(self)
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.playSchedClip)
self.player.stateChanged.connect(self.playerStateChange)
self.playerW.setTrack(self)
self.playerW.show()
def addClips(self, clips):
# Create new Clips from list of dictionary and added them to track.
for c in clips:
clip = Clip(self, c['url'], sPos=c['startPosition'], name=c['name'])
self.clips.append(clip)
# print(f"{clip.name} ({clip.durMsStr()}) has been added to Track {self.no} (with a width {self.width()}). ")
# And track end postition need to be updated, otherwise total duration of tracks will fail.
if clips: self.ePos = max(self.clips, key=operator.attrgetter('ePos')).ePos
def appendClip(self, clip):
# Append a clip and extend the track end position
self.clips.append(clip)
# Now updated the track duration with end position of the new track.
self.ePos = clip.ePos
self.mainWindow.statusBar().showMessage(f"{clip.name} ({clip.durMsStr()}) has been appended to Track {self.no} . ")
# print(f"{clip.name} ({clip.durMsStr()}) has been appended to Track {self.no} (with a width {self.width()}). ")
def getRightPixByDur(self, dur):
tWidth = self.width()
tDur = self.parent().totalDuration
pix = int( tWidth * ( dur / tDur ) )
# print(f'Width of all track are {tWidth} pixels for {tDur}ms, {dur}ms takes {pix} pixels.')
return pix
def popClipBtn(self):
# Sort clips to make sure it's in order for the timer.
self.clips.sort(key=operator.attrgetter('sPos'))
# Ensure total duration in track is updated for correct calculating
for c in self.clips:
c.setContentsMargins(0,0,0,0)
# c.setCheckable(True)
c.setToolTip(f'{c.durMsStr(c.sPos)} -> {c.durMsStr(c.ePos)}\n{c.name}({c.durMsStr()})')
c.setGeometry(QtCore.QRect(QtCore.QPoint(self.getRightPixByDur(c.sPos), 0), QtCore.QSize(self.getRightPixByDur(c.duration), 18)))
c.show()
# print(f'Draw a box of {self.getRightPixByDur(c.duration)}x18 and placed at {self.getRightPixByDur(c.sPos)} pix from right for {c.name}({c.duration})')
def getClipsByPos(self, tpos):
# return the current clip and next clip at pos of tracks, if NA, return None.
#Assuming Blank Track.
cC = None
nC = None
# print(f'Getting clips at {tpos}:')
for c in self.clips:
if c.sPos <= tpos and c.ePos > tpos:
cC = c
continue
if c.sPos > tpos:
nC = c
break
return [cC, nC]
def play(self, tPos):
# Play the current media of track from given position(of the Tracks).
self.player.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl(self.curClip.mrl)))
# set the position of media if appliable (A/V)
# calculated by minus the currnet postion and clip sPos
absPos = tPos - self.curClip.sPos
if absPos > 1000:
self.player.setPosition(absPos)
print(f'Seeked to {absPos} of {self.curClip.name}, playing...')
# print(f"Track.{self.no}: Player state: {self.player.state()}")
self.playerW.setWindowTitle(f'Track {self.no}: {self.curClip.name}')
self.playerW.show()
self.player.play()
self.playerW.setWindowState(QtCore.Qt.WindowActive)
def schNC(self, nextClip):
# Set the timer for next clip (playSchedClip()).
self.nextClip = nextClip
tpos = self.parent().getCurPos()
intV = self.nextClip.sPos - tpos
self.timer.setInterval(intV)
self.timer.start()
print(f'Track {self.no}: Timer set for {nextClip.name}, with a interval of {intV}.')
def playFrom(self, tpos):
# Play from the given position.
# If not a empty track:
if len(self.clips) > 0:
# Play from given position of the tracks.
# First, get the CLIPs at the given position.
cps = self.getClipsByPos(tpos)
cP = cps[0]
nP = cps[1]
# print(cps, self.curClip, self.nextClip)
if cP == None:
# Schedule the next clip if there has.
print(f'Track.{self.no}: Nothing to play now.')
self.curClip = None
self.playerW.hide()
elif self.curClip == cP:
# If it's the current Clip of the track, then open the media for play.
print(f'Track.{self.no}: Resume the current media {self.curClip.name} from tracks postion {tpos}.')
self.play(tpos)
else:
# Replace the current media, and play.
print(f'Track.{self.no}: Replace {self.curClip} with {cP} and play from {tpos}.')
self.curClip = cP
self.play(tpos)
# if there's Next Clip, sechedue it.
if nP:
self.schNC(nP)
elif not self.curClip:
self.playerW.hide()
else: # No clip in this track.
self.playerW.hide()
def playerStateChange(self):
if self.player.state() == QtMultimedia.QMediaPlayer.StoppedState: # and self.nextClip == None:
self.playerW.hide()
def playSchedClip(self):
# trigger by timer
# Load media from the Next CLIP and set as current media of the track.
self.curClip = self.nextClip
self.nextClip = None
# Play immediately from start. Next Clip will be prepared in play().
print(f'Track.{self.no}: Playing scheduled media {self.curClip.name}.')
self.player.setMedia(QtMultimedia.QMediaContent(QtCore.QUrl(self.curClip.mrl)))
self.playerW.setWindowTitle(f'Track {self.no}: {self.curClip.name}')
self.playerW.show()
self.player.play()
nC = self.getClipsByPos(self.curClip.sPos)[1]
if nC:
print(f'Track.{self.no}: Got next clip {nC.name}, schedule it.')
self.schNC(nC)
def pause(self):
if self.player.pause: self.player.pause()
self.curClipPausePos = self.parent().getCurPos()
# Clear the timer.
self.timer.stop()
def setMarker(self, newMarker = True):
trks = self.parent()
if newMarker:
print(trks.getCurPos())
trks.marker = trks.getCurPos()
else:
trks.marker = 0
def syncClipToMarker(self):
trks = self.parent()
clpPos = self.player.position()
oldPos = self.curClip.sPos + clpPos
shiftMS = trks.marker - oldPos
self.curClip.adjustPos(shiftMS)
print(f'Align {oldPos} of {self.curClip.name} with marker at {trks.marker}')
trks.pausePlay()
trks.resumeFrom -= 1000
trks.resumePlay()
def dragEnterEvent(self, event):
self.mainWindow.statusBar().showMessage(f"Drop file(s) to add to Track {self.no} for playing.")
event.accept()
def dropEvent(self, event):
for url in event.mimeData().urls():
filename = url.toLocalFile()
clip = Clip(self, filename, sPos=self.ePos + 1) # Added 1ms to avoid timer overlapping.
durationInMs = clip.duration
if durationInMs > 0 :
# Create a new Clip (button) at the end of track (self.ePos).
self.appendClip(clip)
# Need to update tracks first for updating total duration used in calculate the width of clips!
self.trackUpdated.emit()
# Update btns in all tracks in event dealing in Tracks, not here!
def closeTrack(self):
self.playerW.close()
class Tracks(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.mainWindow = parent
self.tracks = []
# Overall playing info
self.totalDuration = 0
self.isPlaying = False
# Marker position in ms used to sync tracks.
self.marker = 0
self.pbSpeedF = 1
#nano seconds for play position, relay on the system clock.
# Play started postion in ms
self.resumeFrom = 0
# Play started time in ns
self.resumeMomentNS = 0
self.tracksBox = QtWidgets.QVBoxLayout()
self.tracksBox.setSpacing(0)
self.positionSlider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self)
# self.ruler = QtWidgets.QLabel(self)
# self.ruler.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum))
centerBox = QtWidgets.QVBoxLayout()
centerBox.addStretch()
centerBox.addLayout(self.tracksBox)
# centerBox.addWidget(self.ruler)
centerBox.addWidget(self.positionSlider)
self.setLayout(centerBox)
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Maximum)
self.addTrack()
self.positionSlider.sliderPressed.connect(self.startSlide)
self.positionSlider.sliderReleased.connect(self.slided)
def getTracksList(self):
ts = []
for t in self.tracks:
track = {}
track['Number'] = t.no
track['Clips'] = []
for c in t.clips:
clip = {}
clip['name'] = c.name
clip['url'] = c.mrl
clip['startPosition'] = c.sPos
clip['duration'] = c.duration
clip['type'] = c.mediatype
track['Clips'].append(clip)
ts.append(track)
return ts
def loadTracks(self, tracks):
self.closeAllTracks()
for t in tracks:
self.addTrack(t)
def getCurPos(self):
# return cur position in ms.
if self.isPlaying:
return self.resumeFrom + int((time.time_ns()-self.resumeMomentNS)/1000000)
else:
return self.resumeFrom
def resumePlay(self):
# call play on all tracks here.
print(f'Resume play from {self.resumeFrom}.')
for t in self.tracks:
t.playFrom(self.resumeFrom)
self.isPlaying = True
self.resumeMomentNS = time.time_ns()
self.updateWidgets()
def pausePlay(self):
# call pause on all tracks here.
self.resumeFrom = self.getCurPos()
for t in self.tracks:
t.pause()
self.isPlaying = False
self.updateWidgets()
print(f'Paused at {self.resumeFrom}.')
def addTrack(self, t = None):
# Add tracks with a dictionary (from Yaml), or an empty track.
if (t == None or t == False): # add an empty track, when tiggered by the button, it hase the clicked = False parameter.
trackNo = len(self.tracks) + 1
track = Track(self, trackNo)
else:
trackNo = t['Number']
track = Track(self, trackNo)
track.addClips(t['Clips'])
# The costomized signal
track.trackUpdated.connect(self.updateWidgets)
self.tracks.append(track)
self.tracksBox.addWidget(track)
track.show()
self.mainWindow.statusBar().showMessage(f"Added Track {trackNo}.")
def updateWidgets(self):
# print(self.tracks[0].width())
# if self.isPlaying: self.pausePlay()
self.totalDuration = max(self.tracks, key=operator.attrgetter('ePos')).ePos
totalDurS = int(self.totalDuration/1000)
self.mainWindow.progressClock.setText(str(datetime.timedelta(seconds=self.getCurPos()/1000)) + "/" + str(datetime.timedelta(seconds=totalDurS)))
if self.totalDuration > 0:
self.positionSlider.setRange(0, totalDurS)
self.positionSlider.setSingleStep(5)
self.positionSlider.setDisabled(False)
else:
self.positionSlider.setDisabled(True)
for t in self.tracks:
t.popClipBtn()
# self.drawRuler()
# def getRightPixByDur(self, dur):
# tWidth = self.width()
# tDur = self.totalDuration
# pix = int( tWidth * ( dur / tDur ) )
# # print(f'Width of all tracks width are {tWidth} pixels for {tDur}ms, {dur}ms takes {pix} pixels.')
# return pix
# def drawRuler(self):
# rwidth = self.positionSlider.width()
# rheight = 18
# canvas = QtGui.QPixmap(rwidth, rheight)
# canvas.fill(self.palette().color(self.backgroundRole()))
# painter = QtGui.QPainter(canvas)
# painter.drawLine(0, rheight-1, rwidth, rheight-1)
# painter.drawLine(0, 0, 0, rheight)
# painter.drawLine(rwidth-1, 0, rwidth-1, rheight)
# painter.end()
# self.ruler.setPixmap(canvas)
def startSlide(self):
print("Slide started.")
self.pausePlay()
def slided(self):
self.resumeFrom = self.positionSlider.value()*1000
print(f"Slided to {self.resumeFrom}.")
self.resumePlay()
def closeAllTracks(self):
for t in self.tracks:
t.closeTrack()
t.setParent(None)
self.tracks = []
self.resumeFrom = 0
self.totalDuration = 0
self.positionSlider.setValue(0)