-
Notifications
You must be signed in to change notification settings - Fork 1
/
mplayer-web.py
304 lines (253 loc) · 6.66 KB
/
mplayer-web.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
#!/usr/bin/env python3
import datetime
import flask
import functools
import mplayer
import os
import re
import werkzeug.exceptions
VIDEOS_RE = re.compile('^[^[].*\.(mkv|avi|mpg|mpeg|mp4|webm|iso)$',
flags=re.IGNORECASE)
def all_files(dirs):
def gen_files(topdir, recurse):
if recurse:
for dirname, subdirs, filenames in os.walk(topdir):
for filename in filenames:
yield (dirname, filename)
else:
for entry in os.listdir(topdir):
yield (topdir, entry)
return list(os.path.join(dirname, filename)
for topdir, recurse in dirs
for dirname, filename in sorted(gen_files(topdir, recurse))
if VIDEOS_RE.match(filename))
TOP_DIRS = [
# topdir, recurse
('/backup/ext/pvr/', False),
('/backup/ext/films/', False),
('/backup/ext/films/series/', True),
('/backup/ext/films/youtube/', False),
]
class FileTable():
def __init__(self):
self.table = [('DVD', 'dvd://')] # list of (pretty name, real path)
self.selected_idx = 0
self.refresh()
def select(self, idx):
self.selected_idx = min(idx, len(self.table) - 1)
def select_next(self):
self.select(self.selected_idx+1)
def refresh(self):
files = all_files(TOP_DIRS)
self.table = [ self.table[0] ] + [
(os.path.basename(f), f) for f in files
]
self.select(self.selected_idx)
def get_selected_path(self):
return self.table[self.selected_idx][1]
def get_names(self):
return [ e[0] for e in self.table ]
FILE_TABLE = FileTable()
player = None
class PlayerWrapper(mplayer.Player):
def __init__(self, *args, **kwargs):
mpargs = kwargs.get('args', ())
mpargs += ('-msglevel', 'global=6')
mpargs += ('-v',) * 5
kwargs['args'] = mpargs
self.pw_delay = 0
super(PlayerWrapper, self).__init__(*args, **kwargs)
def player_init():
player_stop()
filename = FILE_TABLE.get_selected_path()
args = ()
if re.search('\.iso$', filename):
args += ('-dvd-device', filename)
target = 'dvd://'
else:
args += ('-utf8',)
target = filename
try:
suffix = filename.split('.')[-1]
base = filename.rstrip(suffix)
with open(base + 'delay') as f:
d = f.read().strip()
args += ('-subdelay', d)
except:
pass
global player
player = PlayerWrapper(args=args)
player.loadfile(target)
player.fullscreen = True
player.sub_select(0)
player.osd(1)
player.volume = 5
player.time_pos = 0
player.osd_show_property_text(os.path.basename(filename), 3000)
def player_stop():
global player
if player is not None:
player.quit()
player = None
def player_get_state():
if player is None:
return dict()
return {
'now': datetime.datetime.now().isoformat(),
'filename': player.filename,
'fullscreen': player.fullscreen,
'sub': player.sub,
'osd': player.osdlevel,
'paused': player.paused,
'volume': player.volume,
'mute': player.mute,
'time': player.time_pos,
'audio_track': player.switch_audio,
'sub_delay': player.sub_delay,
}
app = flask.Flask('mplayer-web')
@app.route('/')
def root():
if player is None:
FILE_TABLE.refresh()
page = flask.render_template(
'browse.html',
filenames=enumerate(FILE_TABLE.get_names()),
selected=FILE_TABLE.selected_idx,
)
else:
page = flask.render_template(
'play.html',
)
return page
@app.route('/names')
def names():
FILE_TABLE.refresh()
return flask.jsonify(dict(
names=FILE_TABLE.get_names(),
selected=FILE_TABLE.selected_idx,
))
@app.route('/select')
def select():
idx = flask.request.args.get('idx', '0')
try:
_idx = int(idx)
FILE_TABLE.select(_idx)
player_init()
return ''
except:
raise werkzeug.exceptions.BadRequest(
'Error: bad value for param "idx": "{}"'.format(idx))
@app.route('/state')
def state():
return flask.jsonify(player_get_state())
def pcommand(fun):
@functools.wraps(fun)
def real_fun(*args, **kwargs):
if player is not None:
fun(*args, **kwargs)
return ''
return real_fun
@app.route('/pause')
@pcommand
def pause():
player.osd_show_progression()
player.pause()
@app.route('/vol_inc')
@pcommand
def vol_inc():
v = round(player.volume + 5)
player.volume = v
player.osd_show_property_text('Vol: {}'.format(v), 1000)
@app.route('/vol_dec')
@pcommand
def vol_dec():
v = round(max(0, player.volume - 5))
player.volume = v
player.osd_show_property_text('Vol: {}'.format(v), 1000)
@app.route('/mute')
@pcommand
def mute():
player.mute = not(player.mute)
@app.route('/osd')
@pcommand
def osd():
player.osd()
@app.route('/stop')
@pcommand
def stop():
player_stop()
@app.route('/start')
@pcommand
def start():
player_init()
@app.route('/subp')
@pcommand
def subp():
player.sub_select(-2)
@app.route('/sub')
@pcommand
def sub():
player.sub_select()
@app.route('/fwd')
@pcommand
def fwd():
player.seek(10)
@app.route('/back')
@pcommand
def back():
player.seek(-10)
@app.route('/ffwd')
@pcommand
def ffwd():
player.seek(60)
@app.route('/fback')
@pcommand
def fback():
player.seek(-60)
@app.route('/fffwd')
@pcommand
def fffwd():
player.seek(600)
@app.route('/ffback')
@pcommand
def ffback():
player.seek(-600)
def probe_audio_tracks(track_nums):
# track numbers aren't sequential, probe for the next
for t in track_nums:
player.switch_audio = t
if player.switch_audio == t:
break
@app.route('/audio_next')
@pcommand
def audio_next():
x = player.volume
track = player.switch_audio
probe_audio_tracks(range(track+1, track+10, 1))
player.volume = x
@app.route('/audio_prev')
@pcommand
def audio_prev():
x = player.volume
track = player.switch_audio
probe_audio_tracks(range(track-1, track-10, -1))
player.volume = x
@app.route('/next')
@pcommand
def next():
player_stop()
FILE_TABLE.select_next()
player_init()
@app.route('/sub_delay_down')
@pcommand
def sub_delay_down():
player.sub_delay -= 0.3
player.pw_delay -= 300
player.osd_show_property_text('Sub delay: {}'.format(player.pw_delay), 1000)
@app.route('/sub_delay_up')
@pcommand
def sub_delay_up():
player.sub_delay += 0.3
player.pw_delay += 300
player.osd_show_property_text('Sub delay: {}'.format(player.pw_delay), 1000)