第一次上传

This commit is contained in:
xhlove
2020-01-04 20:09:29 +08:00
parent 87074df590
commit 9e93912847
11 changed files with 800 additions and 0 deletions

68
methods/assbase.py Normal file
View File

@@ -0,0 +1,68 @@
#!/usr/bin/env python
# coding=utf-8
'''
# 作者: weimo
# 创建日期: 2020-01-04 13:01:04
# 上次编辑时间 : 2020-01-04 15:42:02
# 一个人的命运啊,当然要靠自我奋斗,但是...
'''
from datetime import datetime
from random import randint, choice
class ASS(object):
def __init__(self, file_path, get_xy_obj, font="微软雅黑"):
self.font = font
self.get_xy_obj = get_xy_obj
# 起点位置可以随机在一个区域出现
# 起点位置可以随机在一个区域出现 其他扩展
self.baseline = """Dialogue: 0,{start_time},{end_time},{font},,0,0,0,,{{{move_text}{color}}}{text}"""
self.lines = []
def create_new_line(self, comment):
text, color, timepoint = comment
start_time, end_time, show_time = self.set_start_end_time(timepoint)
font = self.set_random_font(line="")
move_text = self.set_start_end_pos(text, show_time)
color = self.set_color(color)
line = self.baseline.format(start_time=start_time, end_time=end_time, font=font, move_text=move_text, color=color, text=text)
self.lines.append(line)
def set_color(self, color: list):
# \1c&FDA742&
if color.__len__() == 1:
color = "\\1c&{}&".format(color[0].lstrip("#").upper())
else:
color = "\\1c&{}&".format(choice(color).lstrip("#").upper())
# color = "\\1c&{}&\\t(0,10000,\\2c&{}&".format(color[0].lstrip("#").upper(), color[1].lstrip("#").upper())
return color
def set_start_end_pos(self, text, show_time):
# 考虑不同大小字体下的情况 TODO
# \move(1920,600,360,600)
# min_index = self.get_min_length_used_y()
start_x = 1920
width, height, start_y = self.get_xy_obj.get_xy(text, show_time)
# start_y = self.all_start_y[min_index]
end_x = -(width + randint(0, 30))
end_y = start_y
move_text = "\\move({},{},{},{})".format(start_x, start_y, end_x, end_y)
# self.update_length_used_y(min_index, text.__len__() * 2)
return move_text
def set_start_end_time(self, timepoint):
# 40*60*60 fromtimestamp接收的数太小就会出问题
t = 144000
# 记录显示时间 用于计算字幕运动速度 在某刻的位置 最终决定弹幕分布选择
show_time = 15 #randint(10, 20)
st = t + timepoint
et = t + timepoint + show_time
start_time = datetime.fromtimestamp(st).strftime("%H:%M:%S.%f")[1:][:-4]
end_time = datetime.fromtimestamp(et).strftime("%H:%M:%S.%f")[1:][:-4]
return start_time, end_time, show_time
def set_random_font(self, line=""):
font = self.font
return font

56
methods/sameheight.py Normal file
View File

@@ -0,0 +1,56 @@
#!/usr/bin/env python
# coding=utf-8
'''
# 作者: weimo
# 创建日期: 2019-12-25 20:35:43
# 上次编辑时间 : 2019-12-25 23:23:32
# 一个人的命运啊,当然要靠自我奋斗,但是...
'''
from PIL.ImageFont import truetype
class SameHeight(object):
'''
# 等高弹幕 --> 矩形分割问题?
'''
def __init__(self, text, font_path="msyh.ttc", font_size=14):
self.font = truetype(font=font_path, size=font_size)
self.width, self.height = self.get_danmu_size(text)
self.height_range = [0, 720]
self.width_range = [0, 1920]
self.lines_start_y = list(range(*(self.height_range + [self.height])))
self.lines_width_used = [[y, 0] for y in self.lines_start_y]
self.contents = []
def get_xy(self, text, show_time):
# 在此之前 务必现将弹幕按时间排序
self.contents.append([text, show_time])
width, height = self.get_danmu_size(text)
lines_index = self.get_min_width_used()
self.update_width_used(lines_index, width)
start_y = self.lines_start_y[lines_index]
return width, height, start_y
def get_min_width_used(self):
sorted_width_used = sorted(self.lines_width_used, key=lambda width_used: width_used[1])
lines_index = self.lines_width_used.index(sorted_width_used[0])
return lines_index
def update_width_used(self, index, length):
self.lines_width_used[index][1] += length
def get_danmu_size(self, text):
# 放在这 不太好 每一次计算都会load下字体
text_width, text_height = self.font.getsize(text)
return text_width + 2, text_height + 2
def main():
text = "测试"
show_time = 13
sh = SameHeight(text)
sh.get_xy(text, show_time)
if __name__ == "__main__":
main()