From 2a029391f3b26e006cef0f8dc25a42c8a246cbf6 Mon Sep 17 00:00:00 2001 From: rydesun Date: Thu, 8 Jul 2021 22:48:31 +0800 Subject: [PATCH] feat(mpv): auto add similar files to playlist --- .config/mpv/scripts/autoload_series.lua | 79 +++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .config/mpv/scripts/autoload_series.lua diff --git a/.config/mpv/scripts/autoload_series.lua b/.config/mpv/scripts/autoload_series.lua new file mode 100644 index 0000000..3e6d44f --- /dev/null +++ b/.config/mpv/scripts/autoload_series.lua @@ -0,0 +1,79 @@ +local mp = require 'mp' +local utils = require 'mp.utils' + +local function levenshtein_distance(s1, s2) + local t = {} + for i = 0, #s1 do + local arr = {} + for j = 0, #s2 do + local value = 0 + if i == 0 then + value = j + elseif j == 0 then + value = i + end + table.insert(arr, value) + end + table.insert(t, arr) + end + + for i = 2, #s1+1 do + for j = 2, #s2+1 do + local cost = 0 + if s1:sub(i-1,i-1) ~= s2:sub(j-1,j-1) then + cost = 1 + end + t[i][j] = math.min( + t[i-1][j] + 1, + t[i][j-1] + 1, + t[i-1][j-1] + cost) + end + end + return t[#s1+1][#s2+1] +end + +local function similar_files(dir, filename) + local o_basename, o_ext = filename:match("(.+)%.(.+)") + local files = utils.readdir(dir, "files") + local res = {} + for _, file in pairs(files) do + if file == filename then + table.insert(res, file) + else + local basename, ext = file:match("(.+)%.(.+)") + if ext ~= o_ext then + goto continue + end + local distance = levenshtein_distance(basename, o_basename) + if distance <= 2 or (distance/#o_basename <= 0.5) then + table.insert(res, file) + end + end + ::continue:: + end + return res +end + +local function autoload_series() + if mp.get_property_number("playlist-count", 1) > 1 then + return + end + + local filepath = mp.get_property("path", "") + local dir, filename = utils.split_path(filepath) + if #dir == 0 then + return + end + + local files = similar_files(dir, filename) + table.sort(files) + 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 +end + +mp.register_event("start-file", autoload_series)