mirror of
https://github.com/xhlove/GetDanMu.git
synced 2025-12-16 16:15:56 +08:00
78 lines
3.1 KiB
Python
78 lines
3.1 KiB
Python
#!/usr/bin/env python3.7
|
||
# coding=utf-8
|
||
'''
|
||
# 作者: weimo
|
||
# 创建日期: 2020-01-04 19:14:46
|
||
# 上次编辑时间 : 2020-02-07 18:33:14
|
||
# 一个人的命运啊,当然要靠自我奋斗,但是...
|
||
'''
|
||
|
||
import os
|
||
import json
|
||
from basic.vars import fonts
|
||
|
||
ass_script = """[Script Info]
|
||
; Script generated by N
|
||
ScriptType: v4.00+
|
||
PlayResX: 1920
|
||
PlayResY: 1080
|
||
Aspect Ratio: 1920:1080
|
||
Collisions: Normal
|
||
WrapStyle: 0
|
||
ScaledBorderAndShadow: yes
|
||
YCbCr Matrix: TV.601"""
|
||
|
||
ass_style_head = """[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding"""
|
||
ass_style_default = """Style: Default,Arial,20,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1"""
|
||
ass_style_base = """Style:{font},{font},{font_size},&H00FFFFFF,&H66FFFFFF,&H66000000,&H66000000,0,0,0,0,100,100,0.00,0.00,1,1,0,7,0,0,0,0"""
|
||
ass_events_head = """[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text"""
|
||
# 基于当前时间范围,在0~1000ms之间停留在(676.571,506.629)处,在1000~3000ms内从位置1300,600移动到360,600(原点在左上)
|
||
# ass_baseline = """Dialogue: 0,0:20:08.00,0:20:28.00,Default,,0,0,0,,{\t(1000,3000,\move(1300,600,360,600))\pos(676.571,506.629)}这是字幕内容示意"""
|
||
|
||
def get_fonts_info():
|
||
fonts_path = r"C:\Windows\Fonts"
|
||
if os.path.exists("config.json"):
|
||
with open("config.json", "r", encoding="utf-8") as f:
|
||
fr = f.read()
|
||
try:
|
||
config = json.loads(fr)
|
||
except Exception as e:
|
||
print("get_fonts_info error info ->", e)
|
||
else:
|
||
fonts_path = config["fonts_base_folder"]
|
||
fonts = config["fonts"]
|
||
return fonts_path, fonts
|
||
|
||
def get_ass_head(font_style_name, font_size):
|
||
ass_head = ass_script + "\n\n" + ass_style_head + "\n" + ass_style_base.format(font=font_style_name, font_size=font_size) + "\n\n" + ass_events_head
|
||
return ass_head
|
||
|
||
def check_font(font):
|
||
fonts_path, fonts = get_fonts_info()
|
||
maybe_font_path = os.path.join(fonts_path, font)
|
||
font_style_name = "微软雅黑"
|
||
font_path = os.path.join(fonts_path, fonts[font_style_name]) # 默认
|
||
if os.path.exists(font):
|
||
# 字体就在当前文件夹 或 完整路径
|
||
if os.path.isfile(font):
|
||
if os.path.isabs(font):
|
||
font_path = font
|
||
else:
|
||
font_path = os.path.join(os.getcwd(), font)
|
||
font_style_name = font[:-os.path.splitext(font)[-1].__len__()]
|
||
else:
|
||
pass
|
||
elif os.path.exists(maybe_font_path):
|
||
# 给的是字体文件名
|
||
if os.path.isfile(maybe_font_path):
|
||
font_path = maybe_font_path
|
||
font_style_name = font[:-os.path.splitext(font)[-1].__len__()]
|
||
else:
|
||
pass
|
||
elif fonts.get(font):
|
||
# 别名映射
|
||
font_path = os.path.join(fonts_path, fonts.get(font))
|
||
font_style_name = font
|
||
else:
|
||
pass
|
||
return font_path, font_style_name |