-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathauto_mode.lua
230 lines (177 loc) · 6.37 KB
/
auto_mode.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
--[[
https://github.com/stax76/mpv-scripts
This script changes options depending on what type of
file is played. It uses the file extension to detect
if the current file is a video, audio or image file.
The changes happen not on every file load, but only
when a mode change is detected.
On mode change 3 things can be done:
1. Change options
2. Change key bindings
3. Send messages
The configuration is done in code.
]]--
----- start config
-- video mode
function on_video_mode_activate()
msg.info("Video mode is activated")
mp.set_property("osd-playing-msg", "${media-title}") -- in video mode use media-title
mp.command("script-message osc-visibility auto no_osd") -- set osc visibility to auto
end
function on_video_mode_deactivate()
end
-- audio mode
function on_audio_mode_activate()
msg.info("Audio mode is activated")
mp.set_property("osd-playing-msg", "${media-title}") -- in audio mode use media-title
mp.command("script-message osc-visibility never no_osd") -- in audio mode disable the osc
end
function on_audio_mode_deactivate()
end
-- image mode
function on_image_mode_activate()
msg.info("Image mode is activated")
mp.set_property("osd-playing-msg", "") -- disable osd-playing-msg for images
mp.set_property("background-color", "#1A2226") -- use dark grey background for images
mp.command("script-message osc-visibility never no_osd") -- disable osc for images
end
function on_image_mode_deactivate()
mp.set_property("background-color", "#000000") -- use black background for audio and video
end
-- called whenever the file extension changes
function on_type_change(old_ext, new_ext)
if new_ext == "gif" then
mp.set_property("loop-file", "inf") -- loop GIF files
end
if old_ext == "gif" then
mp.set_property("loop-file", "no") -- use loop-file=no for anything except GIF
end
end
-- binding configuration
audio_mode_bindings = {
{ "Left", function () mp.command("no-osd seek -10") end, "repeatable" }, -- make audio mode seek length longer than video mode seek length
{ "Right", function () mp.command("no-osd seek 10") end, "repeatable" }, -- make audio mode seek length longer than video mode seek length
}
image_mode_bindings = {
{ "UP", function () mp.command("no-osd add video-pan-y -0.02") end, "repeatable" }, -- move image up
{ "DOWN", function () mp.command("no-osd add video-pan-y 0.02") end, "repeatable" }, -- move image down
{ "LEFT", function () mp.command("playlist-prev") end, "repeatable" }, -- show previous image
{ "RIGHT", function () mp.command("playlist-next") end, "repeatable" }, -- show next image
{ "SPACE", function () mp.command("playlist-next") end, "repeatable" }, -- show next image
{ "WHEEL_UP", function () mp.command("add video-zoom 0.1") end, "repeatable" }, -- increase image size
{ "WHEEL_DOWN", function () mp.command("add video-zoom -0.1") end, "repeatable" }, -- decrease image size
{ "BS", function () mp.command("no-osd set video-pan-y 0; no-osd set video-zoom 0") end }, -- reset image options
}
----- end config
----- string
function ends_with(value, ending)
return ending == "" or value:sub(-#ending) == ending
end
function split(input, sep)
assert(#sep == 1) -- supports only single character separator
local tbl = {}
if input ~= nil then
for str in string.gmatch(input, "([^" .. sep .. "]+)") do
table.insert(tbl, str)
end
end
return tbl
end
----- path
function get_file_ext_short(path)
if path == nil then return nil end
local val = path:match("^.+%.([^%./\\]+)$")
if val == nil then return nil end
return val:lower()
end
----- list
function list_contains(list, value)
for _, v in pairs(list) do
if v == value then
return true
end
end
return false
end
----- key bindings
function add_bindings(definition)
if type(active_bindings) ~= "table" then
active_bindings = {}
end
local script_name = mp.get_script_name()
for _, bind in ipairs(definition) do
local name = script_name .. "_key_" .. (#active_bindings + 1)
active_bindings[#active_bindings + 1] = name
mp.add_forced_key_binding(bind[1], name, bind[2], bind[3])
end
end
function remove_bindings()
if type(active_bindings) == "table" then
for _, name in ipairs(active_bindings) do
mp.remove_key_binding(name)
end
end
end
----- main
local msg = require "mp.msg"
active_mode = "video"
last_type = nil
function enable_video_mode()
if active_mode == "video" then return end
active_mode = "video"
remove_bindings()
on_video_mode_activate()
end
function enable_audio_mode()
if active_mode == "audio" then return end
active_mode = "audio"
remove_bindings()
add_bindings(audio_mode_bindings)
on_audio_mode_activate()
end
function enable_image_mode()
if active_mode == "image" then return end
active_mode = "image"
remove_bindings()
add_bindings(image_mode_bindings)
on_image_mode_activate()
end
function disable_video_mode()
if active_mode ~= "video" then return end
active_mode = ""
remove_bindings()
on_video_mode_deactivate()
end
function disable_image_mode()
if active_mode ~= "image" then return end
active_mode = ""
remove_bindings()
on_image_mode_deactivate()
end
function disable_audio_mode()
if active_mode ~= "audio" then return end
active_mode = ""
remove_bindings()
on_audio_mode_deactivate()
end
function on_start_file(event)
local short_ext = get_file_ext_short(mp.get_property("path"))
if list_contains(split(mp.get_property("image-exts"), ","), short_ext) then
disable_video_mode()
disable_audio_mode()
enable_image_mode()
elseif list_contains(split(mp.get_property("audio-exts"), ","), short_ext) then
disable_image_mode()
disable_video_mode()
enable_audio_mode()
else
disable_audio_mode()
disable_image_mode()
enable_video_mode()
end
if last_type ~= short_ext then
on_type_change(last_type, short_ext)
last_type = short_ext
end
end
mp.register_event("start-file", on_start_file)