Skip to content

Instantly share code, notes, and snippets.

@bitingsock
Created January 14, 2018 22:55
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
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")
@laelnasan
Copy link

omg I've been looking for something like this for so long, thanks!

@Reinbowsaur
Copy link

I don't know if you are still around but this script leaves so much to desire. Such as timeouts between skips to avoid constantly cutting, custom buffers to re-wind/stop abit before the audio starts so you aren't skipped slightly into some detected sound/dialouge. I've tried to mess with this... but I'm just too terrible at making it do what I want. Please consider doing a touchup!

@Topping1
Copy link

Topping1 commented Nov 22, 2022

I don't know if you are still around but this script leaves so much to desire. Such as timeouts between skips to avoid constantly cutting, custom buffers to re-wind/stop abit before the audio starts so you aren't skipped slightly into some detected sound/dialouge. I've tried to mess with this... but I'm just too terrible at making it do what I want. Please consider doing a touchup!

Its been a while but I made a couple of modifications to the script and seems to work better. Be sure to regulate the sensitivity for an optimal experience. I changed the keybindings but that can be changed back if you don't like them.

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",2)
		mp.set_property("speed",2.5)
		mp.set_property("speed",3)
		mp.set_property("speed",2.5)
		mp.set_property("speed",2)
		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=0.5]")
	end
end

function toggle()
	mp.command("af toggle lavfi=[silencedetect=n="..sense.."dB:d=0.5]")
	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("shift+F2", "voldetect", voldetect)
mp.add_key_binding("shift+F3", "sense-up", function() sensitivity(1) end, "repeatable")
mp.add_key_binding("shift+F4", "sense-down", function() sensitivity(-1) end, "repeatable")```

@SuperSamus
Copy link

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

```
like
      this
```

@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