This repository was archived by the owner on May 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiliDanmaku.py
More file actions
executable file
·395 lines (334 loc) · 12.9 KB
/
BiliDanmaku.py
File metadata and controls
executable file
·395 lines (334 loc) · 12.9 KB
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
# -*- coding: utf-8 -*-
# Copyright 2013 CNA_Bld @ SSHZ.ORG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
import unicodedata
import sys
import codecs
if sys.platform == 'win32':
win = True
else:
win = False
VERSION = "V0.1"
RESX = 512
RESY = 384
LINE_SPACE = 2
CHAR_SPACE = 2
OUTPUT_HEAD = '''
[Script Info]
Title: BiliStarLocalDanmakuHandler
Original Script: 嗶哩嗶哩 - ( ゜- ゜)つロ 乾杯~
Script Updated By: BiliStar_BiliDanmakuModule ''' + VERSION + '''
ScriptType: v4.00+
Collisions: Normal
PlayResX: ''' + str(RESX) + '''
PlayResY: ''' + str(RESY) + '''
PlayDepth: 32
Timer: 100.0000
WrapStyle: 2
[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Medium, 黑体,25,11861244,11861244,0,-2147483640,-1,0,0,0,100,100,1,0.00,1,1,0,2,30,30,30,1
Style: Big, 黑体,36,11861244,11861244,0,-2147483640,-1,0,0,0,100,100,2,0.00,1,1,0,2,30,30,30,1
Style: Small, 黑体,18,11861244,11861244,0,-2147483640,-1,0,0,0,100,100,1,0.00,1,1,0,2,30,30,30,1
Style: Compact, 黑体,18,11861244,11861244,0,-2147483640,-1,0,0,0,100,100,1,0.00,1,1,0,2,30,30,30,1
Style: BackG, 黑体,18,11861244,11861244,0,-2147483640,-1,0,0,0,100,100,1,0.00,1,1,0,2,30,30,30,1
Style: Default, 黑体,18,11861244,11861244,0,-2147483640,-1,0,0,0,100,100,1,0.00,1,1,0,10,30,30,30,1
[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
'''
# ========================================
def chr_width(c):
"""
返回字符显示宽度
"""
if unicodedata.east_asian_width(c) in ('F', 'W', 'A'):
return 2
else:
return 1
def win_encode(ori_str):
"""
Windows 平台强制转换为 Unicode
"""
if win:
try:
ori_str = unicode(ori_str, 'utf-8')
except:
pass # 万一它本来就是 Unicode 字符串直接返回
return ori_str
def win_repair():
"""
Windows 平台统一修复函数
"""
global OUTPUT_HEAD # 下面那句调用它本体了不得不设成 Global
OUTPUT_HEAD = win_encode(OUTPUT_HEAD)
# ========================================
def compare_danmaku(danmaku_a, danmaku_b):
"""
比较弹幕时间,danmaku_a 较小或相等返回-1,否则1
"""
if danmaku_a['oritime'] <= danmaku_b['oritime']:
return -1
else:
return 1
def get_danmaku_size(orisize):
"""
弹幕字体大小转换函数
输入: 字符串型字体大小
返回: 字符串型SSA用字体大小参数
"""
if orisize == '18':
return 'Small'
elif orisize == '25':
return 'Medium'
elif orisize == '36':
return 'Big'
else:
return 'Medium' # 其它字号全部按 Medium 处理
def get_danmaku_time(oritime):
"""
弹幕时间参数转换函数
输入: 字符串型弹幕时间
返回: 字符串型SSA用时间参数,格式『开始,结束』
"""
if float(oritime) >= 0:
fractional_part = oritime[oritime.find('.') + 1:oritime.find('.') + 3]
start_time = time.strftime('%H:%M:%S', time.gmtime(float(oritime)))
end_time = time.strftime('%H:%M:%S', time.gmtime(float(oritime) + 4))
return start_time + '.' + fractional_part + ',' + end_time + '.' + fractional_part
else:
return '0:00:00.-20,0:00:03.79' # 在开始播放前发送弹幕则记录弹幕时间为0.-20
def str_length(string, size):
"""
字符串宽度计算函数
输入: 字符串, 字号
返回: 整型
"""
length = 0
for char in string:
length += chr_width(char)
length = length * size / 2
length += CHAR_SPACE * (len(string) - 1)
return length
# ========================================
def xml_to_danmaku_dict(original_xml):
"""
单行弹幕转换字典函数
输入: 原始XML格式单行弹幕
返回: 字典类型单行弹幕
字典定义: {'time':SSA格式时间参数, 'oritime':浮点型原始时间, 'mode':弹幕模式, 'size':SSA风格弹幕尺寸, 'orisize':字符串类型原始尺寸, 'color':SSA标准型颜色参数, 'content': 弹幕内容}
"""
result = {}
result['oritime'] = float(original_xml[original_xml.find('"') + 1:original_xml.find(',')])
result['time'] = get_danmaku_time(original_xml[original_xml.find('"') + 1:original_xml.find(',')])
original_xml = original_xml[original_xml.find(',') + 1:]
result['mode'] = original_xml[0]
original_xml = original_xml[2:]
result['orisize'] = original_xml[0:original_xml.find(',')]
result['size'] = get_danmaku_size(original_xml[0:original_xml.find(',')])
original_xml = original_xml[original_xml.find(',') + 1:]
result['color'] = hex(int(original_xml[0:original_xml.find(',')]))[2:].upper()
result['content'] = original_xml[original_xml.find('>') + 1:original_xml.find('<')]
return result
def parse_xml(xml_data):
"""
主XML解析函数
输入: 原始XML格式字符串型完整弹幕
返回: 列表式字典类型弹幕
"""
xml_data = win_encode(xml_data)
danmaku_list = []
while xml_data.find('<d p=') != -1:
danmaku_list.append(xml_to_danmaku_dict(xml_data[xml_data.find('<d p='):xml_data.find('</d>') + 4]))
xml_data = xml_data[xml_data.find('</d>') + 5:]
danmaku_list.sort(compare_danmaku)
return danmaku_list
# ========================================
class ScreenController():
"""
大工程 将ScreenControl事务打包成为一个类
type属性控制类型。False为上到下,True下到上【内部存储倒过来存,返回定位参数时再倒过来输出就好= =】
"""
def init_screen(self):
"""
初始化自身
"""
self.status = [0.] * (RESY + 1)
self.status[0] = -1.
return
def remove_left_danmaku(self, current_time):
"""
清除已离开控制区的弹幕
输入: 当前时间
"""
for point in range(1, RESY + 1):
if self.status[point] < current_time:
self.status[point] = 0.
return
def check_for_space(self, position, size):
"""
检测该空间能否放下目标弹幕
输入: 弹幕起始坐标, 弹幕大小
返回: T/F
"""
if position + size - 1 > RESY: # 超出屏幕高度
return False
else:
for point in range(position, position + size):
if self.status[point] != 0.:
return False
return True
def add_danmaku(self, size, current_time, life_period=4):
"""
增添作用中弹幕。
输入: 整型弹幕高度, 当前时间, 占用时间秒
返回: SSA用定位参数
"""
self.remove_left_danmaku(current_time)
flag = False
for start_position in range(LINE_SPACE + 1, RESY):
if (self.status[start_position] == 0.) and (self.check_for_space(start_position, size)):
flag = True
break
if not flag: # 无有效空间
self.init_screen()
start_position = 1
for point in range(start_position, start_position + size + LINE_SPACE):
self.status[point] = current_time + life_period
if not self.type:
return start_position + size - 1
else:
return RESY - start_position + 1
def __init__(self, screen_type=False):
self.init_screen()
self.type = screen_type
return
def parse_top_danmaku(danmaku_list):
"""
顶部弹幕解析函数
输入: 列表式字典类型顶部弹幕
返回: 顶部弹幕SSA列表
"""
screen = ScreenController(False)
ssa_list = []
for danmaku in danmaku_list:
position = screen.add_danmaku(int(danmaku['orisize']), danmaku['oritime'], 4)
ssa = {'ssa': u'Dialogue: 5,' + danmaku['time'] + u',' + danmaku['size'] + u',AcFun,0000,0000,0000,,{\\pos(' + str(RESX / 2) + u', ' + str(position) + u')\\c&H' + danmaku['color'] + u'\\fs' + danmaku['orisize'] + u'}' + danmaku['content'], 'time': danmaku['oritime']}
ssa_list.append(ssa)
return ssa_list
def parse_bottom_danmaku(danmaku_list):
"""
底部弹幕解析函数
输入: 列表式字典类型底部弹幕
返回: 底部弹幕SSA列表
"""
screen = ScreenController(True)
ssa_list = []
for danmaku in danmaku_list:
position = screen.add_danmaku(int(danmaku['orisize']), danmaku['oritime'], 4)
ssa = {'ssa': u'Dialogue: 5,' + danmaku['time'] + u',' + danmaku['size'] + u',AcFun,0000,0000,0000,,{\\pos(' + str(RESX / 2) + u', ' + str(position) + u')\\c&H' + danmaku['color'] + u'\\fs' + danmaku['orisize'] + u'}' +danmaku['content'], 'time': danmaku['oritime']}
ssa_list.append(ssa)
return ssa_list
def parse_rolling_danmaku(danmaku_list):
"""
滚动弹幕解析函数
输入: 列表式字典类型滚动弹幕
返回: 滚动弹幕SSA列表
"""
screen = ScreenController(False)
ssa_list = []
for danmaku in danmaku_list:
length = str_length(danmaku['content'], int(danmaku['orisize']))
life_time = 4. * (0.5 * length / (0.5 * length + RESX))
position = screen.add_danmaku(int(danmaku['orisize']), danmaku['oritime'], life_time)
ssa = {'ssa': u'Dialogue: 3,' + danmaku['time'] + u',' + danmaku['size'] + u',AcFun,0000,0000,0000,,{\\move(' + str(RESX + length / 2) + u', ' + str(position) + u', ' + str(-length / 2) + u', ' + str(position) + u')\\c&H' +danmaku['color'] + u'\\fs' + danmaku['orisize'] + u'}' + danmaku['content'], 'time': danmaku['oritime']}
ssa_list.append(ssa)
return ssa_list
# ========================================
def distribute_danmaku(danmaku_list):
"""
弹幕主解析分发函数
输入: 列表式字典类型弹幕
返回: 弹幕SSA列表
SSA字典定义: {'ssa':SSA内容, 'time':浮点原始时间戳}
"""
top_danmaku = []
bottom_danmaku = []
rolling_danmaku = []
ssa_list = []
for danmaku in danmaku_list:
if danmaku['mode'] in ('1', '2', '3', '7'): # 高级弹幕强制认定为滚动弹幕
rolling_danmaku.append(danmaku)
elif danmaku['mode'] == '4':
bottom_danmaku.append(danmaku)
elif danmaku['mode'] == '5':
top_danmaku.append(danmaku)
elif danmaku['mode'] == '6':
rolling_danmaku.append(danmaku) # 这是逆向。。当滚动处理吧。。
else:
print(u'老婆快出来看啊有神弹幕!轨道:' + danmaku['mode'])
#print(danmaku['content'])
#rolling_danmaku.append(danmaku) # 特效轨,脚本可能卡程序,暂时无视
ssa_list.extend(parse_top_danmaku(top_danmaku))
ssa_list.extend(parse_bottom_danmaku(bottom_danmaku))
ssa_list.extend(parse_rolling_danmaku(rolling_danmaku))
return ssa_list
def xml_to_ssa(xml_data):
"""
主函数。
输入: 原始XML
返回: Unicode 编码 SSA文件
"""
print('Parsing XML File...')
try:
danmaku_list = parse_xml(xml_data)
except:
print('FAILED.')
return ''
print('Converting to SSA...')
try:
ssa_list = distribute_danmaku(danmaku_list)
except:
print('FAILED.')
return ''
output_ssa = OUTPUT_HEAD
for ssa_line in ssa_list:
output_ssa += ssa_line['ssa'] + '\n'
print('Converting Successfully Finished - Returning Result.')
return output_ssa
def BiliStarHandler(xml_data, filename):
"""
BiliStar 接口
"""
print('BiliStar - BiliDanmaku Module Version: ' + VERSION)
outfile = codecs.open(filename, 'w', 'utf-8')
outfile.write(xml_to_ssa(xml_data))
outfile.close()
# ========================================
win_repair()
def main():
print('BiliStar - BiliDanmaku Module.\nVersion: ' + VERSION + '\n')
filename = win_encode(input('Filename Please [For Windows: Use \'/\' instead of \'\\\']:'))
if not filename.endswith('.xml'):
print('Not a XML Danmaku File!')
exit()
print('\nOutput File: ' + filename[:-4] + '.ssa\n')
xml_data = win_encode(open(filename).read())
print('Converting...')
output = codecs.open(filename[:-4] + '.ssa', 'w', 'utf-8')
output.write(xml_to_ssa(xml_data))
output.close()
print('Finished.')
if __name__ == '__main__':
main()