summaryrefslogtreecommitdiffstats
path: root/.config/mpv/scripts/show_chapters.lua
blob: 643a51f457f406277cc2071578c4d31e1300da7a (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- show chapters and their time at the bottom left corner
-- the timings get proportionally adjusted at different speeds

----------------------------------------
CHAPTERS_TO_SHOW = 10
HOTKEY = 'c'

FONT_SCALE = 80         -- in % from osd-font-size
FONT_SCALE_inactive_chapters = 75
FONT_ALPHA_inactive_chapters = 60

AUTOSTART_IN_PATHS = {
    "^edl://",
    "/med/p/podcasts",
    "/abooks/"
}
----------------------------------------

local assdraw = require('mp.assdraw')

function disp_time(time)
    local hours = math.floor(time/3600)
    local minutes = math.floor((time % 3600)/60)
    local seconds = math.floor(time % 60)
    
    return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end

function time_2_seconds(time)
    h,m,s = time:match('(.*):(.*):(.*)$')
    
    return h*3600 + m*60 + s
end

function show_chapters()
    if observation_active == false then
        observation_active = true
        mp.observe_property("chapter", "number", show_chapters)
        mp.observe_property("speed", "number", show_chapters)
    end

    local osd_w, osd_h, aspect = mp.get_osd_size()
    local ass = assdraw:ass_new()
    ass:new_event()
    ass:an(1)

    -- font scale
    ass:append('{\\fscx' .. tostring(FONT_SCALE) .. '}')
    ass:append('{\\fscy' .. tostring(FONT_SCALE) .. '}')

    local ch_index = mp.get_property_number("chapter")
    local ch_total = mp.get_property_osd("chapter-list/count")

    if ch_index and ch_index >= 0 then
        ass:append("(" .. tostring(ch_index + 1) .. "/" .. tostring(ch_total) .. ")")

        shift = 1
        if ch_index == 0 then
            start_from = 0
        else
            shift = ch_total - CHAPTERS_TO_SHOW - ch_index - 1
            start_from = -1
        end

        if shift < 0 then
            start_from = shift
        end

        for i_ch = start_from, CHAPTERS_TO_SHOW + start_from do
            if tonumber(ch_total) == ch_index + i_ch then
                break
            end
            
            -- from overshooting backwards
            if ch_index + i_ch < 0 then
                goto continue
            end

            title = mp.get_property_osd("chapter-list/" .. tostring(ch_index + i_ch) .. "/title")
            time = mp.get_property_osd("chapter-list/" .. tostring(ch_index + i_ch) .. "/time")

            ass:append("\\N")

            if i_ch == 0 then
                ass:append("{\\alpha&H" .. '00' .. "}")
                ass:append('{\\fscx' .. tostring(FONT_SCALE) .. '}')
                ass:append('{\\fscy' .. tostring(FONT_SCALE) .. '}')
            else
                ass:append("{\\alpha&H" .. tostring(FONT_ALPHA_inactive_chapters) .. "}")
                ass:append('{\\fscx' .. tostring(FONT_SCALE_inactive_chapters) .. '}')
                ass:append('{\\fscy' .. tostring(FONT_SCALE_inactive_chapters) .. '}')
            end

            -- removing paths
            if string.sub(title, 1, 1) == '/' then
                title = title:match('.+/(.*)$')
            end

            speed = mp.get_property_number("speed")
            if speed ~= 1.0 then
                time = disp_time(time_2_seconds(time) / speed)
            end

            ass:append('[' .. time .. "]  " .. title)
            
            ::continue::
        end
    end

    mp.set_osd_ass(osd_w, osd_h, ass.text)
end

function started()
    local pth = mp.get_property("path")
    
    if pth == nil then
        return
    end

    for i, i_path in pairs(AUTOSTART_IN_PATHS) do
		if pth:find(i_path) then
            running = true
            show_chapters()
			return
		end
    end
end

function show_hide()
	if running == true then
        running = false
        observation_active = false
        mp.set_osd_ass(0, 0, "{}")
        mp.unobserve_property(show_chapters)
	else
		running = true
        show_chapters()
	end
end

observation_active = false

mp.register_event("file-loaded", started)
mp.add_forced_key_binding(HOTKEY, 'show_hide', show_hide, {repeatable=true})