Skip to content

Instantly share code, notes, and snippets.

@bossen
Last active May 8, 2022 01:29
Show Gist options
  • Select an option

  • Save bossen/3cfe86a6cdd61452dbb96865128fb327 to your computer and use it in GitHub Desktop.

Select an option

Save bossen/3cfe86a6cdd61452dbb96865128fb327 to your computer and use it in GitHub Desktop.
mpv lua script. Seeks forward until a black screen appears. Built to skip openings. Uses the lavfi blackdetect filter.
-- betterchapters.lua
-- seeks forward until a black screen appears.
-- default keybinding: b
-- Keybind names: skip_scene
script_name = mp.get_script_name()
detect_label = string.format("%s-detect", script_name)
detecting = false
threshold = 0.9
detection_span = 0.05
negation = false
normal_speed = 1
seek_position = 0.0
function toggle_detect()
if (detecting) then
stop()
else
detecting = not detecting
normal_speed = mp.get_property_native("speed")
mp.set_property("speed", 100)
detect()
end
end
-- from https://github.com/mpv-player/mpv/blob/master/TOOLS/lua/autocrop.lua
function del_filter_if_present(label)
-- necessary because mp.command('vf del @label:filter') raises an
-- error if the filter doesn't exist
local vfs = mp.get_property_native("vf")
for i,vf in pairs(vfs) do
if vf["label"] == label then
table.remove(vfs, i)
mp.set_property_native("vf", vfs)
return true
end
end
return false
end
function toggle_negation()
del_filter_if_present(detect_label)
if (negation) then
del_filter_if_present("negate")
negation = false
else
mp.command('vf add @negate:lavfi=graph="negate=1"')
negation = true
end
end
function detect()
if (detecting) then
toggle_negation()
mp.command(
string.format(
'vf add @%s:lavfi=graph="blackdetect=d=%s:pic_th=%s"',
detect_label, detection_span, threshold)
)
mp.add_timeout(detection_span, seek)
end
end
function seek()
local res = mp.get_property_native(string.format("vf-metadata/%s", detect_label))
if (res["lavfi.black_start"]) then
seek_position = res["lavfi.black_start"]
stop()
else
detect()
end
end
function stop()
del_filter_if_present("negate")
del_filter_if_present(detect_label)
detecting = false
negation = false
mp.set_property("speed", 1)
-- fix audio video desynchronisation
mp.commandv("seek", seek_position, "absolute", "exact")
end
mp.add_key_binding("n", "skip_scene", toggle_detect)
@Mikle-Bond

Copy link
Copy Markdown
-- default keybinding: b
mp.add_key_binding("n", "skip_scene", toggle_detect)

Am I the only one who sees it?

@octos

octos commented Mar 30, 2019

Copy link
Copy Markdown

@Mikle-Bond You're right. That's wrong.

@detuur

detuur commented Jan 24, 2020

Copy link
Copy Markdown

Why are you using negate? This has got me a bit confused.

@Tatsh

Tatsh commented May 8, 2022

Copy link
Copy Markdown

This does not work with hardware acceleration so it is kind of useless for anything above 720p. And obviously no good for those on battery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment