-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwxvlc.py
351 lines (301 loc) · 13 KB
/
wxvlc.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
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# WX example for VLC Python bindings
# Copyright (C) 2009-2010 the VideoLAN team
#
# Functionality extended by Dominik Jain
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
#
# import external libraries
import wx # 2.8
import vlc
# import standard libraries
import os
import user
import time
class Player(wx.Frame):
"""The main window has to deal with events.
"""
def __init__(self, title, version):
wx.Frame.__init__(self, None, -1, title, pos=wx.DefaultPosition, size=(550, 500))
self.title = title
self.version = version
# Menu Bar
self.frame_menubar = wx.MenuBar()
self.SetMenuBar(self.frame_menubar)
# - file Menu
self.file_menu = wx.Menu()
self.file_menu.Append(1, "&Open", "Open from file..")
self.file_menu.AppendSeparator()
self.file_menu.Append(2, "&Close", "Quit")
self.Bind(wx.EVT_MENU, self.OnOpen, id=1)
self.Bind(wx.EVT_MENU, self.OnExit, id=2)
self.frame_menubar.Append(self.file_menu, "File")
# - transport menu
self.transport_menu = wx.Menu()
playMI = self.transport_menu.Append(wx.ID_ANY, "Play/Pause\tSpace")
self.Bind(wx.EVT_MENU, self.OnPlayPause, id=playMI.GetId())
self.frame_menubar.Append(self.transport_menu, "Transport")
# - audio Menu
self.audio_menu = wx.Menu()
self.audio_menu_items = {}
self.frame_menubar.Append(self.audio_menu, "Audio")
# - subtitles Menu
self.subs_menu = wx.Menu()
self.subs_menu_items = {}
self.frame_menubar.Append(self.subs_menu, "Subtitles")
# - video menu
self.view_menu = wx.Menu()
fullScreenMI = self.view_menu.Append(wx.ID_ANY,"Full Screen\tF12", "Toggles Full Screen Mode")
self.Bind(wx.EVT_MENU, self.OnToggleFullScreen, id=fullScreenMI.GetId())
self.frame_menubar.Append(self.view_menu, "View")
# - help menu
self.help_menu = wx.Menu()
aboutMI = self.help_menu.Append(wx.ID_ANY, "About")
self.Bind(wx.EVT_MENU, self.OnAbout, id=aboutMI.GetId())
self.frame_menubar.Append(self.help_menu, "Help")
# Panels
# The first panel holds the video and it's all black
self.videopanel = wx.Panel(self, -1)
self.videopanel.SetBackgroundColour(wx.BLACK)
# The second panel holds controls
ctrlpanel = wx.Panel(self, -1)
self.ctrlpanel = ctrlpanel
self.timeslider = wx.Slider(ctrlpanel, -1, 0, 0, 1000)
self.timeslider.SetRange(0, 1000)
pause = wx.Button(ctrlpanel, label="Pause")
play = wx.Button(ctrlpanel, label="Play")
stop = wx.Button(ctrlpanel, label="Stop")
volume = wx.Button(ctrlpanel, label="Mute")
self.volslider = wx.Slider(ctrlpanel, -1, 0, 0, 100, size=(100, -1))
self.timeDisplay = wx.StaticText(ctrlpanel, label=" 0:00:00", style=wx.ALIGN_RIGHT)
# Bind controls to events
self.Bind(wx.EVT_BUTTON, self.OnPlay, play)
self.Bind(wx.EVT_BUTTON, self.OnPause, pause)
self.Bind(wx.EVT_BUTTON, self.OnStop, stop)
self.Bind(wx.EVT_BUTTON, self.OnToggleVolume, volume)
self.Bind(wx.EVT_SLIDER, self.OnSetVolume, self.volslider)
self.Bind(wx.EVT_SLIDER, self.OnMoveTimeSlider, self.timeslider)
# Give a pretty layout to the controls
ctrlbox = wx.BoxSizer(wx.VERTICAL)
box1 = wx.BoxSizer(wx.HORIZONTAL)
box2 = wx.BoxSizer(wx.HORIZONTAL)
# box1 contains the timeslider
box1.Add(self.timeDisplay, flag=wx.CENTER)
box1.Add(self.timeslider, 1)
# box2 contains some buttons and the volume controls
box2.Add(play, flag=wx.RIGHT, border=0)
box2.Add(pause)
box2.Add(stop)
box2.Add((-1, -1), 1)
box2.Add(volume)
box2.Add(self.volslider, flag=wx.TOP | wx.LEFT, border=5)
# Merge box1 and box2 to the ctrlsizer
ctrlbox.Add(box1, flag=wx.EXPAND | wx.BOTTOM, border=10)
ctrlbox.Add(box2, 1, wx.EXPAND)
ctrlpanel.SetSizer(ctrlbox)
# Put everything together
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.videopanel, 1, flag=wx.EXPAND)
sizer.Add(ctrlpanel, flag=wx.EXPAND | wx.BOTTOM | wx.TOP, border=0)
self.SetSizer(sizer)
self.SetMinSize((350, 300))
self.fullscreen = False
# finally create the timer, which updates the timeslider
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
# VLC player controls
self.Instance = vlc.Instance()
self.player = self.Instance.media_player_new()
def play(self):
if self.player.play() != -1:
wx.CallAfter(self.timer.Start, 75)
def pause(self):
self.player.set_pause(1)
def seek(self, time):
self.player.set_time(time)
def updateTimeSlider(self):
# update length
length = self.player.get_length()
self.timeslider.SetRange(0, length)
# update the time on the slider
currentTime = self.getTime()
self.timeslider.SetValue(currentTime)
# update time display
secs = currentTime / 1000
minutes = secs / 60
hours = minutes / 60
secs = secs % 60
minutes = minutes % 60
display = " %d:%02d:%02d" % (hours, minutes, secs)
self.timeDisplay.SetLabel(display)
def getTime(self):
return self.player.get_time()
def getMedia(self):
return self.player.get_media()
def isPlaying(self):
return self.player.is_playing()
def isPaused(self):
return self.player.get_state() == vlc.State.Paused
def open(self, path):
self.Media = self.Instance.media_new(unicode(path))
self.player.set_media(self.Media)
# Report the title of the file chosen
title = self.player.get_title()
# if an error was encountred while retriving the title, then use
# filename
filename = os.path.basename(path)
if title == -1:
title = filename
self.SetTitle("%s - %s" % (title, self.title))
# set the window id where to render VLC's video output
self.player.set_xwindow(self.videopanel.GetHandle()) # for Linux/Unix
self.player.set_hwnd(self.videopanel.GetHandle()) # for Windows
self.play()
# wait until the video is really playing, so audio track query will work
while not self.player.is_playing():
time.sleep(0.01)
# update audio menu
for audio_track in self.audio_menu_items.values():
self.audio_menu.RemoveItem(audio_track["menu_item"])
self.audio_menu_items = {}
tracks = self.player.audio_get_track_description()
print "audio tracks: %s" % str(tracks)
if len(tracks) == 0: tracks = [(-1, "None"), (1, "1"), (2, "2")] # fallback (should no longer be required)
for (track_no, name) in tracks:
menu_item = self.audio_menu.Append(wx.ID_ANY, name)
id = menu_item.GetId()
self.audio_menu_items[id] = {"menu_item": menu_item, "track_no": track_no}
self.Bind(wx.EVT_MENU, self.OnSelectAudioTrack, id=id)
# update subs menu
for subs_track in self.subs_menu_items.values():
self.subs_menu.RemoveItem(audio_track["menu_item"])
self.subs_menu_items = {}
tracks = self.player.video_get_spu_description()
print "subs tracks: %s" % str(tracks)
for i, (track_no, name) in enumerate(tracks):
menu_item = self.subs_menu.Append(wx.ID_ANY, name)
id = menu_item.GetId()
self.subs_menu_items[id] = {"menu_item": menu_item, "track_no": i} # Weird! Looks like one has to use the index instead of track_no.
self.Bind(wx.EVT_MENU, self.OnSelectSubsTrack, id=id)
# set the volume slider to the current volume
self.volslider.SetValue(self.player.audio_get_volume() / 2)
def OnExit(self, evt):
"""Closes the window.
"""
self.Close()
def OnOpen(self, evt):
"""Pop up a new dialow window to choose a file, then play the selected file.
"""
# if a file is already running, then stop it.
#self.OnStop(None)
# Create a file dialog opened in the current home directory, where
# you can display all kind of files, having as title "Choose a file".
dlg = wx.FileDialog(self, "Choose a file", user.home, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
dirname = dlg.GetDirectory()
filename = dlg.GetFilename()
self.open(os.path.join(dirname, filename))
# finally destroy the dialog
dlg.Destroy()
def OnSelectAudioTrack(self, evt):
track = self.audio_menu_items[evt.GetId()]["track_no"]
print "switching to audio track %d" % track
self.player.audio_set_track(track)
def OnSelectSubsTrack(self, evt):
track = self.subs_menu_items[evt.GetId()]["track_no"]
print "switching to subs track %d" % track
if self.player.video_set_spu(track) == -1:
print "failed to set subs track"
def OnPlay(self, evt):
"""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
# wx.FileDialog to select a file
if self.getMedia() is None:
self.OnOpen(None)
else:
self.play()
def OnPause(self, evt):
"""Pause the player.
"""
self.pause()
def OnStop(self, evt):
"""Stop the player.
"""
self.player.stop()
# reset the time slider
self.timeslider.SetValue(0)
wx.CallAfter(self.timer.Stop)
def OnTimer(self, evt):
"""Update the time slider according to the current movie time.
"""
# since the self.player.get_length can change while playing,
# re-set the timeslider to the correct range.
self.updateTimeSlider()
def OnToggleVolume(self, evt):
"""Mute/Unmute according to the audio button.
"""
is_mute = self.player.audio_get_mute()
self.player.audio_set_mute(not is_mute)
# update the volume slider;
# since vlc volume range is in [0, 200],
# and our volume slider has range [0, 100], just divide by 2.
self.volslider.SetValue(self.player.audio_get_volume() / 2)
def OnSetVolume(self, evt):
"""Set the volume according to the volume sider.
"""
volume = self.volslider.GetValue() * 2
# vlc.MediaPlayer.audio_set_volume returns 0 if success, -1 otherwise
if self.player.audio_set_volume(volume) == -1:
self.errorDialog("Failed to set volume")
def OnMoveTimeSlider(self, evt):
time = self.timeslider.GetValue()
self.OnSeek(time)
def OnSeek(self, time):
self.seek(time)
def OnPlayPause(self, evt):
if self.isPaused():
self.OnPlay(None)
else:
self.OnPause(None)
def OnToggleFullScreen(self, evt):
self.fullscreen = not self.fullscreen
self.ctrlpanel.Show(not self.fullscreen)
self.ShowFullScreen(self.fullscreen)
def OnAbout(self, evt):
dialog = wx.MessageDialog(self, self.title + " " + self.version + "\n\nby Dominik Jain", 'About', wx.OK | wx.ICON_INFORMATION)
dialog.ShowModal()
def errorDialog(self, errormessage):
"""Display a simple error dialog.
"""
edialog = wx.MessageDialog(self, errormessage, 'Error', wx.OK | wx.ICON_ERROR)
edialog.ShowModal()
def questionDialog(self, message, title = "Error"):
"""Displays a yes/no dialog, returning true if the user clicked yes, false otherwise
"""
return wx.MessageDialog(self, message, title, wx.YES_NO | wx.ICON_QUESTION).ShowModal() == wx.ID_YES
if __name__ == "__main__":
# Create a wx.App(), which handles the windowing system event loop
app = wx.App()
# Create the window containing our small media player
player = Player("wxVLC", "v1.0")
# show the player window centred and run the application
player.Centre()
player.Show()
app.MainLoop()