-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.lua
118 lines (96 loc) · 2.77 KB
/
main.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
--[[ fastforward.lua
# Examples
* `script-opts/fastforward.lua`:
```config
max_speed=3
speed_increase=*1.2
speed_decrease=/1.1
```
* `input.conf`:
```config
# make playback faster
) script-binding fastforward/speedup
# reduce speed
( script-binding fastforward/slowdown
# Pro tip: Use `BACKSPACE` to stop fast-forwarding immediately.
```
]]
local options = require 'mp.options'
--[[
This default config makes absolutely no sense, so use `script-opts/fastforward.conf` to change these values.
You can also use expressions like [+-*/]{number}, e.g. /2, *0.5, +1, -0.3...
]]
local opts = {
speed_increase = '+0.2', -- <-- here...
-- Upper speed limit.
max_speed = 8,
-- Time to elapse until first slow down.
decay_delay = 2,
-- If you don't change the playback speed for `decay_delay` seconds,
-- it will be decreased by `speed_decrease` at every
-- `decay_interval` seconds automatically.
decay_interval = 0.5,
speed_decrease = '*0.9', -- <-- ...and here.
}
options.read_options(opts)
local timer = nil
local was_paused
local function pause_changed(_, paused)
if paused then
timer:stop()
else
timer:resume()
end
end
local function speed_changed(_, speed)
if speed <= 1.001 then
mp.unobserve_property(pause_changed)
mp.unobserve_property(speed_changed)
timer:kill()
timer = nil
mp.remove_key_binding('slowdown')
mp.set_property_bool('pause', was_paused)
speed = 1
elseif speed > opts.max_speed then
speed = opts.max_speed
end
mp.set_property_number('speed', speed)
if speed == 1 then
mp.osd_message('')
else
mp.osd_message(('▶▶ x%.2f'):format(speed), 10)
end
end
local function change_speed(amount)
local new_speed = mp.get_property_number('speed')
local op = amount:sub(1, 1)
local val = tonumber(amount:sub(2))
if op == '+' then new_speed = new_speed + val
elseif op == '-' then new_speed = new_speed - val
elseif op == '*' then new_speed = new_speed * val
elseif op == '/' then new_speed = new_speed / val
else new_speed = tonumber(amount)
end
mp.set_property_number('speed', new_speed)
end
local function slow_down()
change_speed(opts.speed_decrease)
end
local function auto_slow_down()
slow_down()
timer = mp.add_periodic_timer(opts.decay_interval, slow_down)
end
local function speed_up()
change_speed(opts.speed_increase)
if timer == nil then
mp.observe_property('pause', 'bool', pause_changed)
mp.observe_property('speed', 'number', speed_changed)
mp.add_key_binding('(', 'slowdown', slow_down, {repeatable=true})
was_paused = mp.get_property_bool('pause')
mp.set_property_bool('pause', false)
else
timer:kill()
end
timer = mp.add_timeout(opts.decay_delay, auto_slow_down)
end
mp.add_key_binding(')', 'speedup', speed_up, {repeatable=true})