Compare commits

...

10 Commits

Author SHA1 Message Date
Jouni
ca2844b8cf fix bad request 2023-08-20 19:12:45 +03:00
Jouni
01e05c633d Remove lua-curl support
I can no longer get lua-curl to work on my machine and there hasn't been
a commit in a couple years.
2023-08-20 17:46:39 +03:00
Jouni
27dd1df461 add ' 2023-05-04 16:39:57 +03:00
Jouni
7999a903ff fix typo 2023-05-04 13:36:07 +03:00
Jouni
9e4868db09 add some docs 2023-05-04 13:28:06 +03:00
Jouni
bbebda3a46 Small tweaks
- Make more variables local
- Remove unnecessary return
- Remove unnecessary ~= nil
- Make sure parse_json returns a table
2023-04-02 11:26:08 +03:00
Jouni
b8c1a2a878 add the option to use sha256HashPrefix 2022-09-16 18:26:43 +03:00
Jouni
7e86056de9 add quotes 2022-06-13 20:48:02 +03:00
Jouni
7150bea48c change word in README 2022-06-13 20:16:31 +03:00
Jouni
18b4939805 read script-opts + set less aggressive default 2022-06-13 20:15:47 +03:00
2 changed files with 89 additions and 63 deletions

27
README
View File

@ -7,10 +7,35 @@ This is a much more simple version of the sponsorblock mpv plugin.
There are no other functions in this other than the sponsor skipping. Also this
uses curl rather than python to get the ranges. There is also no cache so the
ranges may get redownloaded if you watch a video more than once.
ranges will get redownloaded if you watch a video more than once.
b toggles between on/off
This script has a few script-opts you can set:
sponsorblock_minimal-server
This sets the url, where the script gets the segments from. Default is
"https://sponsor.ajay.app/api/skipSegments"
sponsorblock_minimal-categories
This sets the segment categories that are skipped. By default only sponsor
segments are skipped. The format is a little tricky but lets say that you want
to skip sponsor and intro segments then you could do this:
--script-opts=sponsorblock_minimal-categories='["sponsor","intro"]'
You can find a list of all the categories here:
https://wiki.sponsor.ajay.app/w/Types
sponsorblock_minimal-hash
This option makes it so that the video id will not be sent to the sponsorblock
server, but a part of sha256 hash will be sent instead. You will need the
sha256sum program on your system to use this feature. Set to "true" to enable
this.
Link to the original mpv sponsorblock plugin:
https://github.com/po5/mpv_sponsorblock

View File

@ -3,63 +3,29 @@
-- This script skips sponsored segments of YouTube videos
-- using data from https://github.com/ajayyy/SponsorBlock
local opt = require 'mp.options'
local utils = require 'mp.utils'
local ON = false
local ranges = nil
local options = {
server = "https://sponsor.ajay.app/api/skipSegments",
-- Categories to fetch and skip
categories = '"sponsor","intro","outro","interaction","selfpromo"'
categories = '"sponsor"',
-- Set this to "true" to use sha256HashPrefix instead of videoID
hash = ""
}
function getranges()
local luacurl_available, cURL = pcall(require,'cURL')
local cstr = ("categories=[%s]"):format(options.categories)
local vstr = ("videoID=%s"):format(youtube_id)
if not(luacurl_available) then -- if Lua-cURL is not available on this system
local curl_cmd = {
"curl",
"-L",
"-s",
"-G",
"-d", cstr,
"-d", vstr,
options.server
}
local sponsors = mp.command_native{
name = "subprocess",
capture_stdout = true,
playback_only = false,
args = curl_cmd
}
res = sponsors.stdout
else -- otherwise use Lua-cURL (binding to libcurl)
local API = ("%s?%s&%s"):format(options.server, cstr, vstr)
local buf={}
local c = cURL.easy_init()
c:setopt_followlocation(1)
c:setopt_url(API)
c:setopt_writefunction(function(chunk) table.insert(buf,chunk); return true; end)
c:perform()
res = table.concat(buf)
end
if string.match(res,"%[(.-)%]") then
ranges = {}
for i in string.gmatch(string.sub(res,2,-2),"%[(.-)%]") do
k,v = string.match(i,"(%d+.?%d*),(%d+.?%d*)")
ranges[k] = v
end
end
return
end
opt.read_options(options)
function skip_ads(name,pos)
if pos ~= nil then
for k,v in pairs(ranges) do
k = tonumber(k)
v = tonumber(v)
if k <= pos and v > pos then
if pos then
for _, i in pairs(ranges) do
v = i.segment[2]
if i.segment[1] <= pos and v > pos then
--this message may sometimes be wrong
--it only seems to be a visual thing though
mp.osd_message(("[sponsorblock] skipping forward %ds"):format(math.floor(v-mp.get_property("time-pos"))))
@ -70,7 +36,6 @@ function skip_ads(name,pos)
end
end
end
return
end
function file_loaded()
@ -87,22 +52,59 @@ function file_loaded()
"^ytdl://([%w-_]+)$",
"-([%w-_]+)%."
}
youtube_id = nil
local youtube_id = nil
local purl = mp.get_property("metadata/by-key/PURL", "")
for i,url in ipairs(urls) do
youtube_id = youtube_id or string.match(video_path, url) or string.match(video_referer, url) or string.match(purl, url)
if youtube_id then break end
end
if not youtube_id or string.len(youtube_id) < 11 then return end
youtube_id = string.sub(youtube_id, 1, 11)
getranges()
if ranges then
ON = true
mp.add_key_binding("b","sponsorblock",toggle)
mp.observe_property("time-pos", "native", skip_ads)
local args = {"curl", "-L", "-s", "-G", "--data-urlencode", ("categories=[%s]"):format(options.categories)}
local url = options.server
if options.hash == "true" then
local sha = mp.command_native{
name = "subprocess",
capture_stdout = true,
args = {"sha256sum"},
stdin_data = youtube_id
}
url = ("%s/%s"):format(url, string.sub(sha.stdout, 0, 4))
else
table.insert(args, "--data-urlencode")
table.insert(args, "videoID=" .. youtube_id)
end
table.insert(args, url)
local sponsors = mp.command_native{
name = "subprocess",
capture_stdout = true,
playback_only = false,
args = args
}
if sponsors.stdout then
local json = utils.parse_json(sponsors.stdout)
if type(json) == "table" then
if options.hash == "true" then
for _, i in pairs(json) do
if i.videoID == youtube_id then
ranges = i.segments
break
end
end
else
ranges = json
end
if ranges then
ON = true
mp.add_key_binding("b","sponsorblock",toggle)
mp.observe_property("time-pos", "native", skip_ads)
end
end
end
return
end
function end_file()
@ -117,12 +119,11 @@ function toggle()
mp.unobserve_property(skip_ads)
mp.osd_message("[sponsorblock] off")
ON = false
return
else
mp.observe_property("time-pos", "native", skip_ads)
mp.osd_message("[sponsorblock] on")
ON = true
end
mp.observe_property("time-pos", "native", skip_ads)
mp.osd_message("[sponsorblock] on")
ON = true
return
end
mp.register_event("file-loaded", file_loaded)