Skip to content

Instantly share code, notes, and snippets.

@bitingsock
Last active October 1, 2023 06:28
Show Gist options
  • Save bitingsock/47c5ba6466c63c68bcf991dd376f1d18 to your computer and use it in GitHub Desktop.
Save bitingsock/47c5ba6466c63c68bcf991dd376f1d18 to your computer and use it in GitHub Desktop.
Increases playback speed if a subtitle has not been displayed; resumes normal speed when a new subtitle is shown
---user settings---
local WaitTime = 0.1 --how long until we try to enter "fast" mode
local lookAhead = 5 --how far ahead to look for subtitles when trying to enter "fast" mode
local fast = 6 --how fast "fast" mode is by default
local rewind = 0.2 --how far to rewind when entering normal mode; note that if this is more than or equal to WaitTime + lookAhead you will probably enter an infinite loop
---
local searchtimer
local checktimer
local waitTimer
local normal = mp.get_property("speed")
local subDelay = mp.get_property("options/sub-delay")
local checked = 0
local timer = 0
local searching = false
local scale = 1
local function wait()
fast = tonumber(mp.get_property("speed"))
if mp.get_property("sub-text") ~= "" and mp.get_property("sub-text") ~= nil then
mp.set_property("speed", normal)
waitTimer:kill()
checktimer:resume()
mp.command("no-osd seek -"..tostring(0.05 + rewind).." exact")
end
end
local function search()
if timer < (lookAhead/2) then --half of how far ahead we want to search, because
if mp.get_property("sub-text") ~= "" then
timer = 0
mp.set_property("options/sub-delay", subDelay)
mp.set_property("speed", normal)
mp.set_property("options/sub-scale", scale)
searchtimer:kill()
searching = false
return
end
timer = timer + 0.1
mp.set_property("options/sub-delay", subDelay - (timer*2)) --we multiply it here
searchtimer = mp.add_timeout(0.05, search)
else
mp.set_property("options/sub-delay", subDelay)
mp.set_property("speed", fast)
searchtimer:kill()
checktimer:kill()
waitTimer = mp.add_periodic_timer(0.05, wait)
searching = false
end
end
local function check()
if mp.get_property("sub-text") == "" or mp.get_property("sub-text") == nil then
checked = checked + 0.1
elseif searching == false then
-- print(checked)
if tonumber(mp.get_property("speed")) ~= fast then
normal = mp.get_property("speed")
end
mp.set_property("options/sub-delay", subDelay)
mp.set_property("speed", normal)
mp.set_property("options/sub-scale", scale)
checked = 0
else
checked = 0
end
if checked >= WaitTime and searching == false then
subDelay = mp.get_property("options/sub-delay")
if tonumber(mp.get_property("speed")) < fast then
normal = mp.get_property("speed")
else
fast = tonumber(mp.get_property("speed"))
end
checked = 0
timer = 0.1
mp.set_property("options/sub-scale", 0.001)
mp.set_property("options/sub-delay", subDelay-0.5)
searching = true
searchtimer = mp.add_timeout(0.1, search)
end
end
local function toggle()
if checktimer == nil or (checktimer:is_enabled() == false and waitTimer:is_enabled() == false) then
checktimer = mp.add_periodic_timer(0.1, check)
print("enabled")
scale = mp.get_property("options/sub-scale")
elseif checktimer:is_enabled() or waitTimer:is_enabled() then
checktimer:kill()
waitTimer:kill()
mp.set_property("options/sub-scale", scale)
mp.set_property("speed", normal)
print("disabled")
end
end
mp.add_key_binding("Ctrl+j", "toggle_speedsub", toggle)
@AziRizvi
Copy link

Thanks for this.
I really like this script, is there a way to configure it so it loads and applies by default and I don't have to start it up with the keybind?

@bitingsock
Copy link
Author

bitingsock commented Sep 30, 2023

@AziRizvi you should not be using this. it is deprecated in favor of https://github.com/zenyd/mpv-scripts/blob/master/speed-transition.lua

fyi, in that script if you add toggle() to a new line at the bottom of the script it should do what you want

@AziRizvi
Copy link

AziRizvi commented Oct 1, 2023

That worked, thank you very much!

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