-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFREAKING_VLSYNC.py
More file actions
executable file
·289 lines (260 loc) · 10.5 KB
/
FREAKING_VLSYNC.py
File metadata and controls
executable file
·289 lines (260 loc) · 10.5 KB
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
#!/usr/bin/python3
import requests, base64, vlc, sys, time, os.path
from PyQt5 import QtGui, QtCore
from PyQt5 import QtWidgets
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QApplication
class RoomChooser(QtWidgets.QMainWindow):
rooms = []
def __init__(self, clientID, master=None):
QtWidgets.QMainWindow.__init__(self, master)
self.setWindowTitle('VLSync - Choose a room')
self.createUI()
self.timer = QtCore.QTimer(self)
self.timer.setInterval(2500)
self.timer.timeout.connect(self.updateList)
self.timer.start()
self.clientID = clientID
self.updateList()
def updateList(self):
a = requests.get('https://www.nqind.com/vlsync/rooms.php')
self.rooms = a.json()['rooms']
self.roomList.clear()
for room in self.rooms:
self.roomList.addItem(room['name'])
def roomSelected(self, room):
print(type(room))
roomID = False
for i in self.rooms:
if room.text() == i['name']:
roomID = int(i['id'])
if not roomID:
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Icon.Critical, 'Internal error', 'Invalid room').exec()
return
username = self.usernameBox.text()
if not username:
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Icon.Critical, 'Bruh moment', 'Enter a username, dangit').exec()
return
print(f'Joining room ID {i["id"]}')
a = requests.post('https://www.nqind.com/vlsync/join.php', json={'client': self.clientID, 'id': roomID, 'username': username})
if a.json()['response'] == 200:
self.close()
player = Player(self.clientID, {'id': roomID, 'users': a.json()['users']})
player.show()
player.resize(640, 480)
else:
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Icon.Critical, 'Server error', a.json()['err']).exec()
def createRoom(self):
username = self.usernameBox.text()
if not username:
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Icon.Critical, 'Bruh moment', 'Enter a username, dangit').exec()
return
a = requests.post('https://www.nqind.com/vlsync/createRoom.php', json={'client': self.clientID, 'username': username})
print(a.text)
self.roomID = a.json()['id']
print(f'Joining CREATED room ID {i["id"]}')
self.close()
player = Player(self.clientID, {'id': roomID, 'users': a.json()['users']})
player.show()
player.resize(640, 480)
def createUI(self):
self.widget = QtWidgets.QWidget(self)
self.setCentralWidget(self.widget)
self.hthing = QtWidgets.QVBoxLayout()
self.roomList = QtWidgets.QListWidget()
self.hthing.addWidget(self.roomList)
self.usernameBox = QtWidgets.QLineEdit()
self.usernameBox.setPlaceholderText('Username...')
self.hthing.addWidget(self.usernameBox)
self.createRoomButton = QtWidgets.QPushButton('Create Room')
self.hthing.addWidget(self.createRoomButton)
self.createRoomButton.clicked.connect(self.createRoom)
self.widget.setLayout(self.hthing)
self.roomList.itemDoubleClicked.connect(self.roomSelected)
class Player(QtWidgets.QMainWindow):
endSync = pyqtSignal()
def __init__(self, clientID, roomInfo, master=None):
QtWidgets.QMainWindow.__init__(self, master)
self.setWindowTitle('VLSync thing please help')
self.instance = vlc.Instance()
self.mediaplayer = self.instance.media_player_new()
self.createUI()
self.isPaused = False
self.starttime = 0
self.room = roomInfo
self.roomID = roomInfo['id']
self.clientID = clientID
self.threadStart()
event_manager = self.mediaplayer.event_manager()
event_manager.event_attach(vlc.EventType.MediaPlayerPlaying, self.log_delay)
def log_delay(self, _):
print(time.monotonic() - self.starttime)
def sync(self, force_sync=False):
b = requests.post('https://www.nqind.com/vlsync/heartbeat.php', json={'client': self.clientID, 'id': self.room['id']}).json()
a = b['room_state']
print(f'[SYNC] Response: {b}')
self.userslabel.setText(f'Users: {"".join([i + ", " for i in b["users"]])}')
# adjust for current timecode if not paused
timecode = a['timecode'] + (0 if a['paused'] else a['offset'])
time_diff = self.mediaplayer.get_time() - (timecode * 1000)
print(f'[SYNC] Diff: {str(time_diff)}ms')
if force_sync:
self.mediaplayer.play()
if abs(time_diff) > 2500 or not self.isPaused == a['paused'] or force_sync:
self.timeshift(timecode, a['paused'])
def timeshift(self, timecode, paused):
targetTime = int(timecode * 1000)
print(f'[SYNC] Shifting to {targetTime}')
self.mediaplayer.set_time(targetTime)
if not self.isPaused == paused:
self.PlayPause()
print('[SYNC] Toggled pause')
def keyPressEvent(self, ev):
if ev.key() == 32:
self.PlayPause()
self.updateUI()
elif ev.key() == 70:
print('fullscreen')
#self.mediaplayer.set_fullscreen(0 if self.mediaplayer.get_fullscreen == 1 else 1)
self.widget.showFullScreen()
def threadStart(self):
# Remember when we were multithreaded?
# Yeah, me neither
self.syncTimer = QtCore.QTimer(self)
self.syncTimer.setInterval(2500)
self.syncTimer.timeout.connect(self.sync)
self.syncTimer.start()
def closeAll(self):
self.syncTimer.stop()
self.close()
def createUI(self):
self.widget = QtWidgets.QWidget(self)
self.setCentralWidget(self.widget)
# In this widget, the video will be drawn
self.videoframe = QtWidgets.QFrame()
self.palette = self.videoframe.palette()
self.palette.setColor(QtGui.QPalette.Window, QtGui.QColor(0, 0, 0))
self.videoframe.setPalette(self.palette)
self.videoframe.setAutoFillBackground(True)
self.positionslider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self)
self.positionslider.setToolTip('Position')
self.positionslider.setMaximum(1000)
self.positionslider.setTracking(False)
self.positionslider.sliderMoved.connect(self.markPosition)
self.positionslider.sliderReleased.connect(self.setPosition)
self.hbuttonbox = QtWidgets.QHBoxLayout()
self.forcesync = QtWidgets.QPushButton('Sync')
self.hbuttonbox.addWidget(self.forcesync)
self.forcesync.clicked.connect(lambda: self.sync(True))
self.playbutton = QtWidgets.QPushButton('Play')
self.hbuttonbox.addWidget(self.playbutton)
self.playbutton.clicked.connect(self.PlayPause_andupdateserver)
self.stopbutton = QtWidgets.QPushButton('Stop')
self.hbuttonbox.addWidget(self.stopbutton)
self.stopbutton.clicked.connect(self.Stop)
self.loadbutton = QtWidgets.QPushButton('Load')
def openfile():
print('Opening file.')
self.OpenFile()
self.loadbutton.clicked.connect(openfile)
self.hbuttonbox.addWidget(self.loadbutton)
self.userslabel = QtWidgets.QLabel('No users')
self.hbuttonbox.addWidget(self.userslabel)
self.exitbutton = QtWidgets.QPushButton('Exit')
self.exitbutton.clicked.connect(self.closeAll)
self.hbuttonbox.addWidget(self.exitbutton)
self.hbuttonbox.addStretch(1)
self.volumeslider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self)
self.volumeslider.setMaximum(100)
self.volumeslider.setValue(self.mediaplayer.audio_get_volume() or 100)
self.volumeslider.setToolTip('Volume')
self.hbuttonbox.addWidget(self.volumeslider)
self.volumeslider.valueChanged.connect(self.setVolume)
self.setVolume(self.volumeslider.value())
self.vboxlayout = QtWidgets.QVBoxLayout()
self.vboxlayout.addWidget(self.videoframe)
self.vboxlayout.addWidget(self.positionslider)
self.vboxlayout.addLayout(self.hbuttonbox)
self.widget.setLayout(self.vboxlayout)
self.timer = QtCore.QTimer(self)
self.timer.setInterval(200)
self.timer.timeout.connect(self.updateUI)
def PlayPause_andupdateserver(self):
print('[SYNC] Toggling Pause to server!')
requests.post('https://www.nqind.com/vlsync/manageRoom.php', json={'client': self.clientID, 'id': self.roomID, 'action': 'time', 'args': {'timecode': self.mediaplayer.get_time() / 1000, 'paused': not self.isPaused}})
self.PlayPause()
def PlayPause(self):
if self.mediaplayer.is_playing():
self.mediaplayer.pause()
self.playbutton.setText('Play')
self.isPaused = True
else:
if self.mediaplayer.play() == -1:
self.OpenFile()
time.sleep(0.5)
self.playbutton.setText('Pause')
self.timer.start()
self.isPaused = False
self.starttime = time.monotonic()
def Stop(self):
self.mediaplayer.stop()
self.playbutton.setText('Play')
def OpenFile(self, filename=None):
if filename is None:
filename = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', os.path.expanduser('~'), 'Video files (*.mkv *.mp4);;Images (*.png *.xpm *.jpg)')
if filename is not None:
filename = filename[0]
if not filename:
return
self.media = self.instance.media_new(filename)
self.mediaplayer.set_media(self.media)
self.media.parse()
self.setWindowTitle(self.media.get_meta(0))
# Why the frick is this platform-specific
if sys.platform.startswith('linux'): # X only, fools
self.mediaplayer.set_xwindow(self.videoframe.winId())
elif sys.platform == 'win32':
self.mediaplayer.set_hwnd(self.videoframe.winId())
elif sys.platform == 'darwin':
# If this is actually mecb00k compatible, I will shame them all
[print('mecb00k user', file=sys.stderr) for i in range(1, 500)]
self.mediaplayer.set_nsobject(self.videoframe.winId())
def setVolume(self, Volume):
self.mediaplayer.audio_set_volume(Volume)
def markPosition(self, position):
self.position = position
def setPosition(self):
self.mediaplayer.set_position(self.position / 1000.0)
newTimecode = int(self.mediaplayer.get_time() / 1000)
print(f'[SYNC] NEW TIMECODE: {newTimecode}')
r = requests.post('https://www.nqind.com/vlsync/manageRoom.php', json={'client': self.clientID, 'id': self.roomID, 'action': 'time', 'args': {'timecode': newTimecode, 'paused': self.isPaused}})
if r.status_code == 200:
if not r.json()['response'] == 200:
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Icon.Critical, f'Server error {str(r.json()["response"])}', r.json()['err']).exec()
else:
QtWidgets.QMessageBox(QtWidgets.QMessageBox.Icon.Critical, 'Server error', f'Server returned {str(r.json()["response"])}, is it up?').exec()
def updateUI(self):
if not self.positionslider.isSliderDown():
self.positionslider.setValue(int(self.mediaplayer.get_position() * 1000))
if not self.mediaplayer.is_playing():
self.timer.stop()
if not self.isPaused:
# after the video finished, the play button stills shows
# "Pause", not the desired behavior of a media player
# this will fix it
self.Stop()
if __name__ == "__main__":
clientID = base64.b64encode(os.urandom(20)).decode('ascii')
app = QApplication(sys.argv)
roomChooser = RoomChooser(clientID)
roomChooser.show()
roomChooser.resize(1000, 1000)
# TODO open a file with the script?
# Wait, this is an abandoned project
#player = Player(clientID, {})
#player.show()
#player.resize(640, 480)
#if sys.argv[1:]:
# print(sys.argv[1])
# player.OpenFile(sys.argv[1])
sys.exit(app.exec_())