-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.lua
480 lines (426 loc) · 16 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
local utils = require 'mp.utils'
local options = require 'mp.options'
local messages = nil
local chat_overlay = nil
local chat_hidden = false
local is_windows = package.config:sub(1,1) ~= "/"
local xdg_data_home = is_windows and "" or (os.getenv("XDG_DATA_HOME") or (os.getenv("HOME") .. "/.local/share"))
local opts = {}
opts['auto-load'] = false
opts['live-chat-directory'] = is_windows and 'C:/' or (xdg_data_home .. '/youtube-live-chats')
opts['yt-dlp-path'] = 'yt-dlp'
opts['show-author'] = true
opts['author-color'] = 'random'
opts['author-border-color'] = '000000'
opts['message-color'] = 'ffffff'
opts['message-border-color'] = '000000'
opts['font'] = mp.get_property_native('osd-font')
opts['font-size'] = 16
opts['border-size'] = 2
opts['message-duration'] = 10000
opts['max-message-line-length'] = 40
opts['message-break-anywhere'] = false
opts['message-gap'] = 10
opts['anchor'] = 1
options.read_options(opts)
options.read_options(opts, "mpv-youtube-chat")
local NORMAL = 0
local SUPERCHAT = 1
local delimiter_pattern = " %.,%-!%?"
local function split_string(input)
local splits = {}
for input in string.gmatch(input, "[^" .. delimiter_pattern .. "]+[" .. delimiter_pattern .. "]*") do
table.insert(splits, input)
end
return splits
end
function break_message(message, initial_length)
local max_line_length = opts['max-message-line-length']
if max_line_length <= 0 then
return message
end
local current_length = initial_length
local result = ''
if opts['message-break-anywhere'] then
local lines = {}
while #message > 0 do
local newline = message:sub(1, max_line_length)
table.insert(lines, newline)
message = message:sub(max_line_length, #message)
end
result = table.concat(lines, '\n')
else
for _,v in ipairs(split_string(message)) do
current_length = current_length + #v
if current_length > max_line_length then
result = result .. '\n' .. v
current_length = #v
else
result = result .. v
end
end
end
return result
end
function format_message(message)
local message_string = chat_message_to_string(message)
local result = nil
local lines = message_string:gmatch("([^\n]*)\n?")
for line in lines do
local formatting = '{\\an' .. opts['anchor'] .. '}'
.. '{\\fs' .. opts['font-size'] .. '}'
.. '{\\fn' .. opts['font'] .. '}'
.. '{\\bord' .. opts['border-size'] .. '}'
.. string.format(
'{\\1c&H%s&}',
swap_color_string(opts['message-color'])
)
.. string.format(
'{\\3c&H%s&}',
swap_color_string(opts['message-border-color'])
)
if message.type == SUPERCHAT then
formatting = formatting .. string.format(
'{\\1c&H%s&}{\\3c&%s&}',
swap_color_string(string.format('%06x', message.text_color)),
swap_color_string(string.format('%06x', message.border_color))
)
end
local message_string = formatting
.. line
if result == nil then
result = message_string
else
if opts['anchor'] <= 3 then
result = message_string .. '\n' .. result
else
result = result .. '\n' .. message_string
end
end
end
return result or ''
end
function chat_message_to_string(message)
if message.type == NORMAL then
if opts['show-author'] then
if opts['author-color'] == 'random' then
return string.format(
'{\\1c&H%06x&}{\\3c&H%s&}%s{\\1c&H%s&}{\\3c&H%s&}: %s',
message.author_color,
swap_color_string(opts['author-border-color']),
message.author,
swap_color_string(opts['message-color']),
swap_color_string(opts['message-border-color']),
break_message(message.contents, message.author:len() + 2)
)
elseif opts['author-color'] == 'none' then
return string.format(
'{\\3c&H%s&}%s{\\1c&H%s&}{\\3c&H%s&}: %s',
swap_color_string(opts['author-border-color']),
message.author,
swap_color_string(opts['message-color']),
swap_color_string(opts['message-border-color']),
break_message(message.contents, message.author:len() + 2)
)
else
return string.format(
'{\\1c&H%s&}{\\3c&H%s&}%s{\\1c&H%s&}{\\3c&H%s&}: %s',
swap_color_string(opts['author-color']),
swap_color_string(opts['author-border-color']),
message.author,
swap_color_string(opts['message-color']),
swap_color_string(opts['message-border-color']),
break_message(message.contents, message.author:len() + 2)
)
end
else
return break_message(message.contents, 0)
end
elseif message.type == SUPERCHAT then
if message.contents then
return string.format(
'%s %s: %s',
message.author,
message.money,
break_message(message.contents, message.author:len() + message.money:len())
)
else
return string.format(
'%s %s',
message.author,
message.money
)
end
end
end
function file_exists(name)
local f = io.open(name, "r")
if f ~= nil then
f:close()
return true
else
return false
end
end
function download_live_chat(url, filename)
if file_exists(filename) then return end
mp.command_native({
name = "subprocess",
args = {
opts['yt-dlp-path'],
'--skip-download',
'--sub-langs=live_chat',
url,
'--write-sub',
'-o',
'%(id)s',
'-P',
opts['live-chat-directory']
}
})
end
function live_chat_exists_remote(url)
local result = mp.command_native({
name = "subprocess",
capture_stdout = true,
args = { opts['yt-dlp-path'], url, '--list-subs', '--quiet' }
})
if result.status == 0 then
return string.find(result.stdout, "live_chat")
end
return false
end
-- TODO: better way to do this that gives more consistent brightness in colors?
function string_to_color(str)
local hash = 5381
for i = 1,str:len() do
hash = (33 * hash + str:byte(i)) % 16777216
end
return hash
end
function swap_color_string(str)
local r = str:sub(1, 2)
local g = str:sub(3, 4)
local b = str:sub(5, 6)
return b .. g .. r
end
function generate_messages(live_chat_json)
local result = {}
for line in io.lines(live_chat_json) do
local entry = utils.parse_json(line)
if entry.replayChatItemAction then
local time = tonumber(
entry.videoOffsetTimeMsec or
entry.replayChatItemAction.videoOffsetTimeMsec
)
for _,action in ipairs(entry.replayChatItemAction.actions) do
if action.addChatItemAction then
if action.addChatItemAction.item.liveChatTextMessageRenderer then
local liveChatTextMessageRenderer = action.addChatItemAction.item.liveChatTextMessageRenderer
local id = liveChatTextMessageRenderer.authorExternalChannelId
local color = string_to_color(id)
local author
if liveChatTextMessageRenderer.authorName then
author = liveChatTextMessageRenderer.authorName.simpleText or 'NAME_ERROR'
else
author = '-'
end
local message_data = liveChatTextMessageRenderer.message
local message = ""
for _,data in ipairs(message_data.runs) do
if data.text then
message = message .. data.text
elseif data.emoji then
if data.emoji.isCustomEmoji then
message = message .. data.emoji.shortcuts[1]
else
message = message .. data.emoji.emojiId
end
end
end
result[#result+1] = {
type = NORMAL,
author = author,
author_color = color,
contents = message,
time = time
}
elseif action.addChatItemAction.item.liveChatPaidMessageRenderer then
local liveChatPaidMessageRenderer = action.addChatItemAction.item.liveChatPaidMessageRenderer
local border_color = liveChatPaidMessageRenderer.bodyBackgroundColor - 0xff000000
local text_color = liveChatPaidMessageRenderer.bodyTextColor - 0xff000000
local money = liveChatPaidMessageRenderer.purchaseAmountText.simpleText
local author
if liveChatPaidMessageRenderer.authorName then
author = liveChatPaidMessageRenderer.authorName.simpleText or 'NAME_ERROR'
else
author = '-'
end
local message_data = liveChatPaidMessageRenderer.message
local message = ""
if message_data ~= nil then
for _,data in ipairs(message_data.runs) do
if data.text then
message = message .. data.text
elseif data.emoji then
if data.emoji.isCustomEmoji then
message = message .. data.emoji.shortcuts[1]
else
message = message .. data.emoji.emojiId
end
end
end
else
message = nil
end
result[#result+1] = {
type = SUPERCHAT,
author = author,
money = money,
border_color = border_color,
text_color = text_color,
contents = message,
time = time
}
end
end
end
end
end
return result
end
function load_live_chat(filename, interactive)
reset()
local generating_overlay = mp.create_osd_overlay("ass-events")
local path = mp.get_property_native('path')
if filename == nil then
local is_network = path:find('^http://') ~= nil or
path:find('^https://') ~= nil
if is_network then
local id = path:gsub("^.*\\?v=", ""):gsub("&.*", "")
filename = string.format(
"%s/%s.live_chat.json",
opts['live-chat-directory'],
id
)
if not file_exists(filename) then
generating_overlay.data = 'Checking for live chat on remote...'
generating_overlay:update()
if live_chat_exists_remote(path) then
generating_overlay.data = 'Downloading live chat replay...'
generating_overlay:update()
download_live_chat(path, filename)
end
end
else
local base_path = path:match('(.+)%..+$') or path
filename = base_path .. '.live_chat.json'
end
end
generating_overlay.data = 'Parsing live chat replay...'
generating_overlay:update()
if filename ~= nil and file_exists(filename) then
messages = generate_messages(filename)
else
generating_overlay:remove()
if interactive then
mp.command('show-text "Unable to find live chat replay file!"')
end
return
end
generating_overlay:remove()
if not chat_overlay then
chat_overlay = mp.create_osd_overlay("ass-events")
chat_overlay.z = -1
end
update_chat_overlay(mp.get_property_native("time-pos"))
end
function _load_live_chat(_, filename)
load_live_chat(filename)
end
function update_chat_overlay(time)
if chat_hidden or chat_overlay == nil or messages == nil or time == nil then
return
end
local msec = time * 1000
chat_overlay.data = ''
for i=1,#messages do
local message = messages[i]
if message.time > msec then
break
elseif msec <= message.time + opts['message-duration'] then
local message_string = format_message(message)
if opts['anchor'] <= 3 then
chat_overlay.data = message_string
.. '\n'
.. '{\\fscy' .. opts['message-gap'] .. '}{\\fscx0}\\h{\fscy\fscx}'
.. chat_overlay.data
else
chat_overlay.data = chat_overlay.data
.. '{\\fscy' .. opts['message-gap'] .. '}{\\fscx0}\\h{\fscy\fscx}'
.. '\n'
.. message_string
end
end
end
chat_overlay:update()
end
function _update_chat_overlay(_, time)
update_chat_overlay(time)
end
function load_live_chat_interactive(filename)
load_live_chat(filename, true)
end
function set_chat_hidden(state)
if state == nil then
chat_hidden = not chat_hidden
else
chat_hidden = state == 'yes'
end
if chat_overlay ~= nil then
if chat_hidden then
mp.command('show-text "Youtube chat replay hidden"')
chat_overlay:remove()
else
mp.command('show-text "Youtube chat replay unhidden"')
update_chat_overlay(mp.get_property_native("time-pos"))
end
end
end
function set_chat_anchor(anchor)
if anchor == nil then
opts['anchor'] = (opts['anchor'] % 9) + 1
else
opts['anchor'] = tonumber(anchor)
end
if chat_overlay then
update_chat_overlay(mp.get_property_native("time-pos"))
end
end
function set_break_anywhere(state)
if state == nil then
opts['message-break-anywhere'] = not opts['message-break-anywhere']
else
opts['message-break-anywhere'] = state == 'yes'
end
if chat_overlay then
update_chat_overlay(mp.get_property_native("time-pos"))
end
end
function reset()
messages = nil
if chat_overlay then
chat_overlay:remove()
end
chat_overlay = nil
end
mp.add_key_binding(nil, "load-chat", load_live_chat_interactive)
mp.add_key_binding(nil, "unload-chat", reset)
mp.add_key_binding(nil, "chat-hidden", set_chat_hidden)
mp.add_key_binding(nil, "chat-anchor", set_chat_anchor)
mp.add_key_binding(nil, "break-anywhere", set_break_anywhere)
if opts['auto-load'] then
mp.register_event("file-loaded", _load_live_chat)
end
mp.observe_property("time-pos", "native", _update_chat_overlay)
mp.register_event("end-file", reset)