mirror of
https://github.com/rydesun/dotfiles.git
synced 2025-12-26 14:44:58 +08:00
98 lines
2.8 KiB
Lua
98 lines
2.8 KiB
Lua
local mp = require 'mp'
|
|
local opt = require 'mp.options'
|
|
local utils = require 'mp.utils'
|
|
|
|
local user_opts = {
|
|
max_distance = 2, -- 文件名的最大差异
|
|
max_distance_ratio = 0.5, -- 文件名的最大差异占比
|
|
video_types = {
|
|
'3g2', ' 3gp', ' asf', ' avi', ' f4v', ' flv', ' h264', ' h265', ' m2ts', ' m4v', ' mkv', ' mov', ' mp4', ' mp4v',
|
|
' mpeg', ' mpg', ' ogm', ' ogv', ' rm', ' rmvb', ' ts', ' vob', ' webm', ' wmv', ' y4m' },
|
|
}
|
|
|
|
local script_name = mp.get_script_name()
|
|
opt.read_options(user_opts, script_name)
|
|
|
|
local function levenshtein_distance(s1, s2)
|
|
local t = {}
|
|
for i = 0, #s2 do
|
|
table.insert(t, i)
|
|
end
|
|
|
|
for i = 2, #s1+1 do
|
|
local t2 = {i-1}
|
|
for j = 2, #s2+1 do
|
|
local value
|
|
if t[j-1] <= t[j] and t[j-1] <= t2[j-1] then
|
|
local cost = s1:sub(i-1,i-1) ~= s2:sub(j-1,j-1) and 1 or 0
|
|
value = t[j-1] + cost
|
|
elseif t[j] < t2[j-1] then
|
|
value = t[j] + 1
|
|
else
|
|
value = t2[j-1] + 1
|
|
end
|
|
table.insert(t2, value)
|
|
end
|
|
t = t2
|
|
end
|
|
return t[#s2+1]
|
|
end
|
|
|
|
local function similar_files(dir, filename)
|
|
local o_basename, o_ext = filename:match("(.+)%.(.+)")
|
|
local files = utils.readdir(dir, "files")
|
|
local res, skipped = {}, 0
|
|
for _, file in pairs(files) do
|
|
if file == filename then
|
|
table.insert(res, file)
|
|
goto continue
|
|
end
|
|
local basename, ext = file:match("(.+)%.(.+)")
|
|
if not ext then goto continue end
|
|
ext = ext:lower()
|
|
if ext ~= o_ext then
|
|
goto continue
|
|
end
|
|
local distance = levenshtein_distance(basename, o_basename)
|
|
if distance <= user_opts.max_distance
|
|
or (distance/#o_basename <= user_opts.max_distance_ratio) then
|
|
table.insert(res, file)
|
|
else
|
|
skipped = skipped + 1
|
|
end
|
|
::continue::
|
|
end
|
|
return res, skipped
|
|
end
|
|
|
|
local function autoload_series()
|
|
if mp.get_property_number("playlist-count", 1) > 1 then
|
|
return
|
|
end
|
|
|
|
local filepath = mp.get_property("path", "")
|
|
if filepath == nil or filepath:find("^http[s]?://") then
|
|
return
|
|
end
|
|
local dir, filename = utils.split_path(filepath)
|
|
if #dir == 0 then
|
|
return
|
|
end
|
|
|
|
local files, skipped = similar_files(dir, filename)
|
|
table.sort(files, function(s1, s2)
|
|
if #s1 ~= #s2 then return #s1 < #s2 else return s1 < s2 end
|
|
end)
|
|
for i = 1, #files do
|
|
if files[i] ~= filename then
|
|
mp.commandv("loadfile", dir..'/'..files[i], "append")
|
|
else
|
|
mp.commandv("playlist-move", 0, i)
|
|
end
|
|
end
|
|
if skipped > 0 then
|
|
mp.osd_message("skipped: ".. skipped, 3)
|
|
end
|
|
end
|
|
|
|
mp.register_event("start-file", autoload_series)
|