fix empty

This commit is contained in:
2025-05-10 00:21:00 +08:00
parent 96845776aa
commit 1b82425de0

126
convert.py Normal file
View File

@@ -0,0 +1,126 @@
import os
from pathlib import Path
import shutil
from MaichartConverter import ma2tosimai
from ab2png import convert_ab_to_png
from acb2mp3 import convert_awb_to_wav, convert_wav_to_mp3
from search import search_music_by_id
from pv_decode import dat_to_mp4
# 假设你已实现以下函数
def build_maidata_txt(
title: str = "",
freemsg: str = "",
bpm: str = "",
first_notes: dict = None, # {1: 0.123, 2: ..., ...}
levels: dict = None, # {1: "3", 2: "5", ...}
designers: dict = None, # {1: "作者A", 2: ..., ...}
charts: dict = None, # {1: "谱面数据\n...", ...}
levelnum:int = None,
) -> str:
maidata = [f"&title={title}", f"&freemsg={freemsg}", f"&wholebpm={bpm}", "&first=0"]
# 1~6 难度
for i in range(1, levelnum+1):
first = f"{first_notes.get(i):.3f}" if first_notes and i in first_notes else ""
lv = levels.get(i, "") if levels else ""
des = designers.get(i, "") if designers else ""
chart = charts.get(i, "") if charts else ""
maidata.append(f"&first_{i}={first}")
for i in range(2, levelnum+1):
lv = levels.get(i, "") if levels else ""
des = designers.get(i, "") if designers else ""
maidata.append(f"&lv_{i}={lv}")
maidata.append(f"&des_{i}={des}")
for i in range(2, levelnum):
chart = charts.get(i, "") if charts else ""
maidata.append(f"&inote_{i}=")
maidata.append(chart.strip())
maidata.append("&amsg_time=")
maidata.append("&amsg_content=")
return "\n".join(maidata)
def convert_to_simai_folder(result,output_folder):
npof = output_folder
output_folder = Path(output_folder)
info = result[0]
id = info[0]
of = os.path.join("work", id)
if not os.path.exists(of):
os.makedirs(of)
name = info[1]
artist = info[2]
designers = {i + 1: item["designer"] for i, item in enumerate(info[3])}
levels = {i + 1: item["levelshow"] for i, item in enumerate(info[3])}
ma2_list = result[1]
ab_file = result[2]
acb_list = result[3]
dat_file = result[4]
convert_results = {}
for mai in ma2_list:
convert_results.update({ma2_list.index(mai)+1: ma2tosimai(mai)})
convert_awb_to_wav(acb_list[1],f"work/{id}/temp.wav")
convert_wav_to_mp3(f"work/{id}/temp.wav",f"work/{id}/track.mp3")
convert_ab_to_png(ab_file,f"work/{id}/bg.png")
pv_path = dat_to_mp4(dat_file,id)
maidata_txt = build_maidata_txt(title=name,levels=levels,designers=designers,charts=convert_results,levelnum=len(ma2_list),freemsg=artist)
with open(f"work/{id}/maidata.txt", "w") as f:
f.write(maidata_txt)
source_folder = of
# 目标文件夹路径
target_folder = npof
# 要复制的文件列表
files_to_copy = ["bg.png", "maidata.txt", "pv.mp4", "track.mp3"]
# 检查目标文件夹是否存在,不存在则创建
if not os.path.exists(target_folder):
os.makedirs(target_folder)
# 遍历每个文件并复制到目标路径
for file_name in files_to_copy:
src_file = os.path.join(source_folder, file_name)
dst_file = os.path.join(target_folder, file_name)
if os.path.exists(src_file):
shutil.copy(src_file, dst_file)
print(f"文件 {file_name} 复制成功")
else:
print(f"文件 {file_name} 不存在,跳过复制")
# 示例调用
if __name__ == "__main__":
res = search_music_by_id(input("ID:"))
print(res)
print("Converting...")
convert_to_simai_folder(res,"result")