-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathsponsorblock_minimal.lua
More file actions
149 lines (129 loc) · 4.85 KB
/
sponsorblock_minimal.lua
File metadata and controls
149 lines (129 loc) · 4.85 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
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
-- sponsorblock_minimal.lua v 0.5.1
--
-- This script skip/mute sponsored segments of YouTube and bilibili videos
-- using data from https://github.com/ajayyy/SponsorBlock
-- and https://github.com/hanydd/BilibiliSponsorBlock
local opt = require 'mp.options'
local utils = require 'mp.utils'
local options = {
youtube_sponsor_server = "https://sponsor.ajay.app/api/skipSegments",
bilibili_sponsor_server = "https://bsbsb.top/api/skipSegments",
-- Categories to fetch
-- Perform skip/mute/mark chapter based on the 'actionType' returned
categories = '"sponsor"',
}
opt.read_options(options)
local ranges = nil
local video_id = nil
local sponsor_server = nil
local cache = {}
local mute = false
local ON = false
local function getranges(url)
local res = mp.command_native{
name = "subprocess",
capture_stdout = true,
playback_only = false,
args = {
"curl", "-L", "-s", "-g",
"-H", "origin: mpv-script/sponsorblock_minimal",
"-H", "x-ext-version: 0.5.1",
url
}
}
if res.status ~= 0 then
return nil
end
return utils.parse_json(res.stdout)
end
local function make_chapter(ranges)
local chapters_time = {}
local chapters_title = {}
local chapter_index = 0
local all_chapters = mp.get_property_native("chapter-list")
for _, v in pairs(ranges) do
table.insert(chapters_time, v.segment[1])
table.insert(chapters_title, v.category)
table.insert(chapters_time, v.segment[2])
table.insert(chapters_title, "normal")
end
for i = 1, #chapters_time do
chapter_index = chapter_index + 1
all_chapters[chapter_index] = {
title = chapters_title[i] or ("Chapter " .. string.format("%02.f", chapter_index)),
time = chapters_time[i]
}
end
table.sort(all_chapters, function(a, b) return a['time'] < b['time'] end)
mp.set_property_native("chapter-list", all_chapters)
end
local function skip_ads(_, pos)
if pos ~= nil and ranges ~= nil then
for _, v in pairs(ranges) do
if v.actionType == "skip" and v.segment[1] <= pos and v.segment[2] > pos then
--this message may sometimes be wrong
--it only seems to be a visual thing though
local time = math.floor(v.segment[2] - pos)
mp.osd_message(string.format("[sponsorblock] skipping forward %ds", time))
--need to do the +0.01 otherwise mpv will start spamming skip sometimes
mp.set_property("time-pos", v.segment[2] + 0.01)
elseif v.actionType == "mute" then
if v.segment[1] <= pos and v.segment[2] >= pos then
cache[v.segment[2]] = nil
mp.set_property_bool("mute", true)
elseif pos > v.segment[2] and not cache[v.segment[2]] and mute ~= false then
cache[v.segment[2]] = true
mp.set_property_bool("mute", false)
end
end
end
end
end
local function file_loaded()
cache = {}
local video_path = mp.get_property("path", "")
local video_referer = mp.get_property("http-header-fields", ""):match("[Rr]eferer:%s*([^,\r\n]+)") or ""
local purl = mp.get_property("metadata/by-key/PURL", "")
local bilibili = video_path:match("bilibili.com/video") or video_referer:match("bilibili.com/video") or false
mute = mp.get_property_bool("mute")
local urls = {
"ytdl://youtu%.be/([%w-_]+).*",
"ytdl://w?w?w?%.?youtube%.com/v/([%w-_]+).*",
"ytdl://w?w?w?%.?bilibili%.com/video/([%w-_]+).*",
"https?://youtu%.be/([%w-_]+).*",
"https?://w?w?w?%.?youtube%.com/v/([%w-_]+).*",
"https?://w?w?w?%.?bilibili%.com/video/([%w-_]+).*",
"/watch.*[?&]v=([%w-_]+).*",
"/embed/([%w-_]+).*",
"^ytdl://([%w-_]+)$",
"-([%w-_]+)%."
}
for _, url in ipairs(urls) do
video_id = video_id or video_path:match(url) or video_referer:match(url) or purl:match(url)
end
if not video_id or string.len(video_id) < 11 then return end
if bilibili then
sponsor_server = options.bilibili_sponsor_server
video_id = string.sub(video_id, 1, 12)
else
sponsor_server = options.youtube_sponsor_server
video_id = string.sub(video_id, 1, 11)
end
local url = ("%s?videoID=%s&categories=[%s]"):format(sponsor_server, video_id, options.categories)
ranges = getranges(url)
if ranges ~= nil then
make_chapter(ranges)
ON = true
mp.observe_property("time-pos", "native", skip_ads)
end
end
local function end_file()
if not ON then return end
mp.unobserve_property(skip_ads)
video_id = nil
cache = nil
ranges = nil
ON = false
end
mp.register_event("file-loaded", file_loaded)
mp.register_event("end-file", end_file)