This repository has been archived on 2021-08-25. You can view files and clone it, but cannot push or open issues or pull requests.
mpv_sponsorblock_minimal/sponsorblock_minimal.lua

83 lines
2.2 KiB
Lua
Raw Normal View History

2020-10-22 09:22:17 +00:00
-- sponsorblock_minimal.lua
--
-- This script skips sponsored segments of YouTube videos
-- using data from https://github.com/ajayyy/SponsorBlock
local options = {
API = "https://sponsor.ajay.app/api/skipSegments",
-- Categories to fetch and skip
2021-03-12 11:56:30 +00:00
categories = '"sponsor"'
2020-10-22 09:22:17 +00:00
}
function getranges()
local args = {
"curl",
"-s",
"-d",
"videoID="..youtube_id,
"-d",
2020-10-23 11:33:10 +00:00
"categories=["..options.categories.."]",
2020-10-22 09:22:17 +00:00
"-G",
options.API}
2020-10-27 00:56:23 +00:00
local sponsors = mp.command_native({name = "subprocess", capture_stdout = true, playback_only = false, args = args})
if string.match(sponsors.stdout,"%[(.-)%]") then
ranges = {}
for i in string.gmatch(string.sub(sponsors.stdout,2,-2),"%[(.-)%]") do
k,v = string.match(i,"(%d+.?%d*),(%d+.?%d*)")
ranges[k] = v
end
2020-10-22 09:22:17 +00:00
end
return
end
function skip_ads(name,pos)
2020-10-27 00:56:23 +00:00
if pos ~= nil then
for k,v in pairs(ranges) do
if tonumber(k) <= pos and tonumber(v) > pos then
2021-01-14 16:07:38 +00:00
--this message may sometimes be wrong
--it only seems to be a visual thing though
2020-10-27 00:56:23 +00:00
mp.osd_message("[sponsorblock] skipping forward "..math.floor(tonumber(v)-mp.get_property("time-pos")).."s")
2021-02-08 01:53:43 +00:00
--need to do the +0.01 otherwise mpv will start spamming skip sometimes
--example: https://www.youtube.com/watch?v=4ypMJzeNooo
mp.set_property("time-pos",tonumber(v)+0.01)
2020-10-27 00:56:23 +00:00
return
end
end
2020-10-22 09:22:17 +00:00
end
2020-10-27 00:56:23 +00:00
return
2020-10-22 09:22:17 +00:00
end
function file_loaded()
local video_path = mp.get_property("path")
2021-03-12 11:57:04 +00:00
local video_title = mp.get_property("media-title")
youtube_id = string.match(video_title, ".* %(([%w-_]+)%)")
print(youtube_id)
if not string.match(video_path, "https?://.*%.?googlevideo%.com/.*") or not youtube_id or string.len(youtube_id) < 11 then return end
2020-10-22 09:22:17 +00:00
youtube_id = string.sub(youtube_id, 1, 11)
getranges()
2020-10-27 00:56:23 +00:00
if ranges then
ON = true
mp.add_key_binding("b","sponsorblock",toggle)
mp.observe_property("time-pos", "native", skip_ads)
end
return
2020-10-22 09:22:17 +00:00
end
function toggle()
2020-10-22 23:39:14 +00:00
if ON then
mp.unobserve_property(skip_ads)
2020-10-27 01:07:15 +00:00
mp.osd_message("[sponsorblock] off")
2020-10-22 23:39:14 +00:00
ON = false
2020-10-22 09:22:17 +00:00
return
end
2020-10-22 23:39:14 +00:00
mp.observe_property("time-pos", "native", skip_ads)
2020-10-27 01:07:15 +00:00
mp.osd_message("[sponsorblock] on")
2020-10-22 23:39:14 +00:00
ON = true
2020-10-22 09:22:17 +00:00
return
end
mp.register_event("file-loaded", file_loaded)