forked from po5/chapterskip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chapterskip.lua
91 lines (83 loc) · 2.81 KB
/
chapterskip.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
-- chapterskip.lua
--
-- Ain't Nobody Got Time for That
--
-- This script skips chapters based on their title.
local categories = {
prologue = "^[Pp]rologue/^[Ii]ntro",
opening = "^OP/ OP$/^[Oo]pening/[Oo]pening$",
ending = "^ED/ ED$/^[Ee]nding/[Ee]nding$",
credits = "^[Cc]redits/[Cc]redits$",
preview = "[Pp]review$"
}
local options = {
enabled = false,
skip_once = true,
categories = "",
skip = ""
}
mp.options = require "mp.options"
function matches(i, title)
for category in string.gmatch(options.skip, " *([^;]*[^; ]) *") do
if categories[category:lower()] then
if string.find(category:lower(), "^idx%-") == nil then
if title then
for pattern in string.gmatch(categories[category:lower()], "([^/]+)") do
if string.match(title, pattern) then
return true
end
end
end
else
for pattern in string.gmatch(categories[category:lower()], "([^/]+)") do
if tonumber(pattern) == i then
return true
end
end
end
end
end
end
local skipped = {}
local parsed = {}
local function toggle_chapterskip()
options.enabled = not options.enabled
end
function chapterskip(_, current)
mp.options.read_options(options, "chapterskip")
if not options.enabled then return end
for category in string.gmatch(options.categories, "([^;]+)") do
name, patterns = string.match(category, " *([^+>]*[^+> ]) *[+>](.*)")
if name then
categories[name:lower()] = patterns
elseif not parsed[category] then
mp.msg.warn("Improper category definition: " .. category)
end
parsed[category] = true
end
local chapters = mp.get_property_native("chapter-list")
local skip = false
for i, chapter in ipairs(chapters) do
if (not options.skip_once or not skipped[i]) and matches(i, chapter.title) then
if i == current + 1 or skip == i - 1 then
if skip then
skipped[skip] = true
end
skip = i
end
elseif skip then
mp.set_property("time-pos", chapter.time)
skipped[skip] = true
return
end
end
if skip then
if mp.get_property_native("playlist-count") == mp.get_property_native("playlist-pos-1") then
return mp.set_property("time-pos", mp.get_property_native("duration"))
end
mp.commandv("playlist-next")
end
end
mp.observe_property("chapter", "number", chapterskip)
mp.register_event("file-loaded", function() skipped = {} end)
mp.register_script_message("chapter-skip", toggle_chapterskip)