-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathseek-to.lua
More file actions
165 lines (147 loc) · 4.56 KB
/
seek-to.lua
File metadata and controls
165 lines (147 loc) · 4.56 KB
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
local assdraw = require 'mp.assdraw'
local active = false
local mode = "time"
local time_scale = { 60*60*10, 60*60, 60*10, 60, 10, 1 }
local ass_begin = mp.get_property("osd-ass-cc/0")
local ass_end = mp.get_property("osd-ass-cc/1")
local last_mode = ""
local history = {
["time"] = {},
["percent"] = {}
}
local histpos = false
local edit = ""
-- timer to redraw periodically the message
-- to avoid leaving bindings when the seeker disappears for whatever reason
-- pretty hacky tbh
local timer = nil
local timer_duration = 3
function format(time)
local rel = ""
if string.sub(time, 1, 1) == "+" or string.sub(time, 1, 1) == "-" then
rel = string.sub(time, 1, 1)
time = string.sub(time, 2, #time)
end
if mode == "percent" then
return rel..(#time == 0 and '0' or '')..tostring(time)..'%'
elseif mode == "time" then
local s = time
for i = 1, 6 - #s do
s = '0'..s
end
return rel..string.sub(s, 1, 2)..':'..string.sub(s, 3, 4)..':'..string.sub(s, 5, 6)
end
end
function show_seeker()
time = histpos and history[mode][histpos] or edit
mp.osd_message("Seek to: "..ass_begin..format(time)..ass_end, timer_duration)
end
function edithist()
if histpos then
edit = history[mode][histpos]
histpos = false
end
end
function change_number(i)
if (i == '+' or i == '-') and #edit ~= 0 then
return
end
if i == 0 and #edit == 0 then
return
end
if mode == "time" and #edit >= 6 then
return
end
edit = edit..tostring(i)
end
function time_as_sec(time)
local sec = 0
for i = 1, #time do
sec = sec + (tonumber(string.sub(time, i, i)) * time_scale[#time_scale - (#time - i)])
end
return sec
end
function seek_to()
local time = 0
local rel = ""
if string.sub(edit, 1, 1) == "+" or string.sub(edit, 1, 1) == "-" then
rel = string.sub(edit, 1, 1)
edit = string.sub(edit, 2, #edit)
end
if mode == "percent" then
local d = mp.get_property_number("duration")
time = (tonumber(edit) / 100) * d
elseif mode == "time" then
time = time_as_sec(edit)
end
if #rel ~= 0 then
time = mp.get_property_number("time-pos") + (time * (rel == "+" and 1 or -1))
end
mp.commandv("osd-bar", "seek", time, "absolute")
--deduplicate historical timestamps
edit = rel..edit
for i = #history[mode], 1, -1 do
if history[mode][i] == edit then
table.remove(history[mode], i)
end
end
table.insert(history[mode], edit)
last_mode = mode
end
function seek_last()
if #last_mode == 0 or #history[last_mode] == 0 or active then
return
end
mode = last_mode
edit = history[mode][#history[mode]]
seek_to()
end
function backspace()
edit = string.sub(edit, 1, #(edit) - 1)
end
function history_move(up)
if not histpos and up then histpos = #history[mode] return end
if not histpos then return end
if up then
histpos = math.max(1, histpos - 1)
else
if histpos == #history[mode] then histpos = false return end
histpos = math.min(histpos + 1, #history[mode])
end
end
local key_mappings = {
BS = function() edithist() backspace() show_seeker() end,
ESC = function() set_inactive() end,
ENTER = function() edithist() seek_to() set_inactive() end,
UP = function() history_move(true) show_seeker() end,
DOWN = function() history_move(false) show_seeker() end,
["+"] = function() change_number("+") show_seeker() end,
["-"] = function() change_number("-") show_seeker() end
}
for i = 0, 9 do
local func = function() change_number(i) show_seeker() end
key_mappings[string.format("KP%d", i)] = func
key_mappings[string.format("%d", i)] = func
end
function set_active()
edit = ""
if not mp.get_property("seekable") then return end
for key, func in pairs(key_mappings) do
mp.add_forced_key_binding(key, "seek-to-"..key, func)
end
show_seeker()
timer = mp.add_periodic_timer(timer_duration, show_seeker)
active = true
end
function set_inactive()
mp.osd_message("")
for key, _ in pairs(key_mappings) do
mp.remove_key_binding("seek-to-"..key)
end
timer:kill()
histpos = false
active = false
end
mp.add_key_binding(nil, "toggle-seeker", function() if active then set_inactive() else mode = "time" set_active() end end)
mp.add_key_binding(nil, "toggle-seeker-percent", function() if active then set_inactive() else mode = "percent" set_active() end end)
mp.add_key_binding(nil, "seek-last", function() seek_last() end)