mpv: Fix sponsorblock code and formatting

Fixed global variable naming (capitalized) and let code be formatted by
formatter.
This commit is contained in:
Marty Oehme 2022-10-08 18:13:43 +02:00
parent 485730ba83
commit a3b00c4693
Signed by: Marty
GPG key ID: 73BA40D5AFAF49C9

View file

@ -5,7 +5,6 @@
-- --
-- original from https://codeberg.org/jouni/mpv_sponsorblock_minimal -- original from https://codeberg.org/jouni/mpv_sponsorblock_minimal
-- adapted for local playback skipping and some refactoring by me -- adapted for local playback skipping and some refactoring by me
local options = { local options = {
API = "https://sponsor.ajay.app/api/skipSegments", API = "https://sponsor.ajay.app/api/skipSegments",
@ -15,36 +14,39 @@ local options = {
local function getranges() local function getranges()
local args = { local args = {
"curl", "curl", "-s", "-d", "videoID=" .. Youtube_id, "-d",
"-s", "categories=[" .. options.categories .. "]", "-G", options.API
"-d", }
"videoID="..youtube_id, local sponsors = mp.command_native({
"-d", name = "subprocess",
"categories=["..options.categories.."]", capture_stdout = true,
"-G", playback_only = false,
options.API} args = args
local sponsors = mp.command_native({name = "subprocess", capture_stdout = true, playback_only = false, args = args}) })
if string.match(sponsors.stdout,"%[(.-)%]") then if string.match(sponsors.stdout, "%[(.-)%]") then
ranges = {} Ranges = {}
for i in string.gmatch(string.sub(sponsors.stdout,2,-2),"%[(.-)%]") do for i in string.gmatch(string.sub(sponsors.stdout, 2, -2), "%[(.-)%]") do
k,v = string.match(i,"(%d+.?%d*),(%d+.?%d*)") local k, v = string.match(i, "(%d+.?%d*),(%d+.?%d*)")
ranges[k] = v Ranges[k] = v
end
end end
return
end end
return
end
local function skip_ads(name,pos) local function skip_ads(name, pos)
if pos ~= nil then if pos ~= nil then
for k,v in pairs(ranges) do for k, v in pairs(Ranges) do
if tonumber(k) <= pos and tonumber(v) > pos then if tonumber(k) <= pos and tonumber(v) > pos then
--this message may sometimes be wrong -- this message may sometimes be wrong
--it only seems to be a visual thing though -- it only seems to be a visual thing though
mp.osd_message("[sponsorblock] skipping forward "..math.floor(tonumber(v)-mp.get_property("time-pos")).."s") mp.osd_message("[sponsorblock] skipping forward " ..
--need to do the +0.01 otherwise mpv will start spamming skip sometimes math.floor(
--example: https://www.youtube.com/watch?v=4ypMJzeNooo tonumber(v) - mp.get_property("time-pos")) ..
mp.set_property("time-pos",tonumber(v)+0.01) "s")
-- 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)
return return
end end
end end
@ -54,19 +56,23 @@ end
local function file_loaded() local function file_loaded()
local video_path = mp.get_property("path") local video_path = mp.get_property("path")
local youtube_id1 = string.match(video_path, "https?://youtu%.be/([%w-_]+).*") local youtube_id1 = string.match(video_path,
local youtube_id2 = string.match(video_path, "https?://w?w?w?%.?youtube%.com/v/([%w-_]+).*") "https?://youtu%.be/([%w-_]+).*")
local youtube_id2 = string.match(video_path,
"https?://w?w?w?%.?youtube%.com/v/([%w-_]+).*")
local youtube_id3 = string.match(video_path, "/watch.*[?&]v=([%w-_]+).*") local youtube_id3 = string.match(video_path, "/watch.*[?&]v=([%w-_]+).*")
local youtube_id4 = string.match(video_path, "/embed/([%w-_]+).*") local youtube_id4 = string.match(video_path, "/embed/([%w-_]+).*")
local localytfile = string.match(video_path, "-([%a%d%-_]+)%.[mw][kpe][v4b][m]?$") local localytfile = string.match(video_path,
youtube_id = youtube_id1 or youtube_id2 or youtube_id3 or youtube_id4 or localytfile "-([%a%d%-_]+)%.[mw][kpe][v4b][m]?$")
if not youtube_id or string.len(youtube_id) < 11 then return end Youtube_id = youtube_id1 or youtube_id2 or youtube_id3 or youtube_id4 or
youtube_id = string.sub(youtube_id, 1, 11) localytfile
if not Youtube_id or string.len(Youtube_id) < 11 then return end
Youtube_id = string.sub(Youtube_id, 1, 11)
getranges() getranges()
if ranges then if Ranges then
ON = true ON = true
mp.add_key_binding("b","sponsorblock",toggle) mp.add_key_binding("b", "sponsorblock", toggle)
mp.observe_property("time-pos", "native", skip_ads) mp.observe_property("time-pos", "native", skip_ads)
end end
return return