-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodernx.lua
3077 lines (2631 loc) · 111 KB
/
modernx.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- mpv-osc-morden by maoiscat
-- email:[email protected]
-- https://github.com/maoiscat/mpv-osc-morden
-- fork by cyl0
-- https://github.com/cyl0/MordenX
-- forked again by dexeonify
-- https://github.com/dexeonify/mpv-config/blob/main/scripts/modernx.lua
-- forked once again by 1-minute-to-midnight
-- https://github.com/1-minute-to-midnight/mpv-modern-x-compact/
local ipairs,loadfile,pairs,pcall,tonumber,tostring = ipairs,loadfile,pairs,pcall,tonumber,tostring
local debug,io,math,os,string,table,utf8 = debug,io,math,os,string,table,utf8
local min,max,floor,ceil,huge = math.min,math.max,math.floor,math.ceil,math.huge
local mp = require 'mp'
local assdraw = require 'mp.assdraw'
local msg = require 'mp.msg'
local opt = require 'mp.options'
local utils = require 'mp.utils'
--
-- Parameters
--
-- default user option values
-- do not touch, change them in osc.conf
local user_opts = {
showwindowed = true, -- show OSC when windowed?
showfullscreen = true, -- show OSC when fullscreen?
idlescreen = true, -- show mpv logo on idle
scalewindowed = 1, -- scaling of the controller when windowed
scalefullscreen = 1, -- scaling of the controller when fullscreen
scaleforcedwindow = 1.5, -- scaling when rendered on a forced window
vidscale = true, -- scale the controller with the video?
barmargin = 0, -- vertical margin of top/bottombar
boxalpha = 80, -- alpha of the background box,
-- 0 (opaque) to 255 (fully transparent)
hidetimeout = 500, -- duration in ms until the OSC hides if no
-- mouse movement. enforced non-negative for the
-- user, but internally negative is "always-on".
fadeduration = 200, -- duration of fade out in ms, 0 = no fade
deadzonesize = 0.5, -- size of deadzone
minmousemove = 0, -- minimum amount of pixels the mouse has to
-- move between ticks to make the OSC show up
iamaprogrammer = false, -- use native mpv values and disable OSC
-- internal track list management (and some
-- functions that depend on it)
layout = "modernx", -- set thumbnail layout
seekbarhandlesize = 0.6, -- size ratio of the knob handle
seekrangealpha = 64, -- transparency of seekranges
seekbarkeyframes = true, -- use keyframes when dragging the seekbar
title = "${media-title}", -- string compatible with property-expansion
-- to be shown as OSC title
tooltipborder = 1, -- border of tooltip in bottom/topbar
timetotal = false, -- display total time instead of remaining time?
timems = false, -- display timecodes with milliseconds?
visibility = "auto", -- only used at init to set visibility_mode(...)
windowcontrols = "auto", -- whether to show window controls
windowcontrols_alignment = "right", -- which side to show window controls on
windowcontrols_title = true, -- whether to show the title with the window controls
greenandgrumpy = false, -- disable santa hat
livemarkers = true, -- update seekbar chapter markers on duration change
chapters_osd = true, -- whether to show chapters OSD on next/prev
playlist_osd = true, -- whether to show playlist OSD on next/prev
chapter_fmt = "Chapter: %s", -- chapter print format for seekbar-hover. "no" to disable
showtitle = true, -- show title in OSC
showonpause = true, -- show OSC on pause
showonstart = true, -- show OSC on startup or when the next file in
-- playlist starts playing
showonseek = false, -- show OSC when seeking
movesub = true, -- move subtitles when the OSC is visible
titlefont = "", -- font used for the title above OSC and
-- in the window controls bar
blur_intensity = 150, -- adjust the strength of the OSC blur
osc_color = "000000", -- accent of the OSC and the title bar
seekbarfg_color = "E39C42", -- color of the seekbar progress and handle
seekbarbg_color = "FFFFFF", -- color of the remaining seekbar
tick_delay = 1 / 60, -- minimum interval between OSC redraws in seconds
tick_delay_follow_display_fps = false -- use display fps as the minimum interval
}
-- read options from config and command-line
opt.read_options(user_opts, "osc", function(list) update_options(list) end)
user_opts.userdataAvail = (function()
local list = mp.get_property_native("property-list")
for k,v in ipairs(list) do
if (v == "user-data") then
return true
end
end
return false
end)()
-- deus0ww - 2021-11-26
------------
-- tn_osc --
------------
local message = {
osc = {
registration = "tn_osc_registration",
reset = "tn_osc_reset",
update = "tn_osc_update",
finish = "tn_osc_finish",
},
debug = "Thumbnailer-debug",
queued = 1,
processing = 2,
ready = 3,
failed = 4,
}
-----------
-- Utils --
-----------
local OS_MAC, OS_WIN, OS_NIX = "MAC", "WIN", "NIX"
local function get_os()
if jit and jit.os then
if jit.os == "Windows" then return OS_WIN
elseif jit.os == "OSX" then return OS_MAC
else return OS_NIX end
end
if (package.config:sub(1,1) ~= "/") then return OS_WIN end
local res = mp.command_native({ name = "subprocess", args = {"uname", "-s"}, playback_only = false, capture_stdout = true, capture_stderr = true, })
return (res and res.stdout and res.stdout:lower():find("darwin") ~= nil) and OS_MAC or OS_NIX
end
local OPERATING_SYSTEM = get_os()
local function format_json(tab)
local json, err = utils.format_json(tab)
if err then msg.error("Formatting JSON failed:", err) end
if json then return json else return "" end
end
local function parse_json(json)
local tab, err = utils.parse_json(json, true)
if err then msg.error("Parsing JSON failed:", err) end
if tab then return tab else return {} end
end
local function join_paths(...)
local sep = OPERATING_SYSTEM == OS_WIN and "\\" or "/"
local result = ""
for _, p in ipairs({...}) do
result = (result == "") and p or result .. sep .. p
end
return result
end
--------------------
-- Data Structure --
--------------------
local tn_state, tn_osc, tn_osc_options, tn_osc_stats
local tn_thumbnails_indexed, tn_thumbnails_ready
local tn_gen_time_start, tn_gen_duration
local function reset_all()
tn_state = nil
tn_osc = {
cursor = {},
position = {},
scale = {},
osc_scale = {},
spacer = {},
osd = {},
background = {text = "︎✇",},
font_scale = {},
display_progress = {},
progress = {},
mini = {text = "⚆",},
thumbnail = {
visible = false,
path_last = nil,
x_last = nil,
y_last = nil,
},
}
tn_osc_options = nil
tn_osc_stats = {
queued = 0,
processing = 0,
ready = 0,
failed = 0,
total = 0,
total_expected = 0,
percent = 0,
timer = 0,
}
tn_thumbnails_indexed = {}
tn_thumbnails_ready = {}
tn_gen_time_start = nil
tn_gen_duration = nil
end
------------
-- TN OSC --
------------
local osc_reg = {
script_name = mp.get_script_name(),
osc_opts = {
scalewindowed = user_opts.scalewindowed,
scalefullscreen = user_opts.scalefullscreen,
},
}
mp.command_native({"script-message", message.osc.registration, format_json(osc_reg)})
local tn_palette = {
black = "000000",
white = "FFFFFF",
alpha_opaque = 0,
alpha_clear = 255,
alpha_black = min(255, user_opts.boxalpha),
alpha_white = min(255, user_opts.boxalpha + (255 - user_opts.boxalpha) * 0.8),
}
local tn_style_format = {
background = "{\\bord0\\1c&H%s&\\1a&H%X&}",
subbackground = "{\\bord0\\1c&H%s&\\1a&H%X&}",
spinner = "{\\bord0\\fs%d\\fscx%f\\fscy%f",
spinner2 = "\\1c&%s&\\1a&H%X&\\frz%d}",
closest_index = "{\\1c&H%s&\\1a&H%X&\\3c&H%s&\\3a&H%X&\\xbord%d\\ybord%d}",
progress_mini = "{\\bord0\\1c&%s&\\1a&H%X&\\fs18\\fscx%f\\fscy%f",
progress_mini2 = "\\frz%d}",
progress_block = "{\\bord0\\1c&H%s&\\1a&H%X&}",
progress_text = "{\\1c&%s&\\3c&H%s&\\1a&H%X&\\3a&H%X&\\blur0.25\\fs18\\fscx%f\\fscy%f\\xbord%f\\ybord%f}",
text_timer = "%.2ds",
text_progress = "%.3d/%.3d",
text_progress2 = "[%d]",
text_percent = "%d%%",
}
local tn_style = {
background = (tn_style_format.background):format(tn_palette.black, tn_palette.alpha_black),
subbackground = (tn_style_format.subbackground):format(tn_palette.white, tn_palette.alpha_white),
spinner = (tn_style_format.spinner):format(0, 1, 1),
closest_index = (tn_style_format.closest_index):format(tn_palette.white, tn_palette.alpha_black, tn_palette.black, tn_palette.alpha_black, -1, -1),
progress_mini = (tn_style_format.progress_mini):format(tn_palette.white, tn_palette.alpha_opaque, 1, 1),
progress_block = (tn_style_format.progress_block):format(tn_palette.white, tn_palette.alpha_white),
progress_text = (tn_style_format.progress_text):format(tn_palette.white, tn_palette.black, tn_palette.alpha_opaque, tn_palette.alpha_black, 1, 1, 2, 2),
}
local function set_thumbnail_above(offset)
local tn_osc = tn_osc
tn_osc.background.bottom = tn_osc.position.y - offset - tn_osc.spacer.bottom
tn_osc.background.top = tn_osc.background.bottom - tn_osc.background.h
tn_osc.thumbnail.top = tn_osc.background.bottom - tn_osc.thumbnail.h
tn_osc.progress.top = tn_osc.background.bottom - tn_osc.background.h
tn_osc.progress.mid = tn_osc.progress.top + tn_osc.progress.h * 0.5
tn_osc.background.rotation = -1
end
local function set_thumbnail_below(offset)
local tn_osc = tn_osc
tn_osc.background.top = tn_osc.position.y + offset + tn_osc.spacer.top
tn_osc.thumbnail.top = tn_osc.background.top
tn_osc.progress.top = tn_osc.background.top + tn_osc.thumbnail.h + tn_osc.spacer.y
tn_osc.progress.mid = tn_osc.progress.top + tn_osc.progress.h * 0.5
tn_osc.background.rotation = 1
end
local function set_mini_above() tn_osc.mini.y = (tn_osc.background.top - 12 * tn_osc.osc_scale.y) end
local function set_mini_below() tn_osc.mini.y = (tn_osc.background.bottom + 12 * tn_osc.osc_scale.y) end
local set_thumbnail_layout = {
topbar = function() tn_osc.spacer.top = 0.25
set_thumbnail_below(38.75)
set_mini_above() end,
bottombar = function() tn_osc.spacer.bottom = 0.25
set_thumbnail_above(38.75)
set_mini_below() end,
box = function() set_thumbnail_above(15)
set_mini_above() end,
slimbox = function() set_thumbnail_above(12)
set_mini_above() end,
modernx = function() set_thumbnail_above(20)
set_mini_below() end,
}
local function update_tn_osc_params(seek_y)
local tn_state, tn_osc_stats, tn_osc, tn_style, tn_style_format = tn_state, tn_osc_stats, tn_osc, tn_style, tn_style_format
tn_osc.scale.x, tn_osc.scale.y = get_virt_scale_factor()
tn_osc.osd.w, tn_osc.osd.h = mp.get_osd_size()
tn_osc.cursor.x, tn_osc.cursor.y = get_virt_mouse_pos()
tn_osc.position.y = seek_y
local osc_changed = false
if tn_osc.scale.x_last ~= tn_osc.scale.x or tn_osc.scale.y_last ~= tn_osc.scale.y
or tn_osc.w_last ~= tn_state.width or tn_osc.h_last ~= tn_state.height
or tn_osc.osd.w_last ~= tn_osc.osd.w or tn_osc.osd.h_last ~= tn_osc.osd.h
then
tn_osc.scale.x_last, tn_osc.scale.y_last = tn_osc.scale.x, tn_osc.scale.y
tn_osc.w_last, tn_osc.h_last = tn_state.width, tn_state.height
tn_osc.osd.w_last, tn_osc.osd.h_last = tn_osc.osd.w, tn_osc.osd.h
osc_changed = true
end
if osc_changed then
tn_osc.osc_scale.x, tn_osc.osc_scale.y = 1, 1
tn_osc.spacer.x, tn_osc.spacer.y = tn_osc_options.spacer, tn_osc_options.spacer
tn_osc.font_scale.x, tn_osc.font_scale.y = 100, 100
tn_osc.progress.h = (16 + tn_osc_options.spacer)
if not user_opts.vidscale then
tn_osc.osc_scale.x = tn_osc.scale.x * tn_osc_options.scale
tn_osc.osc_scale.y = tn_osc.scale.y * tn_osc_options.scale
tn_osc.spacer.x = tn_osc.osc_scale.x * tn_osc.spacer.x
tn_osc.spacer.y = tn_osc.osc_scale.y * tn_osc.spacer.y
tn_osc.font_scale.x = tn_osc.osc_scale.x * tn_osc.font_scale.x
tn_osc.font_scale.y = tn_osc.osc_scale.y * tn_osc.font_scale.y
tn_osc.progress.h = tn_osc.osc_scale.y * tn_osc.progress.h
end
tn_osc.spacer.top, tn_osc.spacer.bottom = tn_osc.spacer.y, tn_osc.spacer.y
tn_osc.thumbnail.w, tn_osc.thumbnail.h = tn_state.width * tn_osc.scale.x, tn_state.height * tn_osc.scale.y
tn_osc.osd.w_scaled, tn_osc.osd.h_scaled = tn_osc.osd.w * tn_osc.scale.x, tn_osc.osd.h * tn_osc.scale.y
tn_style.spinner = (tn_style_format.spinner):format(min(tn_osc.thumbnail.w, tn_osc.thumbnail.h) * 0.6667, tn_osc.font_scale.x, tn_osc.font_scale.y)
tn_style.closest_index = (tn_style_format.closest_index):format(tn_palette.white, tn_palette.alpha_black, tn_palette.black, tn_palette.alpha_black, -1 * tn_osc.scale.x, -1 * tn_osc.scale.y)
if tn_osc_stats.percent < 1 then
tn_style.progress_text = (tn_style_format.progress_text):format(tn_palette.white, tn_palette.black, tn_palette.alpha_opaque, tn_palette.alpha_black, tn_osc.font_scale.x, tn_osc.font_scale.y, 2 * tn_osc.scale.x, 2 * tn_osc.scale.y)
tn_style.progress_mini = (tn_style_format.progress_mini):format(tn_palette.white, tn_palette.alpha_opaque, tn_osc.font_scale.x, tn_osc.font_scale.y)
end
end
if not tn_osc.position.y then return end
if (osc_changed or tn_osc.cursor.x_last ~= tn_osc.cursor.x) and tn_osc.osd.w_scaled >= (tn_osc.thumbnail.w + 2 * tn_osc.spacer.x) then
tn_osc.cursor.x_last = tn_osc.cursor.x
if tn_osc_options.centered then
tn_osc.position.x = tn_osc.osd.w_scaled * 0.5
else
local limit_left = tn_osc.spacer.x + tn_osc.thumbnail.w * 0.5
local limit_right = tn_osc.osd.w_scaled - limit_left
tn_osc.position.x = min(max(tn_osc.cursor.x, limit_left), limit_right)
end
tn_osc.thumbnail.left, tn_osc.thumbnail.right = tn_osc.position.x - tn_osc.thumbnail.w * 0.5, tn_osc.position.x + tn_osc.thumbnail.w * 0.5
tn_osc.mini.x = tn_osc.thumbnail.right - 6 * tn_osc.osc_scale.x
end
if (osc_changed or tn_osc.display_progress.last ~= tn_osc.display_progress.current) then
tn_osc.display_progress.last = tn_osc.display_progress.current
tn_osc.background.h = tn_osc.thumbnail.h + (tn_osc.display_progress.current and (tn_osc.progress.h + tn_osc.spacer.y) or 0)
set_thumbnail_layout[user_opts.layout]()
end
end
local function find_closest(seek_index, round_up)
local tn_state, tn_thumbnails_indexed, tn_thumbnails_ready = tn_state, tn_thumbnails_indexed, tn_thumbnails_ready
if not (tn_thumbnails_indexed and tn_thumbnails_ready) then return nil, nil end
local time_index = floor(seek_index * tn_state.delta)
if tn_thumbnails_ready[time_index] then return seek_index + 1, tn_thumbnails_indexed[time_index] end
local direction, index = round_up and 1 or -1
for i = 1, tn_osc_stats.total_expected do
index = seek_index + (i * direction)
time_index = floor(index * tn_state.delta)
if tn_thumbnails_ready[time_index] then return index + 1, tn_thumbnails_indexed[time_index] end
index = seek_index + (i * -direction)
time_index = floor(index * tn_state.delta)
if tn_thumbnails_ready[time_index] then return index + 1, tn_thumbnails_indexed[time_index] end
end
return nil, nil
end
local draw_cmd = { name = "overlay-add", id = 9, offset = 0, fmt = "bgra" }
local hide_cmd = { name = "overlay-remove", id = 9}
local function draw_thumbnail(x, y, path)
draw_cmd.x = x
draw_cmd.y = y
draw_cmd.file = path
mp.command_native(draw_cmd)
tn_osc.thumbnail.visible = true
end
local function hide_thumbnail()
if tn_osc and tn_osc.thumbnail and tn_osc.thumbnail.visible then
mp.command_native(hide_cmd)
tn_osc.thumbnail.visible = false
end
end
local function show_thumbnail(seek_percent)
if not seek_percent then return nil, nil end
local scale, thumbnail, total_expected, ready = tn_osc.scale, tn_osc.thumbnail, tn_osc_stats.total_expected, tn_osc_stats.ready
local seek = seek_percent * (total_expected - 1)
local seek_index = floor(seek + 0.5)
local closest_index, path = thumbnail.closest_index_last, thumbnail.path_last
if thumbnail.seek_index_last ~= seek_index
or thumbnail.ready_last ~= ready
or thumbnail.total_expected_last ~= tn_osc_stats.total_expected
then
closest_index, path = find_closest(seek_index, seek_index < seek)
thumbnail.closest_index_last, thumbnail.total_expected_last, thumbnail.ready_last, thumbnail.seek_index_last = closest_index, total_expected, ready, seek_index
end
local x, y = floor((thumbnail.left or 0) / scale.x + 0.5), floor((thumbnail.top or 0) / scale.y + 0.5)
if path and not (thumbnail.visible and thumbnail.x_last == x and thumbnail.y_last == y and thumbnail.path_last == path) then
thumbnail.x_last, thumbnail.y_last, thumbnail.path_last = x, y, path
draw_thumbnail(x, y, path)
end
return closest_index, path
end
local function ass_new(ass, x, y, align, style, text)
ass:new_event()
ass:pos(x, y)
if align then ass:an(align) end
if style then ass:append(style) end
if text then ass:append(text) end
end
local function ass_rect(ass, x1, y1, x2, y2)
ass:draw_start()
ass:rect_cw(x1, y1, x2, y2)
ass:draw_stop()
end
local draw_progress = {
[message.queued] = function(ass, index, block_w, block_h) ass:rect_cw((index - 1) * block_w, 0, index * block_w, block_h) end,
[message.processing] = function(ass, index, block_w, block_h) ass:rect_cw((index - 1) * block_w, block_h * 0.2, index * block_w, block_h * 0.8) end,
[message.failed] = function(ass, index, block_w, block_h) ass:rect_cw((index - 1) * block_w, block_h * 0.4, index * block_w, block_h * 0.6) end,
}
local function display_tn_osc(seek_y, seek_percent, ass)
if not (seek_y and seek_percent and ass and tn_state and tn_osc_stats and tn_osc_options and tn_state.width and tn_state.height and tn_state.duration and tn_state.cache_dir) or not tn_osc_options.visible then hide_thumbnail() return end
update_tn_osc_params(seek_y)
local tn_osc_stats, tn_osc, tn_style, tn_style_format, ass_new, ass_rect, seek_percent = tn_osc_stats, tn_osc, tn_style, tn_style_format, ass_new, ass_rect, seek_percent * 0.01
local closest_index, path = show_thumbnail(seek_percent)
-- Background
ass_new(ass, tn_osc.thumbnail.left, tn_osc.background.top, 7, tn_style.background)
ass_rect(ass, -tn_osc.spacer.x, -tn_osc.spacer.top, tn_osc.thumbnail.w + tn_osc.spacer.x, tn_osc.background.h + tn_osc.spacer.bottom)
local spinner_color, spinner_alpha = tn_palette.white, tn_palette.alpha_white
if not path then
ass_new(ass, tn_osc.thumbnail.left, tn_osc.thumbnail.top, 7, tn_style.subbackground)
ass_rect(ass, 0, 0, tn_osc.thumbnail.w, tn_osc.thumbnail.h)
spinner_color, spinner_alpha = tn_palette.black, tn_palette.alpha_black
end
ass_new(ass, tn_osc.position.x, tn_osc.thumbnail.top + tn_osc.thumbnail.h * 0.5, 5, tn_style.spinner .. (tn_style_format.spinner2):format(spinner_color, spinner_alpha, tn_osc.background.rotation * seek_percent * 1080), tn_osc.background.text)
-- Mini Progress Spinner
if tn_osc.display_progress.current ~= nil and not tn_osc.display_progress.current and tn_osc_stats.percent < 1 then
ass_new(ass, tn_osc.mini.x, tn_osc.mini.y, 5, tn_style.progress_mini .. (tn_style_format.progress_mini2):format(tn_osc_stats.percent * -360 + 90), tn_osc.mini.text)
end
-- Progress Bar
if tn_osc.display_progress.current then
local block_w, index = tn_osc_stats.total_expected > 0 and tn_state.width * tn_osc.scale.y / tn_osc_stats.total_expected or 0, 0
if tn_thumbnails_indexed and block_w > 0 then
-- Loading bar
ass_new(ass, tn_osc.thumbnail.left, tn_osc.progress.top, 7, tn_style.progress_block)
ass:draw_start()
for time_index, status in pairs(tn_thumbnails_indexed) do
index = floor(time_index / tn_state.delta) + 1
if index ~= closest_index and not tn_thumbnails_ready[time_index] and index <= tn_osc_stats.total_expected and draw_progress[status] ~= nil then
draw_progress[status](ass, index, block_w, tn_osc.progress.h)
end
end
ass:draw_stop()
if closest_index and closest_index <= tn_osc_stats.total_expected then
ass_new(ass, tn_osc.thumbnail.left, tn_osc.progress.top, 7, tn_style.closest_index)
ass_rect(ass, (closest_index - 1) * block_w, 0, closest_index * block_w, tn_osc.progress.h)
end
end
-- Text: Timer
ass_new(ass, tn_osc.thumbnail.left + 3 * tn_osc.osc_scale.y, tn_osc.progress.mid, 4, tn_style.progress_text, (tn_style_format.text_timer):format(tn_osc_stats.timer))
-- Text: Number or Index of Thumbnail
local temp = tn_osc_stats.percent < 1 and tn_osc_stats.ready or closest_index
local processing = tn_osc_stats.processing > 0 and (tn_style_format.text_progress2):format(tn_osc_stats.processing) or ""
ass_new(ass, tn_osc.position.x, tn_osc.progress.mid, 5, tn_style.progress_text, (tn_style_format.text_progress):format(temp and temp or 0, tn_osc_stats.total_expected) .. processing)
-- Text: Percentage
ass_new(ass, tn_osc.thumbnail.right - 3 * tn_osc.osc_scale.y, tn_osc.progress.mid, 6, tn_style.progress_text, (tn_style_format.text_percent):format(min(100, tn_osc_stats.percent * 100)))
end
end
---------------
-- Listeners --
---------------
mp.register_script_message(message.osc.reset, function()
hide_thumbnail()
reset_all()
end)
local text_progress_format = { two_digits = "%.2d/%.2d", three_digits = "%.3d/%.3d" }
mp.register_script_message(message.osc.update, function(json)
local new_data = parse_json(json)
if not new_data then return end
if new_data.state then
tn_state = new_data.state
if tn_state.is_rotated then tn_state.width, tn_state.height = tn_state.height, tn_state.width end
draw_cmd.w = tn_state.width
draw_cmd.h = tn_state.height
draw_cmd.stride = tn_state.width * 4
end
if new_data.osc_options then tn_osc_options = new_data.osc_options end
if new_data.osc_stats then
tn_osc_stats = new_data.osc_stats
if tn_osc_options and tn_osc_options.show_progress then
if tn_osc_options.show_progress == 0 then tn_osc.display_progress.current = false
elseif tn_osc_options.show_progress == 1 then tn_osc.display_progress.current = tn_osc_stats.percent < 1
else tn_osc.display_progress.current = true end
end
tn_style_format.text_progress = tn_osc_stats.total > 99 and text_progress_format.three_digits or text_progress_format.two_digits
if tn_osc_stats.percent >= 1 then mp.command_native({"script-message", message.osc.finish}) end
end
if new_data.thumbnails and tn_state then
local index, ready
for time_string, status in pairs(new_data.thumbnails) do
index, ready = tonumber(time_string), (status == message.ready)
tn_thumbnails_indexed[index] = ready and join_paths(tn_state.cache_dir, time_string) .. tn_state.cache_extension or status
tn_thumbnails_ready[index] = ready
end
end
request_tick()
end)
mp.register_script_message(message.debug, function()
msg.info("Thumbnailer OSC Internal States:")
msg.info("tn_state:", tn_state and utils.to_string(tn_state) or "nil")
msg.info("tn_thumbnails_indexed:", tn_thumbnails_indexed and utils.to_string(tn_thumbnails_indexed) or "nil")
msg.info("tn_thumbnails_ready:", tn_thumbnails_ready and utils.to_string(tn_thumbnails_ready) or "nil")
msg.info("tn_osc_options:", tn_osc_options and utils.to_string(tn_osc_options) or "nil")
msg.info("tn_osc_stats:", tn_osc_stats and utils.to_string(tn_osc_stats) or "nil")
msg.info("tn_osc:", tn_osc and utils.to_string(tn_osc) or "nil")
end)
-----------------
-- modernx.lua --
-----------------
local osc_param = { -- calculated by osc_init()
playresy = 0, -- canvas size Y
playresx = 0, -- canvas size X
display_aspect = 1,
unscaled_y = 0,
areas = {},
video_margins = {
l = 0, r = 0, t = 0, b = 0, -- left/right/top/bottom
},
}
local osc_styles = {
transBg = "{\\blur100\\bord" .. user_opts.blur_intensity .. "\\1c&H000000&\\3c&H" .. user_opts.osc_color .. "&}",
seekbarBg = "{\\blur0\\bord0\\1c&H" .. user_opts.seekbarbg_color .. "&}",
seekbarFg = "{\\blur1\\bord1\\1c&H" .. user_opts.seekbarfg_color .. "&}",
bigButtons = "{\\blur0\\bord0\\1c&HFFFFFF&\\3c&HFFFFFF&\\fs18\\fnmodernx-osc-icon}",
mediumButtons = "{\\blur0\\bord0\\1c&HFFFFFF&\\3c&HFFFFFF&\\fs18\\fnmodernx-osc-icon}",
smallButtons = "{\\blur0\\bord0\\1c&HFFFFFF&\\3c&HFFFFFF&\\fs15\\fnmodernx-osc-icon}",
timecodes = "{\\blur0\\bord0\\1c&HFFFFFF&\\3c&H000000&\\fs13}",
tooltip = "{\\blur1\\bord" .. user_opts.tooltipborder .. "\\1c&HFFFFFF&\\3c&H000000&\\fs13}",
vidTitle = "{\\blur1\\bord0.5\\1c&HFFFFFF&\\3c&H0\\fs26\\q2\\fn" .. user_opts.titlefont .. "}",
wcButtons = "{\\1c&HFFFFFF&\\fs20\\fnmodernx-osc-icon}",
wcTitle = "{\\1c&HFFFFFF&\\fs24\\q2\\fn" .. user_opts.titlefont .. "}",
wcBar = "{\\1c&H" .. user_opts.osc_color .. "}",
elementDown = "{\\1c&H999999&}",
elementHover = "{\\blur5\\1c&HFFFFFF&}"
}
local osc_icons = {
close = "\xEE\xA4\x80",
minimize = "\xEE\xA4\x81",
restore = "\xEE\xA4\x82",
maximize = "\xEE\xA4\x83",
volume_mute = "\xEE\xA4\x84",
volume_low = "\xEE\xA4\x85",
volume_med = "\xEE\xA4\x86",
volume_high = "\xEE\xA4\x87",
volume_loud = "\xEE\xA4\x88",
audio = "\xEE\xA4\x89",
subtitle = "\xEE\xA4\x90",
info = "\xEE\xA4\x91",
fullscreen_exit = "\xEE\xA4\x92",
fullscreen = "\xEE\xA4\x93",
playlist_prev = "\xEE\xA4\x94",
playlist_next = "\xEE\xA4\x95",
chapter_prev = "\xEE\xA4\x96",
chapter_next = "\xEE\xA4\x97",
play = "\xEE\xA4\x98",
pause = "\xEE\xA4\x99",
skipback = "\xEE\xA4\xA0",
skipforward = "\xEE\xA4\xA1",
}
-- internal states, do not touch
local state = {
showtime, -- time of last invocation (last mouse move)
osc_visible = false,
anistart, -- time when the animation started
anitype, -- current type of animation
animation, -- current animation alpha
mouse_down_counter = 0, -- used for softrepeat
active_element = nil, -- nil = none, 0 = background, 1+ = see elements[]
active_event_source = nil, -- the "button" that issued the current event
rightTC_trem = not user_opts.timetotal, -- if the right timecode should display total or remaining time
tc_ms = user_opts.timems, -- Should the timecodes display their time with milliseconds
mp_screen_sizeX, mp_screen_sizeY, -- last screen-resolution, to detect resolution changes to issue reINITs
initREQ = false, -- is a re-init request pending?
marginsREQ = false, -- is a margins update pending?
last_mouseX, last_mouseY, -- last mouse position, to detect significant mouse movement
mouse_in_window = false,
message_text,
message_hide_timer,
fullscreen = false,
tick_timer = nil,
tick_last_time = 0, -- when the last tick() was run
hide_timer = nil,
cache_state = nil,
idle = false,
enabled = true,
input_enabled = true,
showhide_enabled = false,
dmx_cache = 0,
border = true,
maximized = false,
osd = mp.create_osd_overlay("ass-events"),
chapter_list = {}, -- sorted by time
lastvisibility = user_opts.visibility, -- save last visibility on pause if showonpause
subpos = 100, -- last value of sub-pos set by the user
}
local thumbfast = {
width = 0,
height = 0,
disabled = true,
available = false
}
local window_control_box_width = 80
local tick_delay = 1 / 60
local is_december = os.date("*t").month == 12
--- Automatically disable OSC
local builtin_osc_enabled = mp.get_property_native("osc")
if builtin_osc_enabled then
mp.set_property_native("osc", false)
end
--
-- Helperfunctions
--
function kill_animation()
state.anistart = nil
state.animation = nil
state.anitype = nil
end
function set_osd(res_x, res_y, text)
if state.osd.res_x == res_x and
state.osd.res_y == res_y and
state.osd.data == text then
return
end
state.osd.res_x = res_x
state.osd.res_y = res_y
state.osd.data = text
state.osd.z = 1000
state.osd:update()
end
-- scale factor for translating between real and virtual ASS coordinates
function get_virt_scale_factor()
local w, h = mp.get_osd_size()
if w <= 0 or h <= 0 then
return 0, 0
end
return osc_param.playresx / w, osc_param.playresy / h
end
-- return mouse position in virtual ASS coordinates (playresx/y)
function get_virt_mouse_pos()
if state.mouse_in_window then
local sx, sy = get_virt_scale_factor()
local x, y = mp.get_mouse_pos()
return x * sx, y * sy
else
return -1, -1
end
end
function set_virt_mouse_area(x0, y0, x1, y1, name)
local sx, sy = get_virt_scale_factor()
mp.set_mouse_area(x0 / sx, y0 / sy, x1 / sx, y1 / sy, name)
end
function scale_value(x0, x1, y0, y1, val)
local m = (y1 - y0) / (x1 - x0)
local b = y0 - (m * x0)
return (m * val) + b
end
-- returns hitbox spanning coordinates (top left, bottom right corner)
-- according to alignment
function get_hitbox_coords(x, y, an, w, h)
local alignments = {
[1] = function () return x, y-h, x+w, y end,
[2] = function () return x-(w/2), y-h, x+(w/2), y end,
[3] = function () return x-w, y-h, x, y end,
[4] = function () return x, y-(h/2), x+w, y+(h/2) end,
[5] = function () return x-(w/2), y-(h/2), x+(w/2), y+(h/2) end,
[6] = function () return x-w, y-(h/2), x, y+(h/2) end,
[7] = function () return x, y, x+w, y+h end,
[8] = function () return x-(w/2), y, x+(w/2), y+h end,
[9] = function () return x-w, y, x, y+h end,
}
return alignments[an]()
end
function get_hitbox_coords_geo(geometry)
return get_hitbox_coords(geometry.x, geometry.y, geometry.an,
geometry.w, geometry.h)
end
function get_element_hitbox(element)
return element.hitbox.x1, element.hitbox.y1,
element.hitbox.x2, element.hitbox.y2
end
function mouse_hit(element)
return mouse_hit_coords(get_element_hitbox(element))
end
function mouse_hit_coords(bX1, bY1, bX2, bY2)
local mX, mY = get_virt_mouse_pos()
return (mX >= bX1 and mX <= bX2 and mY >= bY1 and mY <= bY2)
end
function limit_range(min, max, val)
if val > max then
val = max
elseif val < min then
val = min
end
return val
end
-- translate value into element coordinates
function get_slider_ele_pos_for(element, val)
local ele_pos = scale_value(
element.slider.min.value, element.slider.max.value,
element.slider.min.ele_pos, element.slider.max.ele_pos,
val)
return limit_range(
element.slider.min.ele_pos, element.slider.max.ele_pos,
ele_pos)
end
-- translates global (mouse) coordinates to value
function get_slider_value_at(element, glob_pos)
local val = scale_value(
element.slider.min.glob_pos, element.slider.max.glob_pos,
element.slider.min.value, element.slider.max.value,
glob_pos)
return limit_range(
element.slider.min.value, element.slider.max.value,
val)
end
-- get value at current mouse position
function get_slider_value(element)
return get_slider_value_at(element, get_virt_mouse_pos())
end
function countone(val)
if not (user_opts.iamaprogrammer) then
val = val + 1
end
return val
end
-- align: -1 .. +1
-- frame: size of the containing area
-- obj: size of the object that should be positioned inside the area
-- margin: min. distance from object to frame (as long as -1 <= align <= +1)
function get_align(align, frame, obj, margin)
return (frame / 2) + (((frame / 2) - margin - (obj / 2)) * align)
end
-- multiplies two alpha values, formular can probably be improved
function mult_alpha(alphaA, alphaB)
return 255 - (((1-(alphaA/255)) * (1-(alphaB/255))) * 255)
end
function add_area(name, x1, y1, x2, y2)
-- create area if needed
if (osc_param.areas[name] == nil) then
osc_param.areas[name] = {}
end
table.insert(osc_param.areas[name], {x1=x1, y1=y1, x2=x2, y2=y2})
end
function ass_append_alpha(ass, alpha, modifier)
local ar = {}
for ai, av in pairs(alpha) do
av = mult_alpha(av, modifier)
if state.animation then
av = mult_alpha(av, state.animation)
end
ar[ai] = av
end
ass:append(string.format("{\\1a&H%X&\\2a&H%X&\\3a&H%X&\\4a&H%X&}",
ar[1], ar[2], ar[3], ar[4]))
end
function ass_draw_cir_cw(ass, x, y, r)
ass:round_rect_cw(x-r, y-r, x+r, y+r, r)
end
function ass_draw_rr_h_cw(ass, x0, y0, x1, y1, r1, hexagon, r2)
if hexagon then
ass:hexagon_cw(x0, y0, x1, y1, r1, r2)
else
ass:round_rect_cw(x0, y0, x1, y1, r1, r2)
end
end
function ass_draw_rr_h_ccw(ass, x0, y0, x1, y1, r1, hexagon, r2)
if hexagon then
ass:hexagon_ccw(x0, y0, x1, y1, r1, r2)
else
ass:round_rect_ccw(x0, y0, x1, y1, r1, r2)
end
end
function round(number, decimals)
local power = 10^(decimals or 1)
return math.floor(number * power + 0.5) / power
end
--
-- Tracklist Management
--
local nicetypes = {video = "Video", audio = "Audio", sub = "Subtitle"}
-- updates the OSC internal playlists, should be run each time the track-layout changes
function update_tracklist()
local tracktable = mp.get_property_native("track-list", {})
-- by osc_id
tracks_osc = {}
tracks_osc.video, tracks_osc.audio, tracks_osc.sub = {}, {}, {}
-- by mpv_id
tracks_mpv = {}
tracks_mpv.video, tracks_mpv.audio, tracks_mpv.sub = {}, {}, {}
for n = 1, #tracktable do
if not (tracktable[n].type == "unknown") then
local type = tracktable[n].type
local mpv_id = tonumber(tracktable[n].id)
-- by osc_id
table.insert(tracks_osc[type], tracktable[n])
-- by mpv_id
tracks_mpv[type][mpv_id] = tracktable[n]
tracks_mpv[type][mpv_id].osc_id = #tracks_osc[type]
end
end
end
-- return a nice list of tracks of the given type (video, audio, sub)
function get_tracklist(type)
local msg = "Available " .. nicetypes[type] .. " Tracks: "
if not tracks_osc or #tracks_osc[type] == 0 then
msg = msg .. "none"
else
for n = 1, #tracks_osc[type] do
local track = tracks_osc[type][n]
local lang, title, selected = "unknown", "", "○"
if not(track.lang == nil) then lang = track.lang end
if not(track.title == nil) then title = track.title end
if (track.id == tonumber(mp.get_property(type))) then
selected = "●"
end
msg = msg.."\n"..selected.." "..n..": ["..lang.."] "..title
end
end
return msg
end
-- relatively change the track of given <type> by <next> tracks
--(+1 -> next, -1 -> previous)
function set_track(type, next)
local current_track_mpv, current_track_osc
if (mp.get_property(type) == "no") then
current_track_osc = 0
else
current_track_mpv = tonumber(mp.get_property(type))
current_track_osc = tracks_mpv[type][current_track_mpv].osc_id
end
local new_track_osc = (current_track_osc + next) % (#tracks_osc[type] + 1)
local new_track_mpv
if new_track_osc == 0 then
new_track_mpv = "no"
else
new_track_mpv = tracks_osc[type][new_track_osc].id
end
mp.commandv("set", type, new_track_mpv)
if (new_track_osc == 0) then
show_message(nicetypes[type] .. " Track: none")
else
show_message(nicetypes[type] .. " Track: "
.. new_track_osc .. "/" .. #tracks_osc[type]
.. " [".. (tracks_osc[type][new_track_osc].lang or "unknown") .."] "
.. (tracks_osc[type][new_track_osc].title or ""))
end
end
-- get the currently selected track of <type>, OSC-style counted
function get_track(type)
local track = mp.get_property(type)
if track ~= "no" and track ~= nil then
local tr = tracks_mpv[type][tonumber(track)]
if tr then
return tr.osc_id
end
end
return 0
end
-- WindowControl helpers
function window_controls_enabled()
val = user_opts.windowcontrols
if val == "auto" then
return not state.border
elseif val == "fullscreen_only" then
return (not state.border) or state.fullscreen
else
return val ~= "no"
end
end
function window_controls_alignment()
return user_opts.windowcontrols_alignment
end
--
-- Element Management
--
local elements = {}
function prepare_elements()
-- remove elements without layout or invisble
local elements2 = {}
for n, element in pairs(elements) do
if not (element.layout == nil) and (element.visible) then
table.insert(elements2, element)
end
end
elements = elements2
function elem_compare (a, b)
return a.layout.layer < b.layout.layer
end
table.sort(elements, elem_compare)
for _,element in pairs(elements) do
local elem_geo = element.layout.geometry
-- Calculate the hitbox
local bX1, bY1, bX2, bY2 = get_hitbox_coords_geo(elem_geo)
element.hitbox = {x1 = bX1, y1 = bY1, x2 = bX2, y2 = bY2}