forked from zenyd/mpv-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed-transition.lua
285 lines (265 loc) · 10.1 KB
/
speed-transition.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
lookahead = 5 --if the next subtitle appears after this threshold then speedup
speedup = 2 --the value that "speed" is set to during speedup
leadin = 1 --seconds to stop short of the next subtitle
skipmode = false --instead of speeding up playback seek to the next known subtitle
maxSkip = 5 --max seek distance (seconds) when skipmode is enabled
minSkip = leadin --this is also configurable but setting it too low can actually make your watch time longer
skipdelay = 1 --in skip mode, this setting delays each skip by x seconds (must be >=0)
directskip = false --seek to next known subtitle (must be in cache) no matter how far away
dropOnAVdesync = true
--Because mpv syncs subtitles to audio it is possible that if audio processing lags behind--
--video processing then normal playback may not resume in sync with the video. If "avsync" > leadin--
--then this disables the audio so that we can ensure normal playback resumes on time.
ignorePattern = false --if true, subtitles are matched against "subPattern". A successful match will be treated as if there was no subtitle
subPattern = "^[#♯♩♪♬♫🎵🎶%[%(]+.*[#♯♩♪♬♫🎵🎶%]%)]+$"
---------------User options above this line--
readahead_secs = mp.get_property_native("demuxer-readahead-secs")
normalspeed=mp.get_property_native("speed")
function shouldIgnore(subtext)
if ignorePattern and subtext and subtext~="" then
local st = subtext:match("^%s*(.-)%s*$") -- trim whitespace
if st:find(subPattern) then
return true
end
else
return false
end
end
function set_timeout()
local time_out
if mp.get_property_native("cache-size") ~= nil then
time_out = mp.get_property_native("cache-secs")
else
time_out = mp.get_property_native("demuxer-readahead-secs")
end
return time_out
end
local aid
function restore_normalspeed()
mp.set_property("speed", normalspeed)
if mp.get_property_native("video-sync") == "desync" then
mp.set_property("video-sync", "audio")
end
if (aid~=nil and aid~=mp.get_property("aid")) then mp.set_property("aid", aid) end
end
function check_should_speedup()
local subdelay = mp.get_property_native("sub-delay")
mp.command("no-osd set sub-visibility no")
mp.command("no-osd sub-step 1")
local mark = mp.get_property_native("time-pos")
local nextsubdelay = mp.get_property_native("sub-delay")
local nextsub = subdelay - nextsubdelay
if ignorePattern and nextsub > 0 then
local lookNext = true
local ignore = shouldIgnore(mp.get_property("sub-text"))
while ignore and lookNext do
local delay1 = mp.get_property_native("sub-delay")
mp.command("no-osd sub-step 1")
local delay2 = mp.get_property_native("sub-delay")
ignore = shouldIgnore(mp.get_property("sub-text"))
if delay1 == delay2 then
lookNext = false
nextsub = 0
else
nextsub = subdelay - delay2
end
end
end
mp.set_property("sub-delay", subdelay)
mp.command("no-osd set sub-visibility yes")
return nextsub, nextsub >= lookahead or nextsub == 0, mark
end
function check_audio(_,ds)
if state==0 then
return
elseif ds and tonumber(ds)>leadin and mp.get_property("aid")~="no" then
aid = mp.get_property("aid")
mp.set_property("aid", "no")
print("avsync greater than leadin, dropping audio")
end
end
function check_position(_, position)
if position then
if nextsub ~= 0 and position >= (mark+nextsub-leadin) then
restore_normalspeed()
mp.unobserve_property(check_position)
mp.unobserve_property(check_audio)
elseif nextsub == 0 and position >= (mark+set_timeout()-leadin) then
nextsub, _ , mark = check_should_speedup()
end
end
end
function skipval()
local skipval = mp.get_property_native("demuxer-cache-duration", 0)
if nextsub > 0 then
if directskip then
skipval = nextsub - leadin
elseif nextsub - skipval - leadin <= 0 then
skipval = clamp(nextsub - leadin, 0, maxSkip)
else
skipval = clamp(skipval, 0, maxSkip)
end
elseif directskip then
skipval = clamp(skipval - leadin, 1, nil)
else
skipval = clamp(skipval - leadin, 1, maxSkip)
end
return skipval
end
firstskip = true --make the first skip in skip mode not have to wait for skipdelay
delayEnd = true
function speed_transition(_, sub)
if sub~=nil and shouldIgnore(sub) then
sub = ""
end;
if state == 0 then
if sub == "" then
last_speedup_zone_begin = speedup_zone_begin
nextsub, shouldspeedup, speedup_zone_begin = check_should_speedup()
mark = speedup_zone_begin
speedup_zone_end = mark + nextsub
if shouldspeedup or (skipmode and not firstskip) then
local temp_disable_skipmode = false
if last_speedup_zone_begin and mark < last_speedup_zone_begin then
temp_disable_skipmode = true
end
if skipmode and not temp_disable_skipmode and mp.get_property("pause") == "no" then
if firstskip or skipdelay == 0 then
mp.commandv("no-osd", "seek", skipval(), "relative", "exact")
firstskip = false
elseif delayEnd then
delayEnd = false
mp.add_timeout(skipdelay, function()
delayEnd = true
if mp.get_property("pause") == "no" then
nextsub, shouldskip = check_should_speedup()
if shouldskip or nextsub > leadin then
local tSkip = skipval()
currentSub = mp.get_property("sub-text")
if tSkip > minSkip and (currentSub == "" or shouldIgnore(currentSub)) then
mp.commandv("no-osd", "seek", tSkip, "relative", "exact")
else
firstskip = true
end
end
end
end)
end
else
normalspeed = mp.get_property("speed")
if mp.get_property_native("video-sync") == "audio" then
mp.set_property("video-sync", "desync")
end
mp.set_property("speed", speedup)
mp.observe_property("time-pos", "native", check_position)
state = 1
if dropOnAVdesync then
aid = mp.get_property("aid")
mp.observe_property("avsync", "native", check_audio)
end
end
else
firstskip = true
end
end
elseif state == 1 then
if (sub ~= "" and sub ~= nil) or not mp.get_property_native("sid") then
mp.unobserve_property(check_position)
mp.unobserve_property(check_audio)
restore_normalspeed()
state = 0
else
local pos = mp.get_property_native("time-pos", 0)
if pos < speedup_zone_begin or pos > speedup_zone_end then
nextsub, _ , mark = check_should_speedup()
end
end
end
end
toggle2 = false
function toggle_sub_visibility()
if not toggle2 then
sub_color = mp.get_property("sub-color", "1/1/1/1")
sub_color2 = mp.get_property("sub-border-color", "0/0/0/1")
sub_color3 = mp.get_property("sub-shadow-color", "0/0/0/1")
mp.set_property("sub-color", "0/0/0/0")
mp.set_property("sub-border-color", "0/0/0/0")
mp.set_property("sub-shadow-color", "0/0/0/0")
else
mp.set_property("sub-color", sub_color)
mp.set_property("sub-border-color", sub_color2)
mp.set_property("sub-shadow-color", sub_color3)
end
mp.osd_message("subtitle visibility: "..tostring(toggle2))
toggle2 = not toggle2
end
function toggle_skipmode()
skipmode = not skipmode
if enable then
mp.unobserve_property(speed_transition)
mp.unobserve_property(check_position)
mp.observe_property("sub-text", "native", speed_transition)
state = 0
end
if skipmode then
mp.osd_message("skip mode")
else
mp.osd_message("speed mode")
end
end
function clamp(v,l,u)
if l and v < l then
v = l
elseif u and v > u then
v = u
end
return v
end
function change_speedup(v)
speedup = speedup + v
mp.osd_message("speedup: "..speedup)
end
function change_leadin(v)
--leadin = clamp(leadin + v, 0, 2)
leadin = clamp(leadin + v, 0, nil)
mp.osd_message("leadin: "..leadin)
end
function change_lookAhead(v)
lookahead = clamp(lookahead + v , 0, nil)
mp.osd_message("lookahead: "..lookahead)
end
enable = false
state = 0
function toggle()
if not enable then
normalspeed = mp.get_property("speed")
mp.set_property("demuxer-readahead-secs",lookahead+leadin)
mp.observe_property("sub-text", "native", speed_transition)
mp.osd_message("speed-transition enabled")
else
restore_normalspeed()
mp.set_property("demuxer-readahead-secs",readahead_secs)
mp.unobserve_property(speed_transition)
mp.unobserve_property(check_position)
mp.osd_message("speed-transition disabled")
end
state = 0
enable = not enable
end
function reset_on_file_load()
if state == 1 then
mp.unobserve_property(check_position)
restore_normalspeed()
state = 0
end
end
mp.add_key_binding("ctrl+j", "toggle_speedtrans", toggle)
mp.add_key_binding("alt+j", "toggle_sub_visibility", toggle_sub_visibility)
mp.add_key_binding("ctrl+alt+j", "toggle_skipmode", toggle_skipmode)
mp.add_key_binding("alt++", "increase_speedup", function() change_speedup(0.1) end, {repeatable=true})
mp.add_key_binding("alt+-", "decrease_speedup", function() change_speedup(-0.1) end, {repeatable=true})
mp.add_key_binding("alt+0", "increase_leadin", function() change_leadin(0.25) end)
mp.add_key_binding("alt+9", "decrease_leadin", function() change_leadin(-0.25) end)
mp.add_key_binding("alt+8", "increase_lookahead", function() change_lookAhead(0.25) end)
mp.add_key_binding("alt+7", "decrease_lookahead", function() change_lookAhead(-0.25) end)
mp.register_event("file-loaded", reset_on_file_load)