Skip to content

Commit 40600a2

Browse files
authored
feat: superchat fontsize (#27)
* feat: superchat fontsize * docs: update docs
1 parent d1c5076 commit 40600a2

File tree

7 files changed

+55
-34
lines changed

7 files changed

+55
-34
lines changed

README-en.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ More details:
4747

4848
```bash
4949
dmconvert -h
50-
# optional arguments:
50+
# options:
5151
# -h, --help show this help message and exit
5252
# -V, --version Print version information
5353
# -f FONTSIZE, --fontsize FONTSIZE
5454
# The font size of the danmaku, default is 38
55+
# -sf SCFONTSIZE, --scfontsize SCFONTSIZE
56+
# The font size of the superchat and gift, default is 38
5557
# -x RESOLUTIONX, --resolutionx RESOLUTIONX
5658
# The resolution x of the danmaku, default is 1920
5759
# -y RESOLUTIONY, --resolutiony RESOLUTIONY
@@ -60,7 +62,8 @@ dmconvert -h
6062
# -o ASS, --ass ASS The output ass file
6163

6264
# Example:
63-
# dmconvert -f 38 -x 1920 -y 1080 -i sample.xml -o sample.ass
65+
# dmconvert -i input.xml -o output.ass
66+
# dmconvert -f 38 -sf 30 -x 1920 -y 1080 -i input.xml -o output.ass
6467
```
6568

6669
### Source Version
@@ -72,9 +75,10 @@ from dmconvert.convert import convert_xml_to_ass
7275
# xml_file = "sample.xml"
7376
# ass_file = "sample.ass"
7477
# font_size = 38
78+
# sc_font_size = 30
7579
# resolution_x = 720
7680
# resolution_y = 1280
77-
convert_xml_to_ass(font_size, resolution_x, resolution_y, xml_file, ass_file)
81+
convert_xml_to_ass(font_size, sc_font_size, resolution_x, resolution_y, xml_file, ass_file)
7882
```
7983

8084
## The implementation algorithm
@@ -465,13 +469,13 @@ As we can see, the superchat show up and disappear in chronological order, so ho
465469
My solution is: calculate the position of the superchat message box every time the superchat changes its position in its life cycle. The concrete algorithm is as follows:
466470

467471
```python
468-
def render_superchat(ass_file, font_size, resolution_y, data):
472+
def render_superchat(ass_file, sc_font_size, resolution_y, data):
469473
"""
470474
Render superchat events to the ass file.
471475
472476
Args:
473477
ass_file (str): The path to the ass file.
474-
font_size (int): The font size, which is used to calculate some render parameters.
478+
sc_font_size (int): The superchat font size, which is used to calculate some render parameters.
475479
resolution_y (int): The resolution y, which is used to calculate the initial y coordinate.
476480
data (list): The data to render, which is a list of superchat events.
477481
"""
@@ -535,7 +539,7 @@ def render_superchat(ass_file, font_size, resolution_y, data):
535539
) in enumerate(data):
536540
# print(f"\nSC {i} ({start}-{end}):")
537541
# Initial y coordinate
538-
previous_y = resolution_y - font_size * 2
542+
previous_y = resolution_y - sc_font_size * 2
539543
current_y = previous_y - sc_height
540544
current_time = start
541545
# print(f"Time {start}: y = {current_y}, previous_y = {previous_y}")
@@ -558,6 +562,7 @@ def render_superchat(ass_file, font_size, resolution_y, data):
558562
current_y,
559563
previous_y,
560564
text,
565+
sc_font_size,
561566
).write_superchat(ass_file)
562567
previous_y = current_y
563568
if delta_y[0] == "-":
@@ -576,6 +581,7 @@ def render_superchat(ass_file, font_size, resolution_y, data):
576581
current_y,
577582
previous_y,
578583
text,
584+
sc_font_size,
579585
).write_superchat(ass_file)
580586
```
581587

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ dmconvert -i sample.xml
4545

4646
```bash
4747
dmconvert -h
48-
# optional arguments:
48+
# options:
4949
# -h, --help show this help message and exit
5050
# -V, --version Print version information
5151
# -f FONTSIZE, --fontsize FONTSIZE
5252
# The font size of the danmaku, default is 38
53+
# -sf SCFONTSIZE, --scfontsize SCFONTSIZE
54+
# The font size of the superchat and gift, default is 38
5355
# -x RESOLUTIONX, --resolutionx RESOLUTIONX
5456
# The resolution x of the danmaku, default is 1920
5557
# -y RESOLUTIONY, --resolutiony RESOLUTIONY
@@ -58,7 +60,8 @@ dmconvert -h
5860
# -o ASS, --ass ASS The output ass file
5961

6062
# Example:
61-
# dmconvert -f 38 -x 1920 -y 1080 -i sample.xml -o sample.ass
63+
# dmconvert -i input.xml -o output.ass
64+
# dmconvert -f 38 -sf 30 -x 1920 -y 1080 -i input.xml -o output.ass
6265
```
6366

6467
### 直接引用
@@ -70,9 +73,10 @@ from dmconvert.convert import convert_xml_to_ass
7073
# xml_file = "sample.xml"
7174
# ass_file = "sample.ass"
7275
# font_size = 38
76+
# sc_font_size = 30
7377
# resolution_x = 720
7478
# resolution_y = 1280
75-
convert_xml_to_ass(font_size, resolution_x, resolution_y, xml_file, ass_file)
79+
convert_xml_to_ass(font_size, sc_font_size, resolution_x, resolution_y, xml_file, ass_file)
7680
```
7781

7882
## 实现原理
@@ -460,13 +464,13 @@ Dialogue: 1,0:00:59.20,0:01:10.00,message_box,,0000,0000,0000,,{\pos(20,904.0)\c
460464
我的解决方案是:每次付费留言在其生命周期中改变位置时计算付费留言消息框的位置。具体的算法如下:
461465

462466
```python
463-
def render_superchat(ass_file, font_size, resolution_y, data):
467+
def render_superchat(ass_file, sc_font_size, resolution_y, data):
464468
"""
465-
将付费留言事件渲染到 ASS 文件中。
469+
Render superchat events to the ass file.
466470
467471
Args:
468472
ass_file (str): The path to the ass file.
469-
font_size (int): The font size, which is used to calculate some render parameters.
473+
sc_font_size (int): The superchat font size, which is used to calculate some render parameters.
470474
resolution_y (int): The resolution y, which is used to calculate the initial y coordinate.
471475
data (list): The data to render, which is a list of superchat events.
472476
"""
@@ -530,7 +534,7 @@ def render_superchat(ass_file, font_size, resolution_y, data):
530534
) in enumerate(data):
531535
# print(f"\nSC {i} ({start}-{end}):")
532536
# Initial y coordinate
533-
previous_y = resolution_y - font_size * 2
537+
previous_y = resolution_y - sc_font_size * 2
534538
current_y = previous_y - sc_height
535539
current_time = start
536540
# print(f"Time {start}: y = {current_y}, previous_y = {previous_y}")
@@ -553,6 +557,7 @@ def render_superchat(ass_file, font_size, resolution_y, data):
553557
current_y,
554558
previous_y,
555559
text,
560+
sc_font_size,
556561
).write_superchat(ass_file)
557562
previous_y = current_y
558563
if delta_y[0] == "-":
@@ -571,6 +576,7 @@ def render_superchat(ass_file, font_size, resolution_y, data):
571576
current_y,
572577
previous_y,
573578
text,
579+
sc_font_size,
574580
).write_superchat(ass_file)
575581
```
576582

dmconvert/cli.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def cli():
1818
'''),
1919
epilog=textwrap.dedent('''
2020
Example:
21-
dmconvert -f 38 -x 1920 -y 1080 -i input.xml -o output.ass
21+
dmconvert -i input.xml -o output.ass
22+
dmconvert -f 38 -sf 30 -x 1920 -y 1080 -i input.xml -o output.ass
2223
'''),
2324
)
2425
parser.add_argument(
@@ -35,6 +36,13 @@ def cli():
3536
type=int,
3637
help="The font size of the danmaku, default is 38",
3738
)
39+
parser.add_argument(
40+
"-sf",
41+
"--scfontsize",
42+
default=38,
43+
type=int,
44+
help="The font size of the superchat and gift, default is 38",
45+
)
3846
parser.add_argument(
3947
"-x",
4048
"--resolutionx",
@@ -57,10 +65,11 @@ def cli():
5765
args = parser.parse_args()
5866

5967
if os.path.splitext(args.xml)[1] == ".xml":
60-
xml_file = args.xml
61-
ass_file = args.ass if args.ass else os.path.splitext(xml_file)[0] + ".ass"
68+
xml_file = os.path.abspath(args.xml)
69+
ass_file = os.path.abspath(args.ass) if args.ass else os.path.abspath(os.path.splitext(xml_file)[0] + ".ass")
6270
convert_xml_to_ass(
6371
args.fontsize,
72+
args.scfontsize,
6473
args.resolutionx,
6574
args.resolutiony,
6675
xml_file,

dmconvert/convert.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@
1111
from dmconvert.header.header import draw_ass_header
1212

1313

14-
def convert_xml_to_ass(font_size, resolution_x, resolution_y, xml_file, ass_file):
14+
def convert_xml_to_ass(font_size, sc_font_size, resolution_x, resolution_y, xml_file, ass_file):
1515
# Parse XML
1616
print("DanmakuConvert v0.0.2", flush=True)
1717
print("https://github.com/timerring/DanmakuConvert", flush=True)
1818
tree = ET.parse(xml_file)
1919
root = tree.getroot()
2020
roll_array = DanmakuArray(resolution_x, resolution_y)
2121
btm_array = DanmakuArray(resolution_x, resolution_y)
22-
draw_ass_header(ass_file, resolution_x, resolution_y, font_size)
22+
draw_ass_header(ass_file, resolution_x, resolution_y, font_size, sc_font_size)
2323
draw_normal_danmaku(
2424
ass_file, root, font_size, roll_array, btm_array, resolution_x, resolution_y
2525
)
26-
draw_gift_and_guard(ass_file, root, font_size, resolution_y)
27-
draw_superchat(ass_file, font_size, resolution_y, root)
26+
draw_gift_and_guard(ass_file, root, sc_font_size, resolution_y)
27+
draw_superchat(ass_file, sc_font_size, resolution_y, root)
2828
print(f"Convert {xml_file} to {ass_file} successfully.", flush=True)
2929

3030

3131
if __name__ == "__main__":
3232
xml_file = "sample.xml"
3333
ass_file = "sample.ass"
34-
convert_xml_to_ass(38, 720, 1280, xml_file, ass_file)
34+
convert_xml_to_ass(38, 38, 720, 1280, xml_file, ass_file)

dmconvert/guardgift/gg_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def extract_gift_data(element):
2626
return data
2727

2828

29-
def draw_gift_and_guard(ass_file, root, font_size, resolution_y):
29+
def draw_gift_and_guard(ass_file, root, sc_font_size, resolution_y):
3030
with open(ass_file, "a", encoding="utf-8") as f:
3131
# Convert gifts and guards
3232
raw_gifts = [
@@ -41,6 +41,6 @@ def draw_gift_and_guard(ass_file, root, font_size, resolution_y):
4141
# generate the output
4242
for gift in processed:
4343
lines = generate_ass_line(
44-
gift, resolution_y, font_size
44+
gift, resolution_y, sc_font_size
4545
) # example parameters
4646
f.writelines(lines)

dmconvert/header/header.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2025 DanmakuConvert
22

33

4-
def draw_ass_header(ass_file, resolution_x, resolution_y, font_size):
4+
def draw_ass_header(ass_file, resolution_x, resolution_y, font_size, sc_font_size):
55
# Write ASS header
66
ass_header = f"""[Script Info]
77
ScriptType: v4.00+
@@ -20,8 +20,8 @@ def draw_ass_header(ass_file, resolution_x, resolution_y, font_size):
2020
Style: TOP,Microsoft YaHei,{font_size},&H4BFFFFFF,&H00FFFFFF,&H00000000,&H1E6A5149,0,0,0,0,100.00,100.00,0.00,0.00,1,0.0,1.0,8,0,0,0,1
2121
Style: BTM,Microsoft YaHei,{font_size},&H4BFFFFFF,&H00FFFFFF,&H00000000,&H1E6A5149,0,0,0,0,100.00,100.00,0.00,0.00,1,0.0,1.0,8,0,0,0,1
2222
Style: SP,Microsoft YaHei,{font_size},&H00FFFFFF,&H00FFFFFF,&H00000000,&H1E6A5149,0,0,0,0,100.00,100.00,0.00,0.00,1,0.0,1.0,7,0,0,0,1
23-
Style: message_box,Microsoft YaHei,{font_size},&H00FFFFFF,&H00FFFFFF,&H00000000,&H1E6A5149,0,0,0,0,100.00,100.00,0.00,0.00,1,0.0,0.7,7,0,0,0,1
24-
Style: price,Microsoft YaHei,{int(font_size * 0.7)},&H00FFFFFF,&H00FFFFFF,&H00000000,&H1E6A5149,0,0,0,0,100.00,100.00,0.00,0.00,1,0.0,0.7,7,0,0,0,1
23+
Style: message_box,Microsoft YaHei,{sc_font_size},&H00FFFFFF,&H00FFFFFF,&H00000000,&H1E6A5149,0,0,0,0,100.00,100.00,0.00,0.00,1,0.0,0.7,7,0,0,0,1
24+
Style: price,Microsoft YaHei,{int(sc_font_size * 0.7)},&H00FFFFFF,&H00FFFFFF,&H00000000,&H1E6A5149,0,0,0,0,100.00,100.00,0.00,0.00,1,0.0,0.7,7,0,0,0,1
2525
2626
[Events]
2727
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text

dmconvert/superchat/superchat_handler.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_sc_height(line_num, sc_font_size):
2727
return sc_height, top_box_height, btm_box_height
2828

2929

30-
def draw_superchat(ass_file, font_size, resolution_y, root):
30+
def draw_superchat(ass_file, sc_font_size, resolution_y, root):
3131
sc_list = []
3232
all_superchat = root.findall(".//sc")
3333
superchat_count = len(all_superchat)
@@ -39,7 +39,7 @@ def draw_superchat(ass_file, font_size, resolution_y, root):
3939
disapper_time = float(sc.get("ts")) + float(sc.get("time"))
4040
text = sc.text
4141
processed_text, line_num = get_text_line_num(text)
42-
sc_height, _, btm_box_height = get_sc_height(line_num, font_size)
42+
sc_height, _, btm_box_height = get_sc_height(line_num, sc_font_size)
4343
process = ""
4444
sc_list.append(
4545
[
@@ -54,16 +54,16 @@ def draw_superchat(ass_file, font_size, resolution_y, root):
5454
]
5555
)
5656

57-
render_superchat(ass_file, font_size, resolution_y, sc_list)
57+
render_superchat(ass_file, sc_font_size, resolution_y, sc_list)
5858

5959

60-
def render_superchat(ass_file, font_size, resolution_y, data):
60+
def render_superchat(ass_file, sc_font_size, resolution_y, data):
6161
"""
6262
Render superchat events to the ass file.
6363
6464
Args:
6565
ass_file (str): The path to the ass file.
66-
font_size (int): The font size, which is used to calculate some render parameters.
66+
sc_font_size (int): The superchat font size, which is used to calculate some render parameters.
6767
resolution_y (int): The resolution y, which is used to calculate the initial y coordinate.
6868
data (list): The data to render, which is a list of superchat events.
6969
"""
@@ -127,7 +127,7 @@ def render_superchat(ass_file, font_size, resolution_y, data):
127127
) in enumerate(data):
128128
# print(f"\nSC {i} ({start}-{end}):")
129129
# Initial y coordinate
130-
previous_y = resolution_y - font_size * 2
130+
previous_y = resolution_y - sc_font_size * 2
131131
current_y = previous_y - sc_height
132132
current_time = start
133133
# print(f"Time {start}: y = {current_y}, previous_y = {previous_y}")
@@ -150,7 +150,7 @@ def render_superchat(ass_file, font_size, resolution_y, data):
150150
current_y,
151151
previous_y,
152152
text,
153-
font_size,
153+
sc_font_size,
154154
).write_superchat(ass_file)
155155
previous_y = current_y
156156
if delta_y[0] == "-":
@@ -169,5 +169,5 @@ def render_superchat(ass_file, font_size, resolution_y, data):
169169
current_y,
170170
previous_y,
171171
text,
172-
font_size,
172+
sc_font_size,
173173
).write_superchat(ass_file)

0 commit comments

Comments
 (0)