mpv scripts

This commit is contained in:
Tibeuleu
2025-11-22 22:31:21 +01:00
parent 883c4cabb0
commit 17b7ac323d
5 changed files with 1493 additions and 101 deletions

View File

@@ -73,7 +73,7 @@
# video outputs, but should work well with default settings on most systems.
# If performance or energy usage is an issue, forcing the vdpau or vaapi VOs
# may or may not help.
hwdec=auto
# hwdec=auto
##################
# audio settings #
@@ -97,7 +97,7 @@ hwdec=auto
# other settings #
##################
#
screenshot-template="%F_%p-%#01n"
# screenshot-template="%F_%p-%#01n"
# automatically save the current playback position on quit
save-position-on-quit
@@ -109,11 +109,11 @@ save-position-on-quit
# cache settings
#
# Use a large seekable RAM cache even for local input.
#cache=yes
cache=yes
#
# Use extra large RAM cache (needs cache=yes to make it useful).
#demuxer-max-bytes=500M
#demuxer-max-back-bytes=100M
demuxer-max-bytes=1G
demuxer-max-back-bytes=500M
#
# Disable the behavior that the player will pause if the cache goes below a
# certain fill size.
@@ -154,5 +154,6 @@ alang=fr,en
# Streaming #
#############
# Youtube Support
script-opts=ytdl_hook-ytdl_path=/home/t.barnouin/.local/bin/yt-dlp
ytdl-format=bestvideo[height<=?720][fps<=?30][vcodec!~='^(vp0?9|av0?1)']+bestaudio/best
script-opts=ytdl_hook-ytdl_path=/usr/bin/yt-dlp
ytdl-raw-options-append="extractor-args=youtube:player-client=default,-tv_simply"
ytdl-format=bestvideo[height>=?720][height<=?1080][fps<=?60][vcodec!~='^(vp0?9|av0?1)']+bestaudio/best

View File

@@ -0,0 +1,164 @@
# Whether skipsilence should be enabled by default. Can also be changed
# at runtime and reflects the current enabled state.
enabled = false,
# The silence threshold in decibel. Anything quieter than this is
# detected as silence. Can be adjusted with the threshold-up,
# threshold-down bindings, and adjust-threshold-db script message.
threshold_db = -30,
# Minimum duration of silence to be detected, in seconds. This is
# measured in seconds of stream time, as if playback speed was 1.
threshold_duration = 0.1,
# How long to wait before speedup. This is measured in seconds of real
# time, thus higher playback speeds would reduce the length of content
# skipped.
#
# Ignored while `lookahead` is used. Use `margin_start` instead.
startdelay = 0.05,
# How long to look ahead to allow slowing down ahead of end of silence.
#
# EXPERIMENTAL: Enabling this completely changes internal timing logic. It
# may be less reliable than operation without lookahead.
#
# Low values (~0.2s) tend to make filter adjustments (threshold_*) more
# jarring because of skipped audio. Higher values (~1.0s) cause a seek
# event instead, which may be less problematic. Do not set this too high,
# as it introduces additional buffering and could reduce timing precision.
#
# Recommended values are between 0.5 and 1.0.
#
# Option filter_persistent should be enabled for seamless toggling of the
# script.
lookahead = 0,
# EXPERIMENTAL: For lookahead: Extra margin at start and end of detected
# silence. `margin_start` delays speed-up, `margin_end` slows down
# earlier, by the specified time.
#
# Measured in seconds of stream time. Negative values are allowed, having
# the opposite effect.
#
# Requires lookahead to be active. Maximum backwards adjustment is limited
# by the lookahead period (positive `margin_end` or negative
# `margin_start`).
margin_start = 0.05,
margin_end = 0,
# EXPERIMENTAL: For lookahead: minimum length of silence for speed to be
# increased. This is a way to extend `threshold_duration` without needing
# to update the filter.
#
# Increases the required duration of silence, without delaying the
# starting point like startdelay (by up to the lookahead duration).
# Measured in seconds of stream time.
minduration = 0,
# How often to update the speed during silence, in seconds of real time.
speed_updateinterval = 0.05,
# The maximum playback speed during silence.
speed_max = 4,
# Speedup ramp parameters. The formula for playback speedup is:
#
# ramp_constant + (time * ramp_factor) ^ ramp_exponent
#
# Where time is the real time in seconds passed since start of speedup.
# The result is multiplied with the original playback speed.
#
# - ramp_constant should always be greater or equal to one, otherwise it
# will slow down at the start of silence.
# - Setting ramp_factor to 0 disables the ramp, resulting in a constant
# speed during silence.
# - ramp_exponent is the "acceleration" of the curve. A value of 1
# results in a linear curve, values above 1 increase the speed faster
# the more time has passed, while values below 1 speed up at
# decreasing intervals.
ramp_constant = 1.25,
ramp_factor = 2.5,
ramp_exponent = 1,
# EXPERIMENTAL: Same as ramp_* options, but for slowdown when using
# lookahead. 'time' is the remaining time to the end of silence.
# Note this is measured in stream time, different from the ramp_*
# options, which use real time. Choose a lower exponent to compensate.
#
# While slowdown ramp is active, always the lower speed calculated by the
# two ramps is used.
slowdown_ramp_constant = 1,
slowdown_ramp_factor = 3,
slowdown_ramp_exponent = 0.6,
# Noise reduction filter configuration.
#
# This allows removing noise from the audio stream before the
# silencedetect filter, allowing to speed up pauses in speech despite
# background noise. The output audio is unaffected by default.
#
# Whether the detected audio signal should be preprocessed with arnndn.
# If arnndn_modelpath is empty, this has no effect
arnndn_enable = true,
# Path to the rnnn file containing the model parameters. If empty,
# noise reduction is disabled.
# The value is expanded with the expand-path command. See "Paths" in the
# mpv manual.
# Avoid special characters in this option, they must be escaped to
# work with "af add lavfi=[arnndn='...']".
arnndn_modelpath = "",
# Whether the denoised signal should be used as the output. Disabled by
# default, so the output is unaffected.
arnndn_output = false,
# If >= 0, use this value instead of a playback speed of 1.
# This is a work around to stop audio clicks when switching between
# normal playback and speeding up. Playing back at a slightly different
# speed (e.g. 1.01x), keeps the scaletempo2 filter active, so audio is
# played back without interruptions.
alt_normal_speed = -1,
# Workaround for audio-video de-synchronization with scaletempo2 in
# mpv 0.36 and below. When disabling skipsilence, fix audio sync if this
# many frames have been dropped since the last playback restart
# (seek, etc.). Disabled if value is less than 0.
#
# When disabling skipsilence while frame-drop-count is greater or equal
# to configured value, audio-video sync is fixed by running
# 'seek 0 exact'. May produce a short pause and/or audio repeat.
#
# Note that frame-drop-count does not exactly correspond to the
# audio-video desynchronization. It is used as a heuristic to avoid
# resyncing every time the script is disabled. Recommended value: 100.
resync_threshold_droppedframes = -1,
# Keep the filter added while the script is disabled. This prevents
# most audio interruptions/clicks when toggling the script.
# If arnndn_output is enabled, noise reduction also stays active while
# the script is disabled.
filter_persistent = false,
# Info style used for the 'user-data/skipsilence/info' property and
# the default of the 'info' script-message/binding.
# May be one of
# - 'off' (no information),
# - 'total' (show total saved time),
# - 'compact' (show total and latest saved time),
# - 'verbose' (show most information).
infostyle = "off",
# When to reset the total saved time.
# May be one of
# - 'file-start' (when a new file starts)
# - 'never' (do not reset total)
reset_total = "file-start",
# How to apply external speed change during silence.
# This makes speed change bindings work during fast forward. Set the
# value according to what command you use to change speed:
# - 'add' - add the difference to the normal speed
# - 'multiply' - multiply the normal speed with factor of change
# If 'off', the script will override the speed during silence.
# Note: this option is unreliable in cases where the script changes speed
# at the exact same time. Prefer the adjust-speed message instead.
apply_speed_change = "add",
debug = false,

View File

@@ -0,0 +1,16 @@
# Default configuration options for mpv_sponsorblock_minimal
# Category types that are skipped. Categories are separated by semicolons.
# List of categories can be found here: https://wiki.sponsor.ajay.app/w/Types
# To skip all categories except "filler", you could set this to:
# categories=sponsor
categories=sponsor;selfpromo;interaction;intro;outro;preview;hook
# This option makes it so that the video ID will not be sent to the
# SponsorBlock server, but a part of the video ID's SHA256 hash will be sent
# instead.
# Set to "true" to enable this option.
hash=true
# This sets the URL where the script gets the segments from.
server=https://sponsor.ajay.app/api/skipSegments

File diff suppressed because it is too large Load Diff

View File

@@ -3,6 +3,7 @@
-- This script skips sponsored segments of YouTube videos
-- using data from https://github.com/ajayyy/SponsorBlock
local mp = require "mp"
local opt = require 'mp.options'
local utils = require 'mp.utils'
@@ -10,120 +11,121 @@ local ON = false
local ranges = nil
local options = {
server = "https://sponsor.ajay.app/api/skipSegments",
server = "https://sponsor.ajay.app/api/skipSegments",
-- Categories to fetch and skip
categories = '"sponsor","intro","outro","interaction","selfpromo"',
-- Categories to fetch and skip
categories = '"sponsor","intro","outro","interaction","selfpromo"',
-- Set this to "true" to use sha256HashPrefix instead of videoID
hash = ""
-- Set this to "true" to use sha256HashPrefix instead of videoID
hash = ""
}
opt.read_options(options)
function skip_ads(name,pos)
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"))))
--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",v+0.01)
return
end
end
end
function skip_ads(name, pos)
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"))))
--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", v + 0.01)
return
end
end
end
end
function file_loaded()
local video_path = mp.get_property("path", "")
local video_referer = string.match(mp.get_property("http-header-fields", ""), "Referer:([^,]+)") or ""
local video_path = mp.get_property("path", "")
local video_referer = string.match(mp.get_property("http-header-fields", ""), "Referer:([^,]+)") or ""
local urls = {
"ytdl://youtu%.be/([%w-_]+).*",
"ytdl://w?w?w?%.?youtube%.com/v/([%w-_]+).*",
"https?://youtu%.be/([%w-_]+).*",
"https?://w?w?w?%.?youtube%.com/v/([%w-_]+).*",
"/watch.*[?&]v=([%w-_]+).*",
"/embed/([%w-_]+).*",
"^ytdl://([%w-_]+)$",
"-([%w-_]+)%."
}
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
local urls = {
"ytdl://youtu%.be/([%w-_]+).*",
"ytdl://w?w?w?%.?youtube%.com/v/([%w-_]+).*",
"https?://youtu%.be/([%w-_]+).*",
"https?://w?w?w?%.?youtube%.com/v/([%w-_]+).*",
"/watch.*[?&]v=([%w-_]+).*",
"/embed/([%w-_]+).*",
"^ytdl://([%w-_]+)$",
"-([%w-_]+)%."
}
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)
if not youtube_id or string.len(youtube_id) < 11 then return end
youtube_id = string.sub(youtube_id, 1, 11)
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 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
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
if ranges then
ON = true
mp.add_key_binding("b", "sponsorblock", toggle)
mp.observe_property("time-pos", "native", skip_ads)
end
end
end
end
function end_file()
if not ON then return end
mp.unobserve_property(skip_ads)
ranges = nil
ON = false
if not ON then return end
mp.unobserve_property(skip_ads)
ranges = nil
ON = false
end
function toggle()
if ON then
mp.unobserve_property(skip_ads)
mp.osd_message("[sponsorblock] off")
ON = false
else
mp.observe_property("time-pos", "native", skip_ads)
mp.osd_message("[sponsorblock] on")
ON = true
end
if ON then
mp.unobserve_property(skip_ads)
mp.osd_message("[sponsorblock] off")
ON = false
else
mp.observe_property("time-pos", "native", skip_ads)
mp.osd_message("[sponsorblock] on")
ON = true
end
end
mp.register_event("file-loaded", file_loaded)