forked from Ajatt-Tools/mpvacious
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg_mgr.lua
169 lines (147 loc) · 5.01 KB
/
cfg_mgr.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
--[[
Copyright: Ren Tatsumoto and contributors
License: GNU GPL, version 3 or later; http:https://www.gnu.org/licenses/gpl.html
Config management, validation, loading.
]]
local mpopt = require('mp.options')
local msg = require('mp.msg')
local h = require('helpers')
local min_side_px = 42
local max_side_px = 640
local self = {
config = nil,
profiles = nil,
initial_config = {}
}
local default_profile_filename = 'subs2srs'
local profiles_filename = 'subs2srs_profiles'
local function set_audio_format()
if self.config.audio_format == 'opus' then
self.config.audio_codec = 'libopus'
self.config.audio_extension = '.ogg'
else
self.config.audio_codec = 'libmp3lame'
self.config.audio_extension = '.mp3'
end
end
local function set_video_format()
if self.config.snapshot_format == 'webp' then
self.config.snapshot_extension = '.webp'
self.config.snapshot_codec = 'libwebp'
else
self.config.snapshot_extension = '.jpg'
self.config.snapshot_codec = 'mjpeg'
end
-- Animated webp images can only have .webp extension.
-- The user has no choice on this.
self.config.animated_snapshot_extension = '.webp'
end
local function ensure_in_range(dimension)
self.config[dimension] = self.config[dimension] < min_side_px and -2 or self.config[dimension]
self.config[dimension] = self.config[dimension] > max_side_px and max_side_px or self.config[dimension]
end
local function conditionally_set_defaults(width, height, quality)
if self.config[width] < 1 and self.config[height] < 1 then
self.config[width] = -2
self.config[height] = 200
end
if self.config[quality] < 0 or self.config[quality] > 100 then
self.config[quality] = 15
end
end
local function check_image_settings()
ensure_in_range('snapshot_width')
ensure_in_range('snapshot_height')
conditionally_set_defaults('snapshot_width', 'snapshot_height', 'snapshot_quality')
end
local function ensure_correct_fps()
if self.config.animated_snapshot_fps == nil or self.config.animated_snapshot_fps <= 0 or self.config.animated_snapshot_fps > 30 then
self.config.animated_snapshot_fps = 10
end
end
local function check_animated_snapshot_settings()
ensure_in_range('animated_snapshot_width')
ensure_in_range('animated_snapshot_height')
conditionally_set_defaults('animated_snapshot_width', 'animated_snapshot_height', 'animated_snapshot_quality')
ensure_correct_fps()
end
local function validate_config()
set_audio_format()
set_video_format()
check_image_settings()
check_animated_snapshot_settings()
end
local function remember_initial_config()
if h.is_empty(self.initial_config) then
for key, value in pairs(self.config) do
self.initial_config[key] = value
end
else
msg.fatal("Ignoring. Initial config has been read already.")
end
end
local function restore_initial_config()
for key, value in pairs(self.initial_config) do
self.config[key] = value
end
end
local function read_profile_list()
mpopt.read_options(self.profiles, profiles_filename)
msg.info("Read profile list. Defined profiles: " .. self.profiles.profiles)
end
local function read_profile(profile_name)
mpopt.read_options(self.config, profile_name)
msg.info("Read config file: " .. profile_name)
end
local function read_default_config()
read_profile(default_profile_filename)
end
local function reload_from_disk()
--- Loads default config file (subs2srs.conf), then overwrites it with current profile.
if not h.is_empty(self.config) and not h.is_empty(self.profiles) then
restore_initial_config()
read_default_config()
if self.profiles.active ~= default_profile_filename then
read_profile(self.profiles.active)
end
validate_config()
else
msg.fatal("Attempt to load config when init hasn't been done.")
end
end
local function next_profile()
local first, next, new
for profile in string.gmatch(self.profiles.profiles, '[^,]+') do
if not first then
first = profile
end
if profile == self.profiles.active then
next = true
elseif next then
next = false
new = profile
end
end
if next == true or not new then
new = first
end
self.profiles.active = new
reload_from_disk()
end
local function init(config_table, profiles_table)
self.config, self.profiles = config_table, profiles_table
-- 'subs2srs' is the main profile, it is always loaded. 'active profile' overrides it afterwards.
-- initial state is saved to another table to maintain consistency when cycling through incomplete profiles.
read_profile_list()
read_default_config()
remember_initial_config()
if self.profiles.active ~= default_profile_filename then
read_profile(self.profiles.active)
end
validate_config()
end
return {
reload_from_disk = reload_from_disk,
init = init,
next_profile = next_profile,
}