forked from UlyssesZh/bdanmaku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdanmaku.lua
94 lines (88 loc) · 3.02 KB
/
bdanmaku.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
-- Author: UlyssesZhan <[email protected]>
-- License: MIT
-- Homepage: https://github.com/UlyssesZh/bdanmaku
table.unpack = table.unpack or unpack -- 5.1 compatibility
local CURL = mp.get_opt('curl_executable') or 'curl'
local BILIASS = mp.get_opt('biliass_executable') or 'biliass'
local TMPDIR = mp.get_opt('tmpdir') or '/tmp/danmaku'
local BILIASS_OPTS = {}
for token in (mp.get_opt('biliass_options') or '--fontsize 48 --alpha 0.5 --duration-marquee 10'):gmatch('[^%s]+') do
BILIASS_OPTS[#BILIASS_OPTS + 1] = token
end
local utils = require 'mp.utils'
local danmaku_track_id = nil
local xml_filename = nil
function download_xml()
local url = nil
for track_i = 0, mp.get_property('track-list/count') - 1 do
url = mp.get_property('track-list/'..track_i..'/external-filename')
if url then
url = url:match '%w+://comment.bilibili.com/.*%.xml$'
if url then
danmaku_track_id = track_i
break
end
end
end
if not danmaku_track_id then
mp.msg.debug('no XML danmaku found')
return
end
if os.execute('mkdir -p '..TMPDIR) ~= 0 then
os.execute('powershell mkdir '..TMPDIR)
end
xml_filename = TMPDIR..'/'..mp.get_property('pid')..'.xml'
local curl_args = {
CURL, url,
'--silent',
'--output', xml_filename,
'--compressed'
}
mp.msg.debug('curl_command: '..table.concat(curl_args, ' '))
local curl_result = utils.subprocess({args = curl_args})
if curl_result.status == 0 then
mp.msg.debug('danmaku downloaded, will convert to ASS')
else
xml_filename = nil
mp.msg.warn('downloading XML danmaku from '..url..' failed: '..curl_result.error)
end
end
function replace_sub()
local width, height, par = mp.get_osd_size()
if width == 0 or height == 0 or not xml_filename then
return
end
local resolution = width..'x'..height
local ass_filename = TMPDIR..'/'..mp.get_property('pid')..'.ass'
local biliass_args = {
BILIASS, xml_filename,
'--size', resolution,
'--output', ass_filename,
table.unpack(BILIASS_OPTS)
}
mp.msg.debug('biliass_command: '..table.concat(biliass_args, ' '))
local biliass_result = utils.subprocess({args = biliass_args})
if biliass_result.status == 0 then
local sid = mp.get_property('track-list/'..danmaku_track_id..'/id')
mp.msg.debug('deleting original subtitle sid='..sid)
mp.commandv('sub-remove', sid)
mp.msg.debug('adding new subtitle')
mp.commandv('sub-add', ass_filename, 'select', 'danmaku', 'danmaku')
for track_i = 0, mp.get_property('track-list/count') - 1 do
if mp.get_property('track-list/'..track_i..'/external-filename') == ass_filename then
danmaku_track_id = track_i
break
end
end
else
mp.msg.warn('converting XML danmaku from '..xml_filename..' to '..ass_filename..' failed: '..biliass_result.error)
end
end
function shutdown_handler()
os.remove(TMPDIR..'/'..mp.get_property('pid')..'.xml')
os.remove(TMPDIR..'/'..mp.get_property('pid')..'.ass')
end
mp.register_event('file-loaded', download_xml)
mp.observe_property('osd-width', nil, replace_sub)
mp.observe_property('osd-height', nil, replace_sub)
mp.register_event("shutdown", shutdown_handler)