forked from gthreepw00d/mpv-iptv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptv.lua
503 lines (465 loc) · 10.7 KB
/
iptv.lua
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
--redefine keybindings here if needed; multiple bindings are possible
keybinds = {
activate = {'\\', 'MOUSE_BTN2'},
plsup = {'UP', 'MOUSE_BTN3'},
plsdown = {'DOWN', 'MOUSE_BTN4'},
plsenter = {'ENTER', 'MOUSE_BTN0'}
}
--hide playlist after specified number of seconds
osd_time=10
--show only specified number of playlist entries
window=7
--fade video when showing playlist
fade=false
--if fade=true; -100 — black, 0 — normal
plsbrightness=-70
--favorites get promotion to the top of the pls
favorites = {}
-- END OF CONFIGURABLE VARIABLES
local timer
--local plscount
local pattern=""
local is_active
local is_playlist_loaded
-- UTF-8 lower/upper conversion
local utf8_lc_uc = {
["a"] = "A",
["b"] = "B",
["c"] = "C",
["d"] = "D",
["e"] = "E",
["f"] = "F",
["g"] = "G",
["h"] = "H",
["i"] = "I",
["j"] = "J",
["k"] = "K",
["l"] = "L",
["m"] = "M",
["n"] = "N",
["o"] = "O",
["p"] = "P",
["q"] = "Q",
["r"] = "R",
["s"] = "S",
["t"] = "T",
["u"] = "U",
["v"] = "V",
["w"] = "W",
["x"] = "X",
["y"] = "Y",
["z"] = "Z",
["а"] = "А",
["б"] = "Б",
["в"] = "В",
["г"] = "Г",
["д"] = "Д",
["е"] = "Е",
["ж"] = "Ж",
["з"] = "З",
["и"] = "И",
["й"] = "Й",
["к"] = "К",
["л"] = "Л",
["м"] = "М",
["н"] = "Н",
["о"] = "О",
["п"] = "П",
["р"] = "Р",
["с"] = "С",
["т"] = "Т",
["у"] = "У",
["ф"] = "Ф",
["х"] = "Х",
["ц"] = "Ц",
["ч"] = "Ч",
["ш"] = "Ш",
["щ"] = "Щ",
["ъ"] = "Ъ",
["ы"] = "Ы",
["ь"] = "Ь",
["э"] = "Э",
["ю"] = "Ю",
["я"] = "Я",
["ё"] = "Ё"
}
local utf8_uc_lc = {
["A"] = "a",
["B"] = "b",
["C"] = "c",
["D"] = "d",
["E"] = "e",
["F"] = "f",
["G"] = "g",
["H"] = "h",
["I"] = "i",
["J"] = "j",
["K"] = "k",
["L"] = "l",
["M"] = "m",
["N"] = "n",
["O"] = "o",
["P"] = "p",
["Q"] = "q",
["R"] = "r",
["S"] = "s",
["T"] = "t",
["U"] = "u",
["V"] = "v",
["W"] = "w",
["X"] = "x",
["Y"] = "y",
["Z"] = "z",
["А"] = "а",
["Б"] = "б",
["В"] = "в",
["Г"] = "г",
["Д"] = "д",
["Е"] = "е",
["Ж"] = "ж",
["З"] = "з",
["И"] = "и",
["Й"] = "й",
["К"] = "к",
["Л"] = "л",
["М"] = "м",
["Н"] = "н",
["О"] = "о",
["П"] = "п",
["Р"] = "р",
["С"] = "с",
["Т"] = "т",
["У"] = "у",
["Ф"] = "ф",
["Х"] = "х",
["Ц"] = "ц",
["Ч"] = "ч",
["Ш"] = "ш",
["Щ"] = "щ",
["Ъ"] = "ъ",
["Ы"] = "ы",
["Ь"] = "ь",
["Э"] = "э",
["Ю"] = "ю",
["Я"] = "я",
["Ё"] = "ё"
}
--utf8 char pattern
local utf8_char="[\1-\127\192-\223][\128-\191]*"
local cyr_chars={'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я'}
-- символы, которые возможно вводить для поиска
local chars={}
for i=string.byte('a'),string.byte('z') do
table.insert(chars,i)
end
for i=string.byte('A'),string.byte('Z') do
table.insert(chars,i)
end
for i=string.byte('0'),string.byte('9') do
table.insert(chars,i)
end
for _,v in ipairs({',','^','$','(',')','%','.','[',']','*','+','-','?','`',"'",";"}) do
table.insert(chars,string.byte(v))
end
local keybinder = {
remove = function(action)
for i,_ in ipairs(keybinds[action]) do
mp.remove_key_binding(action..tostring(i))
end
end,
add = function(action, func, repeatable)
for i,key in ipairs(keybinds[action]) do
assert(type(func)=="function", "not a function")
if repeatable then
mp.add_forced_key_binding(key, action..tostring(i), func, "repeatable")
else
mp.add_forced_key_binding(key, action..tostring(i), func)
end
end
end
}
local fader = {
saved_brtns,
on = function(self)
if fade and not self.saved_brtns then
self.saved_brtns = mp.get_property("brightness")
mp.set_property("brightness", plsbrightness)
end
end,
off = function(self)
if fade and self.saved_brtns then
mp.set_property("brightness", self.saved_brtns)
self.saved_brtns=nil
end
end
}
local playlister = {
-- pls — список элементов плейлиста
pls,
-- plsfiltered — список индексов выбранных фильтром элементов плейлиста
plsfiltered,
plspos,
wndstart,
wndend,
cursor,
init = function(self)
if not self.pls then
self.pls = mp.get_property_native("playlist")
end
mp.commandv("stop")
--need to mark first entry non-current (mpv bug?)
if self.pls[1] then
self.pls[1].current = false
end
if favorites and #favorites>0 then
self:sortfavs()
end
pattern = ""
self.plsfiltered = tablekeys(self.pls)
end,
show = function(self)
local i
local newpos
local msg
--media-title
--playlist t[2].title
if not self.plsfiltered then
return
end
if not self.wndstart or not self.cursor then
self.wndstart=1
self.cursor=0
end
msg=""
i = self.wndstart
local prefix
while self.plsfiltered[i] and i<=self.wndstart+window-1 do
if self.pls[self.plsfiltered[i]].current then
prefix="*"
elseif i==self.wndstart+self.cursor then
prefix=">"
else
prefix=" "
end
msg = msg..prefix..(self.pls[self.plsfiltered[i]].title or "").."\n"
i=i+1
end
if self.wndstart>1 then
msg = "...\n"..msg
else
msg = " \n"..msg
end
if self.wndstart+window-1<#self.plsfiltered then
msg = msg.."..."
end
msg="/"..pattern.."\n"..msg
mp.osd_message(msg, osd_time)
end,
sortfavs = function(self)
--favorites bubbles to the top
local favs={}
local nonfavs={}
for _,v in ipairs(self.pls) do
if in_array(favorites,v.title) then
favs[#favs+1] = v
else
nonfavs[#nonfavs+1] = v
end
end
for i=1,#nonfavs do
favs[#favs+1] = nonfavs[i]
end
self.pls = favs
end,
filter = function(self)
self.plsfiltered={}
for i,v in ipairs(self.pls) do
if string.match(mylower(v.title),'.*'..prepat(pattern)..'.*') then
table.insert(self.plsfiltered,i)
end
end
self.wndstart=1
self.cursor=0
end,
down = function(self)
if self.cursor >= #self.plsfiltered-1 then return end
if self.cursor<window-1 then
self.cursor=self.cursor+1
else
if self.wndstart<#self.plsfiltered-window+1 then
self.wndstart=self.wndstart+1
end
end
self.show(self)
end,
up = function(self)
if self.cursor>0 then
self.cursor=self.cursor-1
self.show(self)
else
if self.wndstart>1 then
self.wndstart=self.wndstart-1
self.show(self)
end
end
end,
play = function(self)
mp.commandv("loadfile",self.pls[self.plsfiltered[self.wndstart+self.cursor]].filename)
if self.plspos then
self.pls[self.plspos].current=false
end
self.plspos=self.plsfiltered[self.wndstart+self.cursor]
self.pls[self.plspos].current=true
end
}
function add_bindings()
keybinder.add("plsup", up, true)
keybinder.add("plsdown", down, true)
for i,v in ipairs(chars) do
c=string.char(v)
mp.add_forced_key_binding(c, 'search'..v, typing(c),"repeatable")
end
mp.add_forced_key_binding('SPACE', 'search32', typing(' '),"repeatable")
--[[ mp.add_key_binding('а', 'search1000', typing('а'),"repeatable")
mp.add_key_binding('с', 'search1001', typing('с'),"repeatable")]]
mp.add_forced_key_binding('BS', 'searchbs', backspace,"repeatable")
keybinder.add("plsenter", play)
for i,v in ipairs(cyr_chars) do
mp.add_forced_key_binding(v, 'search'..i+1000, typing(v),"repeatable")
end
end
function remove_bindings()
keybinder.remove('plsup')
keybinder.remove('plsdown')
keybinder.remove('plsenter')
for i,v in ipairs(chars) do
c=string.char(v)
mp.remove_key_binding('search'..v)
end
mp.remove_key_binding('search32')
mp.remove_key_binding('searchbs')
for i,v in ipairs(cyr_chars) do
mp.remove_key_binding('search'..i+1000)
end
end
function activate()
if is_active then
shutdown()
return
else
is_active=true
fader:on()
playlister:show()
add_bindings()
if not timer then
timer=mp.add_periodic_timer(osd_time, shutdown)
timer.oneshot=true
else
resumetimer()
end
end
end
function tablekeys(t)
local result={}
for i,v in ipairs(t) do
table.insert(result,i)
end
return result
end
function in_array(array, value)
for _,v in ipairs(array) do
if v==value then
return true
end
end
return false
end
function mylower(s)
local res,n = string.gsub(s,utf8_char,function (c)
return utf8_uc_lc[c]
end)
return res
end
function myupper(s)
local res,n = string.gsub(s,utf8_char,function (c)
return utf8_lc_uc[c]
end)
return res
end
function prepat(s)
--prepare nocase and magic chars
s = string.gsub(s, "[%^%$%(%)%%%.%[%]%*%+%-%?]",function (c)
return '%'..c
end)
--[[ s = string.gsub(s, utf8_char, function (c)
return string.format("[%s%s]", utf8_uc_lc[c] or c, utf8_lc_uc[c] or c)
end)]]
return s
end
function resumetimer()
timer:kill()
timer:resume()
end
function typing(char)
return function()
local c=string.lower(char)
pattern = pattern..c
playlister:filter()
playlister:show()
resumetimer()
end
end
function backspace()
if string.len(pattern)>0 then
-- pattern = string.sub(pattern,1,-2)
-- for unicode
pattern = string.match(pattern,"(.*)"..utf8_char.."$")
playlister:filter()
playlister:show()
resumetimer()
end
end
function play()
-- mp.commandv("playlist-move", wndstart+cursor, 1)
-- mp.commandv("playlist-clear")
-- mp.commandv("playlist-next")
fader:off()
playlister:play()
playlister:show()
resumetimer()
end
function shutdown()
fader:off()
remove_bindings()
is_active=false
mp.osd_message("", 1)
end
function down()
fader:on()
playlister:down()
resumetimer()
end
function up()
fader:on()
playlister:up()
resumetimer()
end
function on_start_file()
if is_playlist_loaded then
playlister:init()
mp.unregister_event(on_start_file)
activate()
else
is_playlist_loaded = true
end
end
--~ function on_shutdown()
--~ fader:off()
--~ end
if mp.get_opt("iptv") then
mp.set_property_bool("idle", true)
mp.set_property_bool("force-window", true)
mp.register_event("start-file", on_start_file)
--~ mp.register_event("end-file", on_shutdown)
keybinder.add("activate", activate)
end