-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.lua
176 lines (162 loc) · 5.03 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
-- assert lua script
-- ===================|
-- note to escape path for winodws (c:\\users\\user\\...)
local utils = require 'mp.utils'
local danmu_file = nil
local sec_sub_visibility = mp.get_property_native("secondary-sub-visibility")
local sec_sub_ass_override = mp.get_property_native("secondary-sub-ass-override")
local function get_cid()
local cid, danmaku_id = nil, nil
local tracks = mp.get_property_native("track-list")
for _, track in ipairs(tracks) do
if track["lang"] == "danmaku" then
cid = track["external-filename"]:match("/(%d-)%.xml$")
danmaku_id = track["id"]
break
end
end
return cid, danmaku_id
end
local function get_sub_count()
local count = 0
local tracks = mp.get_property_native("track-list")
for _, track in ipairs(tracks) do
if track["type"] == "sub" then
count = count + 1
end
end
return count
end
local function file_exists(path)
if path then
local meta = utils.file_info(path)
return meta.is_file
else
return false
end
end
-- Log function: log to both terminal and MPV OSD (On-Screen Display)
local function log(string,secs)
secs = secs or 2.5
mp.msg.warn(string)
mp.osd_message(string,secs)
end
-- load function
local function load_danmu(danmu_file)
if not file_exists(danmu_file) then return end
log('开火')
-- 如果可用将弹幕挂载为次字幕
if sec_sub_ass_override then
mp.commandv("sub-add", danmu_file, "auto")
local sub_count = get_sub_count()
mp.set_property_native("secondary-sub-ass-override", "yes")
mp.set_property_native("secondary-sid", sub_count)
mp.set_property_native("secondary-sub-visibility", true)
else
-- 挂载subtitles滤镜,注意加上@标签,这样即使多次调用也不会重复挂载,以最后一次为准
mp.commandv('vf', 'append', '@danmu:subtitles=filename="'..danmu_file..'"')
-- 只能在软解或auto-copy硬解下生效,统一改为auto-copy硬解
mp.set_property('hwdec', 'auto-copy')
end
end
-- download function
local function assprocess()
local path = mp.get_property("path")
if path and not path:find('^%a[%w.+-]-://') and not (path:find('bilibili.com') or path:find('bilivideo.com'))
then return end
-- get video cid
local cid = mp.get_opt('cid')
if cid == nil and path and path:find('^%a[%w.+-]-://') then
cid, danmaku_id = get_cid()
if danmaku_id ~= nil then
mp.commandv('sub-remove', danmaku_id)
end
end
if cid == nil then return end
local python_path = 'python' -- path to python bin
-- get script directory
local directory = mp.get_script_directory()
local py_path = ''..directory..'/Danmu2Ass.py'
-- under windows platform, convert path format
if string.find(directory, "\\")
then
string.gsub(directory, "/", "\\")
py_path = ''..directory..'\\Danmu2Ass.py'
end
local dw = mp.get_property_osd('display-width')
local dh = mp.get_property_osd('display-height')
local dw = mp.get_property_number('display-width', 1920)
local dh = mp.get_property_number('display-height', 1080)
local aspect = mp.get_property_number('width', 16) / mp.get_property_number('height', 9)
if aspect > dw / dh then
dh = math.floor(dw / aspect)
elseif aspect < dw / dh then
dw = math.floor(dh * aspect)
end
-- 保留底部多少高度的空白区域 (默认0, 取值0.0-1.0)
local percent = 0.75
-- choose to use python or .exe
local arg = { 'python', py_path, '-d', directory,
-- 设置屏幕分辨率 (自动取值)
'-s', ''..dw..'x'..dh,
-- 设置字体大小 (默认 37.0)
'-fs', '37.0',
-- 设置弹幕不透明度 (默认 0.95)
'-a', '0.95',
-- 滚动弹幕显示的持续时间 (默认 10秒)
'-dm', '10.0',
-- 静止弹幕显示的持续时间 (默认 5秒)
'-ds', '5.0',
'-p', tostring(math.floor(percent*dh)),
'-r',
cid,
}
-- local arg = { ''..directory..'\\Danmu2Ass.exe', '-d', directory, cid}
log('弹幕正在上膛')
-- run python to get comments
mp.command_native_async({
name = 'subprocess',
playback_only = false,
capture_stdout = true,
args = arg,
capture_stdout = true
},function(res, val, err)
if err == nil
then
danmu_file = ''..directory..'/bilibili.ass'
load_danmu(danmu_file)
else
log(err)
end
end)
end
-- toggle function
function asstoggle()
if not file_exists(danmu_file) then return end
local sec_visibility = mp.get_property_bool("secondary-sub-visibility")
if sec_sub_ass_override and sec_visibility then
log('停火')
mp.set_property_native("secondary-sub-visibility", false)
return
elseif sec_sub_ass_override == nil then
-- if exists @danmu filter, remove it
for _, f in ipairs(mp.get_property_native('vf')) do
if f.label == 'danmu' then
log('停火')
mp.commandv('vf', 'remove', '@danmu')
return
end
end
end
-- otherwise, load danmu
if file_exists(danmu_file) then load_danmu(danmu_file) end
end
mp.add_key_binding('b', 'toggle', asstoggle)
mp.register_event("file-loaded", assprocess)
mp.register_event("end-file", function()
asstoggle()
if sec_sub_ass_override then
mp.set_property_native("secondary-sub-visibility", sec_sub_visibility)
mp.set_property_native("secondary-sub-ass-override", sec_sub_ass_override)
end
end)