File size: 1,518 Bytes
7ec48b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- 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)