summaryrefslogtreecommitdiff
path: root/mpv/scripts/auto-save-state.lua
blob: a4ec3ec6aa55fbbbcfa26f7cd17642dc2bd91295 (plain)
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
47
48
49
50
51
52
53
54
55
-- Save watch-later conditionally.
-- a) Always for playlists (so mpv remembers the position within this playlist)
-- b) Never for files shorter than `min_length` seconds
-- c) When the current playback position is > `thresh_start` and < `thresh_end`


local opts = require 'mp.options'
local o = {
    min_length = 600,
    thresh_end = 180,
    thresh_start = 60,
}
opts.read_options(o)


-- Return true when multiple files are being played
function check_playlist()
    local pcount, err = mp.get_property_number("playlist-count")
    if not pcount then
        print("error: " .. err)
        pcount = 1
    end

    return pcount > 1
end


-- Return true when the current playback time is not too close to the start or end
-- Always return false for short files, no matter the playback time
function check_time()
    local duration = mp.get_property_number("duration", 9999)
    if duration < o.min_length then
        return false
    end

    local remaining, err = mp.get_property_number("time-remaining")
    if not remaining then
        print("error: " .. err)
        remaining = -math.huge
    end
    local pos, err = mp.get_property_number("time-pos")
    if not pos then
        print("error: " .. err)
        pos = -math.huge
    end

    return pos > o.thresh_start and remaining > o.thresh_end
end


mp.add_key_binding("q", "quit-watch-later-conditional",
    function()
        mp.set_property_bool("options/save-position-on-quit", check_playlist() or check_time())
        mp.command("quit")
    end)