-
Notifications
You must be signed in to change notification settings - Fork 1
/
t2demo.py
294 lines (239 loc) · 10.4 KB
/
t2demo.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
import sys, os, thread, time
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject, Gtk, Gdk
# Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively:
from gi.repository import GdkX11, GstVideo
class GTK_Main(object):
def __init__(self):
window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
window.set_title("Videocorum")
window.set_default_size(500, 400)
window.connect("destroy", Gtk.main_quit, "WM destroy")
vbox = Gtk.VBox(False, 0)
window.add(vbox)
menu_bar = Gtk.MenuBar()
hbox = Gtk.HBox()
hbox.add(menu_bar)
vbox.pack_start(hbox, False, False, 0)
menu_bar.show()
menu_bar.append(self.file_submenu())
menu_bar.append(self.generate_dummy_list_items("Subtitles"))
menu_bar.append(self.generate_dummy_list_items("Settings"))
menu_bar.append(self.generate_dummy_list_items("Help"))
hbox = Gtk.HBox()
vbox.pack_start(hbox, False, False, 0)
self.movie_window = Gtk.DrawingArea()
vbox.add(self.movie_window)
hbox = Gtk.HBox()
button_start = Gtk.Button(stock=Gtk.STOCK_MEDIA_PAUSE)
hbox.add(button_start)
button_start.connect("clicked", self.action_pause)
button_start.show()
button = Gtk.Button(stock=Gtk.STOCK_MEDIA_STOP)
hbox.add(button)
button.connect("clicked", self.action_stop, button_start)
button.show()
vbox.pack_start(hbox, False, False, 0)
buttonbox = Gtk.HButtonBox()
hbox.pack_start(buttonbox, False, False, 0)
rewind_button = Gtk.Button("Rewind")
rewind_button.connect("clicked", self.rewind_callback)
buttonbox.add(rewind_button)
forward_button = Gtk.Button("Forward")
forward_button.connect("clicked", self.forward_callback)
buttonbox.add(forward_button)
fast_button = Gtk.Button("Fast")
fast_button.connect("clicked", self.fast_callback)
buttonbox.add(fast_button)
slow_button = Gtk.Button("Slow")
slow_button.connect("clicked", self.slow_callback)
buttonbox.add(slow_button)
self.pbRate = 1
self.time_label = Gtk.Label()
self.time_label.set_text("00:00 / 00:00")
hbox.add(self.time_label)
window.show_all()
self.player = Gst.Pipeline.new("player")
source = Gst.ElementFactory.make("filesrc", "file-source")
demuxer = Gst.ElementFactory.make("oggdemux", "demuxer")
demuxer.connect("pad-added", self.demuxer_callback)
self.audio_decoder = Gst.ElementFactory.make("vorbisdec", "vorbis-decoder")
audioconv = Gst.ElementFactory.make("audioconvert", "converter")
audiosink = Gst.ElementFactory.make("autoaudiosink", "audio-output")
for ele in [source, demuxer, self.audio_decoder, audioconv, audiosink]:
self.player.add(ele)
source.link(demuxer)
self.audio_decoder.link(audioconv)
audioconv.link(audiosink)
bus = self.player.get_bus()
bus.add_signal_watch()
bus.connect("message", self.on_message)
window.show_all()
self.player = Gst.ElementFactory.make("playbin", "player")
bus = self.player.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("message", self.on_message)
bus.connect("sync-message::element", self.on_sync_message)
def action_stop(self, widget, button_start):
self.player.set_state(Gst.State.NULL)
self.player.set_state(Gst.State.PLAYING)
self.player.set_state(Gst.State.PAUSED)
button_start.set_label(Gtk.STOCK_MEDIA_PLAY)
def play_thread(self):
play_thread_id = self.play_thread_id
Gdk.threads_enter()
self.time_label.set_text("00:00 / 00:00")
Gdk.threads_leave()
while play_thread_id == self.play_thread_id:
try:
time.sleep(0.2)
dur_int = self.player.query_duration(Gst.Format.TIME, None)[0]
if dur_int == -1:
continue
dur_str = self.convert_ns(dur_int)
Gdk.threads_enter()
self.time_label.set_text("00:00 / " + dur_str)
Gdk.threads_leave()
break
except:
pass
time.sleep(0.2)
while play_thread_id == self.play_thread_id:
pos_int = self.player.query_position(Gst.Format.TIME, None)[0]
pos_str = self.convert_ns(pos_int)
if play_thread_id == self.play_thread_id:
Gdk.threads_enter()
self.time_label.set_text(pos_str + " / " + dur_str)
Gdk.threads_leave()
time.sleep(1)
def action_pause(self, widget):
if ("pause" in widget.get_label()):
self.player.set_state(Gst.State.PAUSED)
widget.set_label(Gtk.STOCK_MEDIA_PLAY)
else:
self.player.set_state(Gst.State.PLAYING)
widget.set_label(Gtk.STOCK_MEDIA_PAUSE)
def open_file(self, widget, string):
dialog = Gtk.FileChooserDialog("Open", None,
Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
dialog.set_default_response(Gtk.ResponseType.OK)
fil = Gtk.FileFilter()
fil.set_name("mp4")
fil.add_pattern("*.mp4")
dialog.add_filter(fil)
response = dialog.run()
if response == Gtk.ResponseType.OK:
print(dialog.get_filename())
self.player.set_state(Gst.State.NULL)
self.player.set_property("uri", "file://" + dialog.get_filename())
self.player.set_state(Gst.State.PLAYING)
dialog.destroy()
def menuitem_response(self, widget, string):
print "%s" % string
def file_submenu(self):
menu = Gtk.Menu()
menu_items = Gtk.MenuItem("Open..")
menu.append(menu_items)
menu_items.connect("activate", self.open_file, "Open..")
menu_items.show()
root_menu = Gtk.MenuItem("File")
root_menu.show()
root_menu.set_submenu(menu)
return root_menu
def generate_dummy_list_items(self, name):
menu = Gtk.Menu()
for i in range(3):
# Copy the names to the buf.
buf = "Test-undermenu - %d" % i
# Create a new menu-item with a name...
menu_items = Gtk.MenuItem(buf)
# ...and add it to the menu.
menu.append(menu_items)
# Do something interesting when the menuitem is selected
menu_items.connect("activate", self.menuitem_response, buf)
# Show the widget
menu_items.show()
root_menu = Gtk.MenuItem(name)
root_menu.show()
root_menu.set_submenu(menu)
return root_menu
def start_stop(self, w):
if self.button.get_label() == "Start":
filepath = self.entry.get_text().strip()
if os.path.isfile(filepath):
filepath = os.path.realpath(filepath)
self.button.set_label("Stop")
self.player.set_property("uri", "file://" + filepath)
self.player.set_state(Gst.State.PLAYING)
self.play_thread_id = thread.start_new_thread(self.play_thread, ())
else:
self.play_thread_id = None
self.player.set_state(Gst.State.NULL)
self.button.set_label("Start")
self.time_label.set_text("00:00 / 00:00")
def on_message(self, bus, message):
t = message.type
if t == Gst.MessageType.EOS:
self.play_thread_id = None
self.player.set_state(Gst.State.NULL)
self.button.set_label("Start")
self.time_label.set_text("00:00 / 00:00")
elif t == Gst.MessageType.ERROR:
self.play_thread_id = None
self.player.set_state(Gst.State.NULL)
err, debug = message.parse_error()
#print "Error: %s" % err, debug
self.button.set_label("Start")
self.time_label.set_text("00:00 / 00:00")
def demuxer_callback(self, demuxer, pad):
adec_pad = self.audio_decoder.get_static_pad("sink")
pad.link(adec_pad)
def rewind_callback(self, w):
rc, pos_int = self.player.query_position(Gst.Format.TIME)
seek_ns = pos_int - 10 * 1000000000
if seek_ns < 0:
seek_ns = 0
print 'Backward: %d ns -> %d ns' % (pos_int, seek_ns)
self.player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, seek_ns)
def forward_callback(self, w):
rc, pos_int = self.player.query_position(Gst.Format.TIME)
seek_ns = pos_int + 10 * 1000000000
print 'Forward: %d ns -> %d ns' % (pos_int, seek_ns)
self.player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, seek_ns)
def fast_callback(self, w):
self.pbRate += .25
print "rate changed to ", self.pbRate
rc, pos_int = self.player.query_position(Gst.Format.TIME)
print rc, pos_int
event = Gst.Event.new_seek(self.pbRate, Gst.Format.TIME,
Gst.SeekFlags.FLUSH|Gst.SeekFlags.ACCURATE,
Gst.SeekType.SET, pos_int, Gst.SeekType.NONE, 0)
self.player.send_event(event)
def slow_callback(self, w):
self.pbRate -= .25
print "rate changed to ", self.pbRate
rc, pos_int = self.player.query_position(Gst.Format.TIME)
print rc, pos_int
event = Gst.Event.new_seek(self.pbRate, Gst.Format.TIME,
Gst.SeekFlags.FLUSH|Gst.SeekFlags.ACCURATE,
Gst.SeekType.SET, pos_int, Gst.SeekType.NONE, 0)
self.player.send_event(event)
def convert_ns(self, t):
s,ns = divmod(t, 1000000000)
m,s = divmod(s, 60)
if m < 60:
return "%02i:%02i" %(m,s)
else:
h,m = divmod(m, 60)
return "%i:%02i:%02i" %(h,m,s)
def on_sync_message(self, bus, message):
if message.get_structure().get_name() == 'prepare-window-handle':
imagesink = message.src
imagesink.set_property("force-aspect-ratio", True)
imagesink.set_window_handle(self.movie_window.get_property('window').get_xid())
GObject.threads_init()
Gst.init(None)
GTK_Main()
Gtk.main()