-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtk_vlcplayer.py
261 lines (207 loc) · 8.07 KB
/
gtk_vlcplayer.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
import gtk
gtk.gdk.threads_init()
import sys
import gobject
import os
class Windowed(gtk.DrawingArea):
def __init__(self):
super(Windowed, self).__init__()
self.w = gtk.Window()
self.w.set_decorated(False)
self.w.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('black'))
self.w.show()
def sync_move(*args, **kwargs):
x,y = self.get_toplevel().window.get_position()
o_x, o_y = gtk_translate_coordinates(self, self.get_toplevel(), 0, 0)
self.w.window.move(x + o_x, y + 28)
def sync_state(w, event, *args):
print 'sync_state:', event.new_window_state
if event.new_window_state == gtk.gdk.WINDOW_STATE_ICONIFIED:
self.w.hide()
else:
self.w.show()
def handle_embed(*args, **kwargs):
self.w.set_transient_for(self.get_toplevel())
self.get_toplevel().connect('configure-event',
sync_move)
self.get_toplevel().connect('window-state-event',
sync_state)
return True
self.connect("map", handle_embed)
def do_size_allocate(self, allocation):
#print 'do_size:', allocation
self.w.resize(allocation.width, allocation.height)
#if self.flags() & gtk.REALIZED:
#super(Windowed, self).do_size_allocate(self, allocation)
def gtk_translate_coordinates(widget, relative_widget, x, y):
""" replacement for:
self.translate_coordinates(self.get_toplevel(), -1, -1)
"""
parent = widget
while parent != relative_widget:
x += parent.get_allocation()[0]
y += parent.get_allocation()[1]
parent = parent.get_parent()
return (x, y)
class WindowsVLCWidget(Windowed):
__gsignals__ = {
'play-started': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'play-paused': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'play-ended': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'play-stopped': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
}
def __init__(self):
super(WindowsVLCWidget, self).__init__()
self._player = None
self._audio_visual = None
self.set_size_request(320, 200)
gobject.timeout_add(1000, self._play_status)
def play(self, file_path):
volume = 50
mute = False
if self._player:
self._player.stop()
volume = self._player.audio_get_volume()
mute = self._player.audio_get_mute()
# create new instance cause i don't know how to reset audio filters
self._player = self.vlc_instance.media_player_new()
self._player.set_hwnd(self.w.window.handle)
self._player.audio_set_volume(volume)
self._player.audio_set_mute(mute)
if self._audio_visual and self.is_audio_only(file_path):
print 'audio_visual:', self._audio_visual
self._player.set_media(self.vlc_instance.media_new(
file_path,
'sub-filter=marq', #'vout=caca'
'audio-visual=' + self._audio_visual
))
else:
self._player.set_media(self.vlc_instance.media_new(
file_path,
'sub-filter=marq', #'vout=caca'
))
self._player.play()
_, file_name = os.path.split(file_path)
self.set_text(file_name)
self.emit('play-started')
def set_text(self, text, **kwargs):
self._player.video_set_marquee_string(vlc.VideoMarqueeOption.Text, text)
self._player.video_set_marquee_int(vlc.VideoMarqueeOption.Timeout,
kwargs.get('timeout', 3000))
self._player.video_set_marquee_int(vlc.VideoMarqueeOption.Position,
kwargs.get('position', 8))
def toggle_pause(self, *args):
if self._player.get_state() == vlc.State.Paused:
self._player.play()
self.set_text('PLAY', position=5, timeout=2000)
self.emit('play-started')
else:
self._player.pause()
self.set_text('PAUSED', position=5, timeout=0)
self.emit('play-paused')
def stop(self, *args):
self._player.stop()
self.emit('play-stopped')
def jump_relative(self, relative, *args):
print 'jump_relative:', relative
new_time = self.get_time() + relative
if new_time <= 0:
new_time = 0
elif new_time >= self.get_length():
new_time = self.get_length()
self._player.set_time(new_time*1000)
self.set_text('%s / %s' % (format_duration(new_time),
format_duration(self.get_length())))
print ('new_play_status: pos=%s/%s, st=%s'
% (self.get_time(), self.get_length(), self._player.get_state()))
def set_volume(self, relative, *args):
volume = self._player.audio_get_volume()
volume += relative
if volume < 0:
volume = 0
elif volume > 100:
volume = 100
self._player.audio_set_volume(volume)
self.set_text('VOLUME: %s' % volume, position=5, timeout=2000)
def toggle_mute(self, *args):
self._player.audio_toggle_mute()
if self._player.audio_get_mute():
self.set_text('MUTE', position=5, timeout=0)
else:
volume = self._player.audio_get_volume()
self.set_text('VOLUME: %s' % volume, position=5, timeout=2000)
def set_audio_visualization(self, visualization):
if visualization == 'none':
visualization = None
self._audio_visual = visualization
print 'set_audio_visual:', self._audio_visual
def is_audio_only(self, file_path):
_, ext = os.path.splitext(file_path)
r = ext.lower() == '.mp3'
print 'is audio:', r
return r
def get_length(self):
""" total play duration, in seconds """
return int(self._player.get_length()/1000)
def get_time(self):
""" current play pos in seconds """
return int(self._player.get_time()/1000)
def _play_status(self, *args):
state = self._player.get_state() if self._player else None
if state == vlc.State.Ended:
self.emit('play-ended')
return True # enble repetation
def fullscreen(self, *args):
self.w.fullscreen()
def unfullscreen(self, *args):
self.w.unfullscreen()
class FakeWidget(Windowed):
__gsignals__ = {
'play-started': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'play-paused': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'play-ended': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
'play-stopped': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, ()),
}
def __init__(self):
super(FakeWidget, self).__init__()
self.label = gtk.Label()
self.label.set_size_request(320, 200)
self.w.add(self.label)
self.label.show()
self.set_size_request(320, 200)
def play(self, file_path):
self.label.set_text(file_path)
def toggle_pause(self, *args):
pass
def stop(self, *args):
pass
def jump_relative(self, relative, *args):
print 'jump_relative:', relative
pass
def set_volume(self, relative, *args):
pass
def toggle_mute(self, *args):
pass
def fullscreen(self, *args):
self.w.fullscreen()
def unfullscreen(self, *args):
self.w.unfullscreen()
def format_duration(seconds):
hours = int(seconds/ 3600)
seconds = seconds - hours * 3600
minutes = int(seconds/60)
seconds = seconds - minutes*60
s = ''
if hours:
s += '%sh ' % hours
if minutes:
s += '%smin ' % minutes
s += '%ssec' % seconds
return s
if sys.platform == 'win32':
VLCWidget = WindowsVLCWidget
import vlc
WindowsVLCWidget.vlc_instance = vlc.Instance()
else:
VLCWidget = FakeWidget
gobject.type_register(VLCWidget)