-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaverage_volume.lua
288 lines (217 loc) · 6 KB
/
average_volume.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
286
287
288
--[[
https://github.com/stax76/mpv-scripts
This script records the volume per song in order to restore
it in future sessions.
What is recorded and restored is the volume offset relative
to the session average volume.
For every song the last ten sessions are recorded,
the average of that is used.
It gives much better results than replay gain.
Configuration: ~~\script-opts\average_volume.conf
monitored_directories=<directories seperated with a semicolon>
#storage_path=~~/average_volume.json
Files not updated or orphaned get removed
automatically after 500 days.
]]--
----- options
local o = {
monitored_directories = "",
storage_path = "~~/average_volume.json",
}
----- math
function round(value)
return value >= 0 and math.floor(value + 0.5) or math.ceil(value - 0.5)
end
----- string
function is_empty(input)
if input == nil or input == "" then
return true
end
end
function contains(value, find)
return value:find(find, 1, true)
end
function replace(str, what, with)
what = string.gsub(what, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1")
with = string.gsub(with, "[%%]", "%%%%")
return string.gsub(str, what, with)
end
function starts_with(str, start)
return str:sub(1, #start) == start
end
function split(value, sep)
local t = {}
for str in string.gmatch(value, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
----- path
function normalize_path(path)
if path == nil then
return ""
end
local is_windows = package.config:sub(1,1) == "\\"
if is_windows and ((contains(path, ":/") and not contains(path, "://")) or
(contains(path, ":\\") and contains(path, "/"))) then
path = replace(path, "/", "\\")
end
return path
end
----- list
function list_contains(list, value)
for _, v in pairs(list) do
if v == value then
return true
end
end
return false
end
----- table
function table_count(t)
local count = 0
for _ in pairs(t) do
count = count + 1
end
return count
end
----- file
function file_exists(name)
local file = io.open(name, "r")
if file ~= nil then
io.close(file)
return true
else
return false
end
end
function file_read(file_path)
local file = assert(io.open(file_path, "r"))
local content = file:read("*all")
file:close()
return content
end
function file_write(file_path, content)
local file = assert(io.open(file_path, "w"))
file:write(content)
file:close()
end
----- average_volume
local utils = require "mp.utils"
local msg = require "mp.msg"
local previous_name = nil
local data = nil
local session_data = {}
local opt = require "mp.options"
opt.read_options(o)
o.storage_path = mp.command_native({"expand-path", o.storage_path})
if is_empty(o.monitored_directories) then
msg.warn("No directory to be monitored found.")
return
end
local monitored_directories = split(o.monitored_directories, ";")
function get_filename(path)
local _, filename = utils.split_path(path)
return filename
end
function get_average(array)
local sum = 0
local count = 0
for _, v in pairs(array) do
sum = sum + v
count = count + 1
end
if count > 0 then
return round(sum / count)
else
return 0
end
end
function get_item(name)
for _, v in pairs(data) do
if name == v.name then
return v
end
end
end
mp.register_event("start-file", function (event)
local file = normalize_path(mp.get_property("path"))
local found = false
for _, dir in pairs(monitored_directories) do
dir = normalize_path(dir)
if starts_with(file, dir) then
found = true
break
end
end
if not found then
return
end
if data == nil then
if file_exists(o.storage_path) then
data = utils.parse_json(file_read(o.storage_path))
else
data = {}
end
end
local volume = mp.get_property_number("volume")
if volume == 0 and previous_name ~= nil then
for _, v in pairs(data) do
if v.name == previous_name then
v.volumes = { 0 }
break
end
end
end
if volume ~= 0 and previous_name ~= nil then
session_data[previous_name] = volume
end
previous_name = get_filename(file)
if starts_with(previous_name, "00 - ") then
previous_name = string.sub(previous_name, 6)
end
local session_average = get_average(session_data)
if session_average == 0 then
session_average = volume
end
if session_data[previous_name] ~= nil then
mp.set_property_number("volume", session_data[previous_name])
elseif get_item(previous_name) ~= nil then
local item = get_item(previous_name)
if item ~= nil and #session_data > 1 then
mp.set_property_number("volume", session_average + get_average(item.volumes))
end
else
mp.set_property_number("volume", session_average)
end
end)
mp.register_event("shutdown", function ()
if table_count(session_data) < 3 then
return
end
local session_average = get_average(session_data)
for name, volume in pairs(session_data) do
local item = get_item(name)
if item == nil then
item = {}
item.name = name
item.date = os.time()
item.volumes = { volume - session_average }
table.insert(data, item)
else
while #item.volumes > 9 do
table.remove(item.volumes, 1)
end
table.insert(item.volumes, volume - session_average)
item.date = os.time()
end
end
for i=#data,1,-1 do
local item = data[i]
local days = (os.time() - item.date) / 60 / 24
if days > 500 then
table.remove(data, i)
end
end
file_write(o.storage_path, utils.format_json(data))
end)