-
Notifications
You must be signed in to change notification settings - Fork 4
/
danmaku.lua
615 lines (567 loc) · 19.4 KB
/
danmaku.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
local utils = require('mp.utils')
-- modified from https://bitop.luajit.org/download.html (LuaBitOp-1.0.2 / md5test.lua)
-- and https://github.com/kikito/md5.lua/blob/master/md5.lua
local byte, char, rep = string.byte, string.char, string.rep
local tobit, tohex, bnot, bor, band, bxor, lshift, rshift, rol, bswap
local ok, bit = pcall(require, 'bit')
if ok then
tobit, tohex = bit.tobit, bit.tohex
bnot, bor, band, bxor, lshift, rshift = bit.bnot, bit.bor, bit.band, bit.bxor, bit.lshift, bit.rshift
rol, bswap = bit.rol, bit.bswap
else
local bit = require('bit32')
local bit32_bnot = bit.bnot
tobit = function(a) return a <= 0x7fffffff and a or -(bit32_bnot(a) + 1) end
bnot = function(a) return tobit(bit32_bnot(tobit(a))) end
bor, band, bxor, lshift, rshift, rol = bit.bor, bit.band, bit.bxor, bit.lshift, bit.rshift, bit.lrotate
tohex = function(a) return string.sub(string.format('%08x', a), -8) end
bswap = function(a)
return bor(rshift(a, 24), band(rshift(a, 8), 0xff00), lshift(band(a, 0xff00), 8), lshift(a, 24))
end
end
local function tr_f(a, b, c, d, x, s) return rol(bxor(d, band(b, bxor(c, d))) + a + x, s) + b end
local function tr_g(a, b, c, d, x, s) return rol(bxor(c, band(d, bxor(b, c))) + a + x, s) + b end
local function tr_h(a, b, c, d, x, s) return rol(bxor(b, c, d) + a + x, s) + b end
local function tr_i(a, b, c, d, x, s) return rol(bxor(c, bor(b, bnot(d))) + a + x, s) + b end
local function transform(x, a1, b1, c1, d1)
local a, b, c, d = a1, b1, c1, d1
a = tr_f(a, b, c, d, x[1] + 0xd76aa478, 7)
d = tr_f(d, a, b, c, x[2] + 0xe8c7b756, 12)
c = tr_f(c, d, a, b, x[3] + 0x242070db, 17)
b = tr_f(b, c, d, a, x[4] + 0xc1bdceee, 22)
a = tr_f(a, b, c, d, x[5] + 0xf57c0faf, 7)
d = tr_f(d, a, b, c, x[6] + 0x4787c62a, 12)
c = tr_f(c, d, a, b, x[7] + 0xa8304613, 17)
b = tr_f(b, c, d, a, x[8] + 0xfd469501, 22)
a = tr_f(a, b, c, d, x[9] + 0x698098d8, 7)
d = tr_f(d, a, b, c, x[10] + 0x8b44f7af, 12)
c = tr_f(c, d, a, b, x[11] + 0xffff5bb1, 17)
b = tr_f(b, c, d, a, x[12] + 0x895cd7be, 22)
a = tr_f(a, b, c, d, x[13] + 0x6b901122, 7)
d = tr_f(d, a, b, c, x[14] + 0xfd987193, 12)
c = tr_f(c, d, a, b, x[15] + 0xa679438e, 17)
b = tr_f(b, c, d, a, x[16] + 0x49b40821, 22)
a = tr_g(a, b, c, d, x[2] + 0xf61e2562, 5)
d = tr_g(d, a, b, c, x[7] + 0xc040b340, 9)
c = tr_g(c, d, a, b, x[12] + 0x265e5a51, 14)
b = tr_g(b, c, d, a, x[1] + 0xe9b6c7aa, 20)
a = tr_g(a, b, c, d, x[6] + 0xd62f105d, 5)
d = tr_g(d, a, b, c, x[11] + 0x02441453, 9)
c = tr_g(c, d, a, b, x[16] + 0xd8a1e681, 14)
b = tr_g(b, c, d, a, x[5] + 0xe7d3fbc8, 20)
a = tr_g(a, b, c, d, x[10] + 0x21e1cde6, 5)
d = tr_g(d, a, b, c, x[15] + 0xc33707d6, 9)
c = tr_g(c, d, a, b, x[4] + 0xf4d50d87, 14)
b = tr_g(b, c, d, a, x[9] + 0x455a14ed, 20)
a = tr_g(a, b, c, d, x[14] + 0xa9e3e905, 5)
d = tr_g(d, a, b, c, x[3] + 0xfcefa3f8, 9)
c = tr_g(c, d, a, b, x[8] + 0x676f02d9, 14)
b = tr_g(b, c, d, a, x[13] + 0x8d2a4c8a, 20)
a = tr_h(a, b, c, d, x[6] + 0xfffa3942, 4)
d = tr_h(d, a, b, c, x[9] + 0x8771f681, 11)
c = tr_h(c, d, a, b, x[12] + 0x6d9d6122, 16)
b = tr_h(b, c, d, a, x[15] + 0xfde5380c, 23)
a = tr_h(a, b, c, d, x[2] + 0xa4beea44, 4)
d = tr_h(d, a, b, c, x[5] + 0x4bdecfa9, 11)
c = tr_h(c, d, a, b, x[8] + 0xf6bb4b60, 16)
b = tr_h(b, c, d, a, x[11] + 0xbebfbc70, 23)
a = tr_h(a, b, c, d, x[14] + 0x289b7ec6, 4)
d = tr_h(d, a, b, c, x[1] + 0xeaa127fa, 11)
c = tr_h(c, d, a, b, x[4] + 0xd4ef3085, 16)
b = tr_h(b, c, d, a, x[7] + 0x04881d05, 23)
a = tr_h(a, b, c, d, x[10] + 0xd9d4d039, 4)
d = tr_h(d, a, b, c, x[13] + 0xe6db99e5, 11)
c = tr_h(c, d, a, b, x[16] + 0x1fa27cf8, 16)
b = tr_h(b, c, d, a, x[3] + 0xc4ac5665, 23)
a = tr_i(a, b, c, d, x[1] + 0xf4292244, 6)
d = tr_i(d, a, b, c, x[8] + 0x432aff97, 10)
c = tr_i(c, d, a, b, x[15] + 0xab9423a7, 15)
b = tr_i(b, c, d, a, x[6] + 0xfc93a039, 21)
a = tr_i(a, b, c, d, x[13] + 0x655b59c3, 6)
d = tr_i(d, a, b, c, x[4] + 0x8f0ccc92, 10)
c = tr_i(c, d, a, b, x[11] + 0xffeff47d, 15)
b = tr_i(b, c, d, a, x[2] + 0x85845dd1, 21)
a = tr_i(a, b, c, d, x[9] + 0x6fa87e4f, 6)
d = tr_i(d, a, b, c, x[16] + 0xfe2ce6e0, 10)
c = tr_i(c, d, a, b, x[7] + 0xa3014314, 15)
b = tr_i(b, c, d, a, x[14] + 0x4e0811a1, 21)
a = tr_i(a, b, c, d, x[5] + 0xf7537e82, 6)
d = tr_i(d, a, b, c, x[12] + 0xbd3af235, 10)
c = tr_i(c, d, a, b, x[3] + 0x2ad7d2bb, 15)
b = tr_i(b, c, d, a, x[10] + 0xeb86d391, 21)
return tobit(a + a1), tobit(b + b1), tobit(c + c1), tobit(d + d1)
end
-- assert(#s % 4 == 0)
local function md5_update(self, s)
local a, b, c, d = self.a, self.b, self.c, self.d
local x, k = self.x, self.k
for i = 1, #s, 4 do
local m0, m1, m2, m3 = byte(s, i, i + 3)
x[k] = bor(m0, lshift(m1, 8), lshift(m2, 16), lshift(m3, 24))
if k == 16 then
a, b, c, d = transform(x, a, b, c, d)
k = 1
else
k = k + 1
end
end
self.a, self.b, self.c, self.d, self.k = a, b, c, d, k
self.len = self.len + #s
return self
end
local function md5_finish(self)
local len = self.len
local s = '\128' .. rep('\0', 63 - band(len + 8, 63)) ..
char(band(lshift(len, 3), 255), band(rshift(len, 5), 255), band(rshift(len, 13), 255),
band(rshift(len, 21), 255)) .. '\0\0\0\0'
md5_update(self, s)
return tohex(bswap(self.a)) .. tohex(bswap(self.b)) .. tohex(bswap(self.c)) .. tohex(bswap(self.d))
end
local md5 = {}
function md5.new()
return {
a = 0x67452301,
b = 0xefcdab89,
c = 0x98badcfe,
d = 0x10325476,
x = {},
k = 1,
len = 0,
update = md5_update,
finish = md5_finish,
}
end
-- utils
local function split_by_comma(s)
local list = {}
local start = 1
while true do
local i, j = s:find(',', start, true)
if i == nil then
break
end
table.insert(list, s:sub(start, i - 1))
start = j + 1
end
table.insert(list, s:sub(start))
return list
end
local function dup(list)
local dup = {}
for _, elem in ipairs(list) do
table.insert(dup, elem)
end
return dup
end
local function trim(s) return string.gsub(s, '^%s*(.-)%s*$', '%1') end
-- https://api.dandanplay.net/swagger/ui/index
local MAX_DURATION = 12
local INTERVAL = 0.005
local MIN_STEP = INTERVAL / MAX_DURATION
local MAX_STEP = MIN_STEP * 1.3
local enabled, danmaku, filter, filter_source
local speed, osd_width, osd_height, delay, pause = 0, 0, 0, 0, true
local overlay = mp.create_osd_overlay('ass-events')
local opts = {
font_size = 40,
transparency = 0x30,
reserved_space = 0,
speed = 1,
no_overlap = true,
filter = '',
filter_source = '',
filter_bilibili = '',
proxy = '',
}
local function render()
local pos, err = mp.get_property_number('time-pos')
if err ~= nil then
return mp.msg.error(err)
end
local width, height = 1920, 1080
local ratio = osd_width / osd_height
if width / height < ratio then
height = width / ratio
elseif width / height > ratio then
width = height * ratio
end
local spacing = opts.font_size / 10
local rows = {}
for _ = 1, math.max(1, height * (1 - opts.reserved_space) / (opts.font_size + spacing)) do
table.insert(rows, {
ending = 0,
step = MIN_STEP,
})
end
local comments = {}
for _, c in ipairs(danmaku) do
if c.blocked then
goto continue
end
local time = c.time + delay
if time > pos then
break
end
if c.status == nil then
local ticks = (pos - time) / INTERVAL
for i, row in ipairs(rows) do
if row.ending < width - width * ticks * MIN_STEP then
local max_step = MAX_STEP
if row.ending ~= 0 then
-- 1 / max_step - ticks = row.ending / width / row.step
max_step = math.min(MAX_STEP, 1 / (ticks + row.ending / width / row.step))
end
local step = math.random()
step = MIN_STEP + (max_step - MIN_STEP) * step
c.status = {
row = i,
step = step,
x = width - width * ticks * step,
}
break
end
end
if c.status == nil then
if opts.no_overlap then
c.status = false
goto continue
end
local min = 1
for i, row in ipairs(rows) do
if row.ending < rows[min].ending then
min = i
end
end
c.status = {
row = min,
step = MIN_STEP,
x = width - width * ticks * MIN_STEP,
}
end
elseif not c.status then
goto continue
end
if c.status.x + #c.message * opts.font_size + spacing <= 0 then
goto continue
end
table.insert(comments,
string.format('{\\pos(%f,%f)\\c&H%x%x%x&\\alpha&H%x\\fs%f\\bord1.5\\shad0\\b1\\q2}%s', c.status.x,
(c.status.row - 1) * (opts.font_size + spacing), c.b, c.g, c.r, opts.transparency, opts.font_size,
c.message))
c.status.x = c.status.x - width * c.status.step * speed * opts.speed
local row = rows[c.status.row]
if row ~= nil then
local ending = c.status.x + #c.message * opts.font_size + spacing
if ending / c.status.step > row.ending / row.step then
rows[c.status.row] = {
ending = ending,
step = c.status.step,
}
end
end
::continue::
end
overlay.res_x = width
overlay.res_y = height
overlay.data = table.concat(comments, '\n')
overlay:update()
end
local function is_blocked(message, source)
if filter_source[source] then
return true
end
for _, keyword in ipairs(filter) do
if message:find(keyword, 1, true) then
return true
end
end
end
local function update_filter(on_update)
local need_update = false
if on_update == nil or on_update.filter or on_update.filter_bilibili then
need_update = true
if #opts.filter > 0 then
filter = split_by_comma(opts.filter)
else
filter = {}
end
end
if on_update == nil or on_update.filter_source then
need_update = true
filter_source = {}
if #opts.filter_source > 0 then
for _, source in ipairs(split_by_comma(opts.filter_source)) do
filter_source[source:lower()] = true
end
end
end
if on_update == nil or on_update.filter_bilibili then
need_update = true
if #opts.filter_bilibili > 0 then
local path, error = mp.command_native({ 'expand-path', opts.filter_bilibili })
if error ~= nil then
mp.msg.error(error)
goto skip
end
local file, error = io.open(path, 'rb')
if error ~= nil then
mp.msg.error(error)
goto skip
end
local content = file:read('*a')
file:close()
local rules = utils.parse_json(content)
if rules == nil then
goto skip
end
for _, rule in ipairs(rules) do
if rule.type == 0 and rule.opened then
table.insert(filter, rule.filter)
end
end
end
end
::skip::
if need_update and danmaku ~= nil then
for _, c in ipairs(danmaku) do
c.blocked = is_blocked(c.message, c.source)
c.status = nil
end
if enabled then
render()
end
end
end
require('mp.options').read_options(opts, nil, update_filter)
update_filter()
local curl_args = { 'curl', '-sSfL', '--connect-timeout', '10', '-H', 'User-Agent: danmaku/mpv' }
if #opts.proxy > 0 then
table.insert(curl_args, '-x')
table.insert(curl_args, opts.proxy)
end
local function show_error(error)
mp.msg.error(error)
if enabled then
mp.osd_message('Danmaku: ' .. error)
end
end
local function show_loaded()
local n, b = 0, 0
for _, c in ipairs(danmaku) do
if c.blocked then
b = b + 1
else
n = n + 1
end
end
mp.osd_message('Loaded ' .. n .. ' danmaku comments, blocked ' .. b)
end
local function reset_status()
for _, c in ipairs(danmaku) do
c.status = nil
end
end
local timer = mp.add_periodic_timer(INTERVAL, render, true)
local function get_danmaku()
local path, error = mp.get_property('path')
if error ~= nil then
return show_error(error)
end
local file, error = io.open(path, 'rb')
if error ~= nil then
return show_error(error)
end
local m = md5.new()
for _ = 1, 16 * 1024 do
local content = file:read(1024)
if content == nil then
file:close()
return show_error('fail to read')
end
m:update(content)
end
file:close()
local hash = m:finish()
mp.msg.info('hash:', hash)
local _, name = utils.split_path(path)
local body, error = utils.format_json({
fileName = name,
fileHash = hash,
})
if error ~= nil then
return show_error(error)
end
local args = dup(curl_args)
local url = 'https://api.dandanplay.net/api/v2/match'
table.insert(args, '-X')
table.insert(args, 'POST')
table.insert(args, '--data-raw')
table.insert(args, body)
table.insert(args, '-H')
table.insert(args, 'Content-Type: application/json')
table.insert(args, url)
mp.msg.info('post', url)
mp.command_native_async({
name = 'subprocess',
args = args,
capture_stdout = true,
capture_stderr = true,
}, function(success, result, error)
if not success then
return show_error(error)
elseif result.status ~= 0 then
if not result.killed_by_us then
show_error(trim(result.stderr))
end
return
end
local match, error = utils.parse_json(result.stdout)
if error ~= nil then
return show_error('fail to parse json')
elseif #match.matches > 1 then
return show_error('multiple matching episodes')
elseif not match.isMatched then
return show_error('no matching episode')
elseif not enabled then
return
end
local args = dup(curl_args)
local url = 'https://api.dandanplay.net/api/v2/comment/' .. match.matches[1].episodeId .. '?withRelated=true'
table.insert(args, url)
if mp.get_property('path') ~= path then
return
end
mp.msg.info('get', url)
result, error = mp.command_native_async({
name = 'subprocess',
args = args,
capture_stdout = true,
capture_stderr = true,
}, function(success, result, error)
if not success then
return show_error(error)
elseif result.status ~= 0 then
if not result.killed_by_us then
show_error(trim(result.stderr))
end
return
end
local comments, error = utils.parse_json(result.stdout)
if error ~= nil then
return show_error('fail to parse json')
end
comments = comments.comments
mp.msg.info('get ' .. #comments .. ' danmaku comments')
for i, c in ipairs(comments) do
local p = split_by_comma(c.p)
local color = tonumber(p[3])
local user = p[4]
local source = user:match('^%d+$') and 'dandan' or user:match('^%[(.-)%]')
if source ~= nil then
source = source:lower()
end
local message, error = mp.command_native({ 'escape-ass', c.m })
if error ~= nil then
mp.msg.error(error)
message = c.m
end
comments[i] = {
message = message,
time = tonumber(p[1]),
r = color / (256 * 256),
g = color % (256 * 256) / 256,
b = color % 256,
source = source,
blocked = is_blocked(c.m, source),
}
end
table.sort(comments, function(a, b) return a.time < b.time end)
if mp.get_property('path') ~= path then
return
end
danmaku = comments
if enabled then
render()
if not pause then
timer:resume()
end
show_loaded()
end
end)
end)
end
mp.observe_property('speed', 'number', function(_, value) speed = value or speed end)
mp.observe_property('osd-width', 'number', function(_, value) osd_width = value or osd_width end)
mp.observe_property('osd-height', 'number', function(_, value) osd_height = value or osd_height end)
mp.observe_property('pause', 'bool', function(_, value)
if value ~= nil then
pause = value
end
if enabled then
if pause then
timer:kill()
elseif danmaku ~= nil then
timer:resume()
end
end
end)
mp.register_event('file-loaded', function(event)
if event.error then
return mp.msg.error(event.error)
end
danmaku, delay = nil, 0
if enabled then
timer:kill()
overlay:remove()
get_danmaku()
end
end)
mp.register_event('playback-restart', function(event)
if event.error then
return mp.msg.error(event.error)
end
if enabled and danmaku ~= nil then
reset_status()
render()
end
end)
mp.register_event('client-message', function(event)
if event.error then
return mp.msg.error(event.error)
end
if event.args[1] == 'toggle-danmaku' then
enabled = not enabled
mp.msg.info('enabled:', enabled)
if enabled then
if danmaku == nil then
mp.osd_message('Danmaku: on')
get_danmaku()
else
reset_status()
render()
show_loaded()
if not pause then
timer:resume()
end
end
else
timer:kill()
overlay:remove()
mp.osd_message('Danmaku: off')
end
elseif event.args[1] == 'danmaku-delay' then
if #event.args < 2 then
return mp.msg.error('command danmaku-delay: required argument seconds not set')
end
local value = tonumber(event.args[2])
if value == nil then
return mp.msg.error('command danmaku-delay: invalid time')
end
delay = delay + value
if enabled and danmaku ~= nil then
reset_status()
render()
end
mp.osd_message('Danmaku delay: ' .. (delay * 1000) .. ' ms')
end
end)