sidrefresh's picture
Add files using upload-large-folder tool
5eec1f1 verified
Raw History Blame Contribute Delete
1.52 kB
-- Track actual forward playback of the captioned tutorial. This is installed
-- in the viewer image, not exposed as an editing tool to the agent.
local mp = require 'mp'
local path = '/logs/artifacts/tutorial-watch.json'
local watched = 0
local max_pos = 0
local last = nil
local started = os.time()
local function finite(n)
if type(n) ~= 'number' or n ~= n or n == math.huge or n == -math.huge then
return 0
end
return n
end
local function save(event)
local payload = string.format(
'{"event":"%s","started_at_epoch":%d,"updated_at_epoch":%d,' ..
'"watched_seconds":%.1f,"max_position_seconds":%.1f,' ..
'"duration_seconds":%.1f,"paused":%s}\n',
event, started, os.time(), finite(watched), finite(max_pos),
finite(mp.get_property_number('duration', 0)),
tostring(mp.get_property_bool('pause', false)))
local file = io.open(path, 'w')
if file then
file:write(payload)
file:close()
end
end
mp.register_event('file-loaded', function() save('loaded') end)
mp.register_event('end-file', function() save('ended') end)
mp.add_periodic_timer(1, function()
local pos = mp.get_property_number('time-pos', nil)
if pos then
if pos > max_pos then max_pos = pos end
if last and not mp.get_property_bool('pause', false) then
local delta = pos - last
if delta > 0 and delta < 2 then watched = watched + delta end
end
last = pos
save('playing')
end
end)