Skip to content

Instantly share code, notes, and snippets.

@bitingsock
Created January 14, 2018 22:55
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bitingsock/e8a56446ad9c1ed92d872aeb38edf124 to your computer and use it in GitHub Desktop.
Save bitingsock/e8a56446ad9c1ed92d872aeb38edf124 to your computer and use it in GitHub Desktop.
playback speed triggered by audio volume. similar to https://github.com/zenyd/mpv-scripts/blob/master/speed-transition.lua
local sense=-20
local speed = mp.get_property("speed")
local detect = false
function f(msg)
if string.find(msg.text, "silence_end") and detect then
mp.set_property("speed",speed)
endmsg=msg.text
detect = false
--print("end")
elseif string.find(msg.text, "silence_start") and detect==false then
speed = mp.get_property("speed")
mp.set_property("speed",5)
startmsg=msg.text
detect = true
--print("start")
end
end
function sensitivity(change)
sense = sense + change
mp.osd_message("detection sensitivity: "..sense.."dB")
if string.find(mp.get_property("af"), "silencedetect") then
mp.set_property("af", "lavfi=[silencedetect=n="..sense.."dB:d=1]")
end
end
function toggle()
mp.command("af toggle lavfi=[silencedetect=n="..sense.."dB:d=1]")
if string.find(mp.get_property("af"), "silencedetect") then
mp.osd_message("Silence detection enabled")
mp.register_event("log-message", f)
else
mp.set_property("speed",speed)
mp.osd_message("Silence detection disabled")
mp.unregister_event(f)
end
end
function det(msg)
if string.find(msg.text, "max_volume") then
mp.osd_message(msg.text)
end
end
local timer
function voldetect()
if timer~=nil and timer:is_enabled() then
timer:kill()
mp.unregister_event(det)
else
mp.register_event("log-message", det)
timer = mp.add_periodic_timer(0.1, function()
mp.command("no-osd af toggle lavfi=[volumedetect]")
mp.add_timeout(0.09, function()
mp.command("no-osd af toggle lavfi=[volumedetect]")
end)
end)
end
end
mp.enable_messages("v")
mp.add_key_binding("F2", "toggle", toggle)
mp.add_key_binding("alt+F2", "voldetect", voldetect)
mp.add_key_binding("shift+F2", "sense-up", function() sensitivity(1) end, "repeatable")
mp.add_key_binding("ctrl+F2", "sense-down", function() sensitivity(-1) end, "repeatable")
@Topping1
Copy link

@Topping1 You need to use three ticks in order to show whitespace.

like
this

thanks!

@ferreum
Copy link

ferreum commented Jul 15, 2023

I've reworked this into a highly configurable script. I've published it here: https://github.com/ferreum/mpv-skipsilence
The main issue is that audio-video is still desynced very easily. Using less aggressive speed ramps and a slower update interval helps, but it's never perfect. I added a workaround for that (check the doc at the top of the script). But if you're only listening to the audio, this is not a problem.

Thanks for publishing this, it was a very good starting point.

@bitingsock
Copy link
Author

Good job! My hope with this, although not that useful personally, was always that it would inspire someone who really cared about it to take it to a point that they thought was useful.

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