1
+ import pandas as pd
2
+ from datetime import timedelta
3
+ import json
4
+ import sys
5
+
6
+ def seconds_to_time_string (seconds ):
7
+ # 将秒数转换为 timedelta 对象
8
+ time_delta = timedelta (seconds = seconds )
9
+
10
+ # 使用 strftime 方法将 timedelta 对象格式化为字符串,并保留两位小数
11
+ time_string = time_delta .total_seconds ()
12
+ time_string = "{:.2f}" .format (time_string )
13
+
14
+ # 将秒数部分替换为 HH:MM:SS 格式
15
+ hours , remainder = divmod (int (time_string .split ('.' )[0 ]), 3600 )
16
+ minutes , seconds = divmod (remainder , 60 )
17
+ time_string = f"{ hours :02d} :{ minutes :02d} :{ seconds :02d} .{ time_string .split ('.' )[1 ]} "
18
+
19
+ return time_string
20
+
21
+ def df_to_ass (df ):
22
+ ass_content = "[Script Info]\n "
23
+ ass_content += "ScriptType: v4.00+\n "
24
+ ass_content += "PlayResX: 1920\n "
25
+ ass_content += "PlayResY: 1080\n "
26
+ ass_content += "WrapStyle: 0\n "
27
+ ass_content += "\n "
28
+ ass_content += "[V4+ Styles]\n "
29
+ ass_content += "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n "
30
+ ass_content += "Style: Default,Arial,36,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,0\n "
31
+ ass_content += "\n "
32
+ ass_content += "[Events]\n "
33
+ ass_content += "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n "
34
+
35
+ for index , row in df .iterrows ():
36
+ ass_content += f"Dialogue: 0,{ seconds_to_time_string (row ['start_time' ])} ,{ seconds_to_time_string (row ['end_time' ])} ,Default,,0,0,0,,{ row ['content' ].replace ('\n ' , '\\ N' )} \n "
37
+
38
+ return ass_content
39
+
40
+ # 读取BCC文件并解析为DataFrame
41
+ def read_bcc_file (file_path ):
42
+ with open (file_path , 'r' , encoding = 'utf-8' ) as file :
43
+ data = json .load (file )
44
+ return pd .DataFrame (data ['body' ])
45
+
46
+ # 主函数
47
+ def main ():
48
+ # 检查命令行参数是否正确
49
+ if len (sys .argv ) != 5 or sys .argv [1 ] != '-i' or sys .argv [3 ] != '-o' :
50
+ print ("请使用正确的参数格式:python bcc2ass.py -i input_file -o output_file" )
51
+ sys .exit (1 )
52
+
53
+ # 获取输入和输出文件路径
54
+ input_file_path = sys .argv [2 ]
55
+ output_file_path = sys .argv [4 ]
56
+
57
+ # 读取BCC文件并解析为DataFrame
58
+ df = read_bcc_file (input_file_path )
59
+
60
+ # 将DataFrame转换为ASS文件内容
61
+ ass_file_content = df_to_ass (df )
62
+
63
+ # 将ASS文件内容写入文件
64
+ with open (output_file_path , 'w' , encoding = 'utf-8' ) as file :
65
+ file .write (ass_file_content )
66
+
67
+ # 运行主函数
68
+ if __name__ == "__main__" :
69
+ main ()
0 commit comments