-
Notifications
You must be signed in to change notification settings - Fork 5
/
twitch_vlc_player.py
310 lines (242 loc) · 8.99 KB
/
twitch_vlc_player.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
#! python3
#TODO: fix length, and audio fadeout
#TODO: Look into transition at the end
import vlc # download https://git.videolan.org/?p=vlc/bindings/python.git;a=blob_plain;f=generated/vlc.py;hb=HEAD
from pytube import YouTube # pip install pytube
import sys
import tkinter as Tk
from tkinter import ttk
# import standard libraries
import os
import pathlib
from threading import Thread, Event
import time
import platform
start = time.clock()
def getYoutubeUrl(input_url):
if input_url[:32] == "https://www.youtube.com/watch?v=":
yt = YouTube(input_url) # additional sanitization needed
uid = input_url[32:]
elif input_url[:31] == "http://www.youtube.com/watch?v=":
yt = YouTube(input_url) # additional sanitization needed
uid = input_url[31:]
else:
try:
yt = YouTube("https://www.youtube.com/watch?v=%s" % input_url)
except Exception as e:
sys.exit("Error message: %s" % format(e))
videos = yt.get_videos()
for video in videos:
print(video)
yt.set_filename("intro-%s" % input_url)
video = yt.get('mp4', '360p')
return video.url
# video.download("./")
class ttkTimer(Thread):
"""a class serving same function as wxTimer... but there may be better ways to do this
"""
def __init__(self, callback, tick):
Thread.__init__(self)
self.callback = callback
self.stopFlag = Event()
self.tick = tick
self.iters = 0
def run(self):
while not self.stopFlag.wait(self.tick):
self.iters += 1
self.callback()
def stop(self):
self.stopFlag.set()
def get(self):
return self.iters
class Player(Tk.Frame):
"""The main window has to deal with events.
"""
def __init__(self, parent, title=None):
Tk.Frame.__init__(self, parent)
self.parent = parent
if title == None:
title = "Not ready"
self.parent.title(title)
# self.parent.overrideredirect(1)
w = 640 # width for the Tk root
h = 360 # height for the Tk root
# get screen width and height
ws = self.parent.winfo_screenwidth() # width of the screen
hs = self.parent.winfo_screenheight() # height of the screen
# calculate x and y coordinates for the Tk root window
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
# set the dimensions of the screen
# and where it is placed
self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))
self.parent.wm_attributes('-topmost', 1)
# The second panel holds controls
self.player = None
self.videopanel = ttk.Frame(self.parent)
self.parent.bind("<KeyPress>", self.keydown)
self.videopanel.pack(fill=Tk.BOTH, expand=1)
# VLC player controls
self.Instance = vlc.Instance()
self.player = self.Instance.media_player_new()
self.timer = ttkTimer(self.OnTimer, 0.1)
self.timer.start()
# self.parent.update()
# self.setVideo("C:\\Users\\spiffomatic64\\Google Drive\\nerd stuff\\twitch\\tests\\intro-9JoeVoyKH8M.mp4")
# self.setUrlVideo("https://www.youtube.com/watch?v=9JoeVoyKH8M")
# self.parent.attributes("-alpha", 0.5)
def initTK(self):
Tk.Frame.__init__(self, self.parent)
def keydown(self, e):
print ("Pressed: %d" % e.keycode)
if e.keycode == 27:
_quit()
if e.keycode == 32:
self.player.pause()
def OnExit(self, evt):
"""Closes the window.
"""
self.Close()
def setVideo(self, fullname):
# if a file is already running, then stop it.
self.OnStop()
# Create a file dialog opened in the current home directory, where
# you can display all kind of files, having as title "Choose a file".
p = pathlib.Path(os.path.expanduser("~"))
# fullname = askopenfilename(initialdir = p, title = "choose your file",filetypes = (("all files","*.*"),("mp4 files","*.mp4")))
if os.path.isfile(fullname):
dirname = os.path.dirname(fullname)
filename = os.path.basename(fullname)
# Creation
self.Media = self.Instance.media_new(str(os.path.join(dirname, filename)))
self.player.set_media(self.Media)
# set the window id where to render VLC's video output
if platform.system() == 'Windows':
self.player.set_hwnd(self.GetHandle())
else:
self.player.set_xwindow(self.GetHandle()) # this line messes up windows
# FIXME: this should be made cross-platform
self.OnPlay()
def setVideoUrl(self, url):
# if a file is already running, then stop it.
self.OnStop()
self.Media = self.Instance.media_new(url)
self.player.set_media(self.Media)
# set the window id where to render VLC's video output
if platform.system() == 'Windows':
self.player.set_hwnd(self.GetHandle())
else:
self.player.set_xwindow(self.GetHandle()) # this line messes up windows
# FIXME: this should be made cross-platform
def OnPlay(self):
"""Toggle the status to Play/Pause.
If no file is loaded, open the dialog window.
"""
# check if there is a file to play, otherwise open a
# Tk.FileDialog to select a file
print("Playing!")
if not self.player.get_media():
self.OnOpen()
else:
# Try to launch the media, if this fails display an error message
if self.player.play() == -1:
self.errorDialog("Unable to play.")
def GetHandle(self):
return self.videopanel.winfo_id()
# def OnPause(self, evt):
def OnPause(self):
"""Pause the player.
"""
self.player.pause()
def OnStop(self):
"""Stop the player.
"""
self.player.stop()
# reset the time slider
# self.timeslider.set(0)
def OnTimer(self):
if self.player == None:
return
# update the time on the slider
tyme = self.player.get_time()
if tyme > 5000:
self.fade_away()
elif tyme > 100:
self.SetVolume(75)
def volume_sel(self, evt):
if self.player == None:
return
volume = self.volume_var.get()
if volume > 100:
volume = 100
if self.player.audio_set_volume(volume) == -1:
self.errorDialog("Failed to set volume")
def SetVolume(self, volume):
# vlc.MediaPlayer.audio_set_volume returns 0 if success, -1 otherwise
if volume > 100:
volume = 100
if self.player.audio_set_volume(volume) == -1:
print("Failed to set volume")
def errorDialog(self, errormessage):
print('Error: %s', errormessage)
# need to add a color/chroma based wipe here
def fade_away_old(self):
alpha = self.parent.attributes("-alpha")
if alpha > 0:
alpha -= 0.01
self.parent.attributes("-alpha", alpha)
self.SetVolume(int(alpha * 50))
self.after(100, self.fade_away)
else:
_quit()
def fade_away(self):
cur_vol = self.player.audio_get_volume()
print(cur_vol)
if cur_vol > 0:
self.player.audio_set_volume(cur_vol - 5)
else:
_quit()
def drawthings(self):
self.canvas.create_oval(10, 10, 80, 80, outline="gray", fill="gray", width=2)
def Tk_get_root():
if not hasattr(Tk_get_root, "root"): # (1)
Tk_get_root.root = Tk.Tk() # initialization call is inside the function
return Tk_get_root.root
def _quit():
print("_quit: bye")
root = Tk_get_root()
root.quit() # stops mainloop
# root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
os._exit(1)
def elapsed(message):
end = time.clock()
print("%f - %s" % (end - start, message))
if __name__ == "__main__":
if(len(sys.argv)!=2):
sys.exit('Usage: %s youtube url' % sys.argv[0])
elapsed("Getting youtube: %s" % sys.argv[1])
#ytUrl = getYoutubeUrl("9JoeVoyKH8M")
#TODO sanitize argv[1]
print(sys.argv[1])
ytUrl = getYoutubeUrl(sys.argv[1])
# Create a Tk.App(), which handles the windowing system event loop
elapsed("Creating TK")
root = Tk_get_root()
root.protocol("WM_DELETE_WINDOW", _quit)
elapsed("Creating player (this is what shows the box)")
player = Player(root, title="tkinter vlc")
elapsed("Setting URL")
player.setVideoUrl(ytUrl)
elapsed("Starting to play")
player.OnPlay()
while not player.player.is_playing():
time.sleep(0.001)
# print(player.player.get_state())
# player.player.pause()
elapsed("VLC says its playing")
time.sleep(0.1)
# player.drawthings()
# player.player.play()
# show the player window centred and run the application
root.mainloop()