-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadsub.lua
More file actions
65 lines (51 loc) · 1.54 KB
/
loadsub.lua
File metadata and controls
65 lines (51 loc) · 1.54 KB
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
local options = {
sub_folder = {"sub"},
sub_ext = {"srt", "ass"}
}
mp.options = require "mp.options"
mp.options.read_options(options, "loadsub")
mp.msg = require "mp.msg"
mp.utils = require "mp.utils"
local function get_season_episode(filename)
local season, episode = string.match(filename, "[sS]0*(%d+)%D*[eE]0*(%d+)")
return season, episode
end
local function is_sub_ext(ext)
if not ext then
return false
end
for _, target_sub in ipairs(options.sub_ext) do
if ext == target_sub then
return true
end
end
end
local function load_dir_sub(dir, key)
local files = mp.utils.readdir(dir, "files")
if not files then
return
end
for _, file in ipairs(files) do
local ext = string.match(file, key)
if is_sub_ext(ext) then
local full_path = mp.utils.join_path(dir, file)
mp.msg.info(full_path)
mp.commandv("sub-add", full_path, "cached")
end
end
end
local function loadsub()
local path = mp.get_property("path")
local dir, filename = mp.utils.split_path(path)
local season, episode = get_season_episode(filename)
if not (season and episode) then
return
end
local new_match = "[sS]0*"..season.."%D*[eE]0*"..episode.."%D.*%.(%w+)$"
load_dir_sub(dir, new_match)
for _, sub_folder in ipairs(options.sub_folder) do
local sub_folder = mp.utils.join_path(dir, sub_folder)
load_dir_sub(sub_folder, new_match)
end
end
mp.register_event("file-loaded", loadsub)