-
Notifications
You must be signed in to change notification settings - Fork 1
/
pb2player
executable file
·222 lines (193 loc) · 7.71 KB
/
pb2player
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
#!/usr/bin/env python
from __future__ import with_statement
from optparse import OptionParser
option_parser = OptionParser(description = "GStreamer-based player with variable-speed playback support",
usage = "%prog [options] [ mediafile.ext | playlist.m3u ]", version = "%prog 1.0.1")
(prog_options, prog_args) = option_parser.parse_args()
import sys, os
import pygst; pygst.require("0.10")
import gst
from callback_dict import CallbackDict
from gtkplayer import GTK_Main
class PlayerModel(CallbackDict):
def __init__(self):
super(PlayerModel, self).__init__()
self.filepath = None
self.rate = 1.0
self.playlist = []; self.playlist_idx = -1
self.movie_window_xid = None
self.player = gst.element_factory_make("playbin2", "player")
self.player_state = None
self.set_player_state("STOP")
bus = self.player.get_bus()
bus.add_signal_watch()
bus.connect("message", self.on_message)
bus.enable_sync_message_emission()
bus.connect("sync-message::element", self.on_sync_message)
def save_movie_window_xid(self, xid):
self.movie_window_xid = xid
def open_file(self, filepath = None):
self.set_player_state("STOP")
if filepath is None:
filepath = self.filepath
else:
self.filepath = filepath
print "File %s" % (filepath)
if os.path.isfile(filepath): filepath = "file://" + filepath
self.player.set_property("uri", filepath)
self.player.set_state(gst.STATE_PAUSED)
def load_file(self, filepath):
self.playlist = []; self.playlist_idx = -1
self.open_file(filepath)
def load_playlist(self, filepath):
(pl_dir, _) = os.path.split (filepath)
with open (filepath) as fpl:
self.playlist = list (os.path.join (pl_dir, fn.rstrip ("\n")) for fn in fpl)
self.playlist_idx = -1
def playlist_jump(self, nidx):
if nidx < 0 or nidx >= len(self.playlist): return False
if self.player_state != "STOP": self.set_player_state("STOP")
self.playlist_idx = nidx
self.open_file(self.playlist [self.playlist_idx])
return True
def forward_backward(self, delta):
if self.duration is None: return
pos_int = self.player.query_position(gst.FORMAT_TIME, None)[0]
self.player.seek (self.rate, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH,
gst.SEEK_TYPE_SET, pos_int + delta,
gst.SEEK_TYPE_NONE, -1)
def seek_percent(self, percent):
if self.duration is None: return
self.player.seek (self.rate, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH,
gst.SEEK_TYPE_SET, self.duration * percent / 100.0,
gst.SEEK_TYPE_NONE, -1)
def change_rate(self, rate, pos = None):
self.rate = rate; print "Rate %s" % (self.rate)
if self.duration is None: return
if pos is None:
pos = self.player.query_position(gst.FORMAT_TIME, None)[0]
self.player.seek (self.rate, gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH,
gst.SEEK_TYPE_SET, pos,
gst.SEEK_TYPE_NONE, -1)
def on_message(self, bus, message):
t = message.type
if t == gst.MESSAGE_ASYNC_DONE:
if self.player_state == "PLAY":
if self.duration is None: # 1st time only
self.duration = self.player.query_duration (gst.FORMAT_TIME, None) [0]
print "Duration %s" % (self.duration)
self.change_rate (self.rate, pos = 0)
self.player.set_state (gst.STATE_PLAYING)
if t == gst.MESSAGE_EOS:
self.set_player_state("STOP")
self.call_callbacks("player-state", "STOP", "EOS")
elif t == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
print "Error: %s" % err, debug
self.set_player_state("STOP")
self.call_callbacks("player-state", "STOP", "ERR")
def on_sync_message(self, bus, message):
if message.structure is None: return
message_name = message.structure.get_name()
if message_name == "prepare-xwindow-id":
imagesink = message.src
imagesink.set_property("force-aspect-ratio", True)
imagesink.set_xwindow_id(self.movie_window_xid)
def set_player_state(self, state):
if self.player_state == state: return
print "player_state", state
if state == "STOP":
self.player.set_state(gst.STATE_NULL)
self.duration = None
elif state == "PLAY":
self.player.set_state(gst.STATE_PAUSED if self.player_state == "STOP" else gst.STATE_PLAYING)
elif state == "PAUSE":
self.player.set_state(gst.STATE_PAUSED)
self.player_state = state
class PB2Player:
def __init__(self, options, args):
self.timeout = None
self.model = PlayerModel()
self.model.add_callback("player-state", self.on_model_state_changed)
self.ui = GTK_Main()
for m in "destroy,faster,slower,normal_speed,forward,backward,seek_percent,open,advance,iofn_changed,playpause,stop".split (","):
self.ui.add_callback(m, getattr (self, "on_" + m))
self.ui.provide_movie_window_xid(self.model.save_movie_window_xid)
if len (args) > 0:
self.open_something(os.path.abspath (args [0]))
def run(self):
self.ui.run()
def on_model_state_changed(self, state, reason):
print "player-state", state, reason
if state == "STOP" and reason != "ERR" and len (self.model.playlist) > 0:
self.on_advance(1)
else:
self.set_app_state("STOP")
def update_ui (self):
if self.model.player.get_state() [1] == gst.STATE_PLAYING:
self.ui.update_ui(100.0 * self.model.player.query_position (gst.FORMAT_TIME, None) [0] / self.model.duration)
return True
def on_iofn_changed(self, i):
i = i - 1
if i != self.model.playlist_idx:
print "Spun to %d" % i
self.model.playlist_jump(i)
self.set_app_state("PLAY")
def on_faster(self):
self.model.change_rate (self.model.rate * 1.07)
def on_slower(self):
self.model.change_rate (self.model.rate / 1.07)
def on_normal_speed(self):
self.model.change_rate (1.0)
def on_forward(self):
self.model.forward_backward(5000000000)
def on_backward(self):
self.model.forward_backward(-5000000000)
def on_seek_percent(self, percent):
self.model.seek_percent(percent)
def on_playpause(self):
if self.model.player_state == "PAUSE": # unpause
self.set_app_state("PLAY")
elif self.model.player_state == "STOP": # start (again)
self.model.load_file(None)
self.ui.set_fname_entry(self.model.filepath)
self.set_app_state("PLAY")
else: # pause
self.set_app_state("PAUSE")
def on_stop(self):
self.set_app_state("STOP")
def on_open(self, filepath):
self.open_something(filepath)
def open_something(self, filepath):
if filepath [-4:] == ".m3u":
self.model.load_playlist(filepath)
n = len (self.model.playlist)
self.ui.playlist_mode(True, n)
self.on_advance(1)
else:
self.set_app_state("STOP")
self.model.load_file(filepath)
self.ui.set_fname_entry(self.model.filepath)
self.ui.playlist_mode(False)
self.set_app_state("PLAY")
def on_advance(self, delta):
if self.model.playlist_jump(self.model.playlist_idx + delta):
self.ui.set_iofn(self.model.playlist_idx + 1)
self.ui.set_fname_entry(self.model.filepath)
self.set_app_state("PLAY")
def on_destroy(self):
self.model.set_player_state("STOP") # should wait...
self.ui.quit()
def set_app_state(self, state):
print "app_state", state
if state == "PLAY" and self.timeout is None:
print "timer started"
self.timeout = self.ui.create_timer (1, self.update_ui)
if state in ["PAUSE", "STOP"] and self.timeout is not None:
print "timer stopped"
self.ui.cancel_timer(self.timeout)
self.timeout = None
self.model.set_player_state(state)
self.ui.set_ui_state(state)
player = PB2Player(prog_options, prog_args)
player.run()