-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobserve.lua
154 lines (146 loc) · 4.23 KB
/
observe.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
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
146
147
148
149
150
151
152
153
print(_VERSION)
local mp = require 'mp'
socket = require "socket"
socket.unix = require"socket.unix"
local last_time = 0
local last_arate = 0
function on_width_change(name,value)
local file = io.open("video_size","r")
local exists = false
if file then
io.close(file)
exists = true
end
height = mp.get_property("osd-height")
if (value > 0 and tonumber(height) > 0) then
file = io.open("video_size", "w")
file:write(string.format("%d\n%d\n",value,height))
par = mp.get_property("osd-par")
print("on_width_change : " .. value .. " " .. height .. " par " .. par)
file:close()
if exists then
return
end
local c = assert(socket.unix())
if (c:connect("sock_list")) then
c:send("clear\n")
c:close()
end
-- on insère l'appel dans une pause de 1s parce que la video se
-- configure 2 fois très rapidement quand lancé en plein écran !
mp.add_timeout(0.2,function ()
os.execute("./info short")
end)
end
end
function metadata(name,value)
if not value then
return
end
for i,v in pairs(value) do
local c = assert(socket.unix())
if (c:connect("sock_info")) then
c:send("metadata " .. i .. " " .. v)
c:close()
else
print(i,v)
end
end
local c = assert(socket.unix())
if (c:connect("sock_info")) then
c:send("metadata end 1")
c:close()
end
end
function on_abitrate(name,value)
local vcodec = mp.get_property("video-codec")
if not value then
return
end
if not vcodec or string.find(vcodec,"JPEG") then
local acodec = mp.get_property("audio-codec-name")
if not acodec then
return
end
if math.abs(value/1000-last_arate) < 1 then
return
end
last_arate = value/1000
local c = assert(socket.unix())
if (c:connect("sock_info")) then
c:send("codec " .. acodec .. " " .. (value/1000) .. "k")
c:close()
else
print("acodec " .. acodec .. " bitrate " .. (value/1000) .. "k")
end
end
end
function show_ass(name)
-- for a weird story about utf-8 expanding, it's just impossible to
-- pass an ass color sequence to the socket, the \\1c is taken as a
-- standard esc sequence, which is unknown and it just throws an error
-- so we extend the syntax here, recognizing at least {red} {orange}
-- and {white}
-- command should be used like that :
-- echo '{ "command": ["expand-properties", "script-message", "show-ass", "text"] }'
-- This is actually so crazy that I would advise against using this, I
-- just need some hardware monitoring here so I'll use it anyway...
name = string.gsub(name,"{red}","{\\1c&H0000FF&}")
name = string.gsub(name,"{orange}","{\\1c&H0080FF&}")
name = string.gsub(name,"{white}","{\\1c&HffffFF&}")
mp.commandv("show-text", name, 8000)
-- s="show-text ${osd-ass-cc/0}{\\1c&H0000FF&}hello"
end
function timing(name,value)
local duration = mp.get_property("duration")
if not duration then
return
end
local vcodec = mp.get_property("video-codec")
if vcodec and not vcodec:match("^mjpeg") and not string.find(vcodec,"JPEG") then
return
end
value = math.floor(value)
if value == last_time then
return
end
last_time = value
local c = assert(socket.unix())
if (c:connect("sock_info")) then
c:send("progress " .. value .. " " .. duration)
c:close()
end
end
mp.observe_property("osd-width","number",on_width_change)
mp.observe_property("audio-bitrate","number",on_abitrate)
mp.observe_property("metadata","native",metadata)
-- mp.observe_property("chapter-metadata","native",metadata)
mp.observe_property("time-pos","native",timing)
mp.register_script_message("show-ass",show_ass)
mp.add_hook("on_unload", 10, function ()
local c = assert(socket.unix())
name = mp.get_property("path")
if (c:connect("sock_info")) then
c:send("unload " .. name)
c:close()
end
pos = mp.get_property_number("percent-pos")
if not pos then
-- si on lit une url, on aura pas de pos ici
return
end
c = assert(socket.unix())
if (c:connect("sock_list")) then
local duration = mp.get_property_number("duration")
if (pos < 90 or pos > 100) and duration > 9*60 then
-- gestion basique des bookmarks :
-- on ne semble pas pouvoir modifier directement le fichier
-- en lua, donc on transmet la commande à list.pl
pos = mp.get_property("time-pos")
c:send("bookmark " .. name .. " " .. pos .. "\n")
else
c:send("bookmark " .. name .. " del\n")
end
c:close()
end
end)