forked from blue-sky-r/mpv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosd-clock.lua
96 lines (82 loc) · 2.65 KB
/
osd-clock.lua
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
-- Show OSD clock
--
-- Shows OSD clock periodicaly with many configurable options,
-- OSD options like duration, alignment, border, scale could be set in ~/.config/mpv/mpv.conf
-- OSD-clock configurable options:
-- interval ... how often to show OSD clock, either seconds or human friendly format like '1h 33m 5s' is supported (default '15m')
-- format ... date format string (default "%H:%M")
-- duration ... how long [in seconds] OSD stays, fractional values supported (default 1.2)
-- key ... to bind show OSD clock on request (false for no binding; default 'h' key)
-- name ... symbolic name (can be used in input.conf, see mpv doc for details; default 'show-clock')
--
-- To customize configuration place osd-clock.conf into ~/.config/mpv/lua-settings/ and edit
--
-- Place script into ~/.config/mpv/scripts/ for autoload
--
-- GitHub: https://github.com/blue-sky-r/mpv/tree/master/scripts
local options = require("mp.options")
local utils = require("mp.utils")
-- defaults
local cfg = {
interval = '15m',
format = "%H:%M",
duration = 2.5,
key = 'h',
name = 'show-clock'
}
-- human readable time format to seconds: 15m 3s -> 903
local function htime2sec(hstr)
local s = tonumber(hstr)
-- only number withoout units
if s then return s end
-- human units h,m,s to seconds
local hu = {h=60*60, m=60, s=1}
s = 0
for unit,mult in pairs(hu) do
local _,_,num = string.find(hstr, "(%d+)"..unit)
if num then
s = s + tonumber(num)*mult
end
end
return s
end
-- calc aligned timeout in sec
local function aligned_timeout(align)
local time = os.time()
local atout = align * math.ceil(time / align) - time
return atout
end
-- read lua-settings/osd-clock.conf
options.read_options(cfg, 'osd-clock')
-- log active config
mp.msg.verbose('cfg = '..utils.to_string(cfg))
-- OSD show clock
local function osd_clock()
local s = os.date(cfg.format)
mp.osd_message(s, cfg.duration)
end
-- non empty interval enables osd clock
if cfg.interval then
-- log
mp.msg.info('interval:'..cfg.interval..', format:'..cfg.format)
-- osd timer
local osd_timer = mp.add_periodic_timer( htime2sec(cfg.interval), osd_clock)
osd_timer:stop()
-- start osd timer exactly at interval boundary
local delay = aligned_timeout( htime2sec( cfg.interval))
-- delayed start
mp.add_timeout(delay,
function()
osd_timer:resume()
osd_clock()
end
)
-- log startup delay for osd timer
mp.msg.verbose('for osd_interval:'..cfg.interval..' calculated startup delay:'..delay)
-- optional bind to the key
if cfg.key then
mp.add_key_binding(cfg.key, cfg.name, osd_clock)
-- log binding
mp.msg.verbose("key:'"..cfg.key.."' bound to '"..cfg.name.."'")
end
end