Last active
November 24, 2018 14:33
-
-
Save jgreco/b4a5de76da3637e3f85006f34c090e29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local mp = require 'mp' | |
local msg = require 'mp.msg' | |
local time_saved = 0 -- TODO keep running system total | |
local last_time = 0 -- FIXME reset this on on_load? | |
local last_speed = 1 -- FIXME not necessarily true, set this for real on on_load | |
local last_tick_time = 0 | |
local function update_saved(last, new, accum, speed) | |
local time_elapsed = new - last | |
return accum + (time_elapsed - time_elapsed / speed) | |
end | |
mp.observe_property("speed", "number", function(name, new_speed) | |
local new_time = mp.get_property_number("time-pos") | |
if not new_time then return end | |
time_saved = update_saved(last_time, new_time, time_saved, last_speed) | |
last_speed = new_speed | |
last_time = new_time | |
end) | |
mp.add_periodic_timer(1, function() | |
local new_time = mp.get_property_number("time-pos") | |
if not new_time then return end | |
--guard against race between timer and seek event | |
if new_time > 2.0*last_speed+last_time then last_time = new_time return end | |
time_saved = update_saved(last_time, new_time, time_saved, last_speed) | |
last_time = new_time | |
-- TODO update system total (on slower timer tho) | |
msg.error("Time saved: "..time_saved) | |
end) | |
mp.register_event("seek", function() | |
local new_time = mp.get_property_number("time-pos") | |
if new_time < 0 then new_time = 0 end | |
last_time = new_time | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment