Skip to content

Instantly share code, notes, and snippets.

@microraptor
Created June 21, 2021 22:54
Show Gist options
  • Save microraptor/ddbf72eb4d66ca67ecc48b0303f6de19 to your computer and use it in GitHub Desktop.
Save microraptor/ddbf72eb4d66ca67ecc48b0303f6de19 to your computer and use it in GitHub Desktop.
This is a MPV (https://mpv.io/) script and should be placed into the scripts folder of its config folder. The script skips sponsor segments. It is meant to be used with downloaded Youtube videos via yt-dlp that have chapters marked with sponskrub.
--[[
This is a MPV (https://mpv.io/) script and should be placed into the scripts folder of its config folder.
The script skips sponsor segments.
It is meant to be used with downloaded Youtube videos via yt-dlp that have chapters marked with sponskrub:
https://github.com/yt-dlp/yt-dlp#sponskrub-sponsorblock-options
It also works with mpv_sponsorblock.
The actions for each type of segment can be configured below or in script-opts/sponsor-skip.conf.
All six segment types can be tested with these videos:
https://www.youtube.com/watch?v=K07sEM6y4Uc
https://www.youtube.com/watch?v=BgfcToAjfdc
https://www.youtube.com/watch?v=Joed0P3hhbc
If you are using the uosc script (https://github.com/darsain/uosc),
you can color the segments in the timeline using the uosc.conf file like this:
chapter_ranges=sponskrub%-sponsor<007800:0.5>.*,Sponsor segment start<007800:0.5>Sponsor segment end,sponskrub%-intro<808000:0.5>.*,Intro segment start<808000:0.5>Intro segment end,sponskrub%-outro<700000:0.5>.*,Outro segment start<700000:0.5>Outro segment end,sponskrub%-interaction<87006c:0.5>.*,Interaction segment start<87006c:0.5>Interaction segment end,sponskrub%-selfpromo<35bfbf:0.5>.*,Selfpromo segment start<35bfbf:0.5>Selfpromo segment end,sponskrub%-music%_offtopic<4a63a6:0.5>.*
TODO: skip-on-keypress action
--]]
require 'mp.options'
-- ACTIONS CONFIG --
-- skip: skips to the next chapter on playback
-- print: prints temporarily the segment type on the on-screen-display
local options = {
actions = {
sponsor = "skip",
intermission = "none", -- intro
endcards = "none", -- outro
interaction_reminder = "print",
unpaid_promotion = "print",
non_music = "none",
},
patterns = {
-- uses lua patterns; % is an escape symbol
sponsor = {
"sponskrub%-sponsor",
-- The keyword below is commented out because mpv_sponsorblock already skips sponsor segments by default
-- "Sponsor segment start",
},
intermission = {
"sponskrub%-intro",
"Intro segment start",
},
endcards = {
"sponskrub%-outro",
"Outro segment start",
},
interaction_reminder = {
"sponskrub%-interaction",
"Interaction segment start",
},
unpaid_promotion = {
"sponskrub%-selfpromo",
"Selfpromo segment start",
},
non_music = {
"sponskrub%-music%_offtopic",
},
},
pretty_names = {
sponsor = "Sponsor",
intermission = "Intermission",
endcards = "Endcards",
interaction_reminder = "Interaction Reminder",
unpaid_promotion = "Unpaid Promotion",
non_music = "Non-Music",
},
print_duration = 2,
}
read_options(options)
function check_chapter(_, chapter)
if not chapter then
return
end
for segment_type, action in pairs(options.actions) do
if (action == "skip" or action == "print") then
for _, p in pairs(options.patterns[segment_type]) do
if string.match(chapter, p) then
if action == "skip" then
mp.osd_message("Skipped " .. options.pretty_names[segment_type], options.print_duration)
mp.command("no-osd add chapter 1")
else
mp.osd_message(options.pretty_names[segment_type], options.print_duration)
end
return
end
end
end
end
end
mp.observe_property("chapter-metadata/by-key/title", "string", check_chapter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment