import os def save_m3t(m3t_path, mes): with open(m3t_path, "w", encoding="utf-8") as f: for m in mes: if 'name' in m: f.write("○ NAME: {}\n\n".format(m["name"])) f.write("○ {}\n".format(m["ori"])) if 'llm' in m: f.write("△ {}\n".format(m["llm"])) if 'dst_name' in m: f.write("○ NAME: {}\n\n".format(m["dst_name"])) if m['dst']: f.write("● {}\n".format(m["dst"])) else: f.write("●\n") f.write("\n") def load_dltxt(dltxt_path): mes = [] name = None ori = None line_num = 0 for line in open(dltxt_path, "r", encoding="utf-8"): if line_num == 0: line = line.lstrip("\ufeff") line_num += 1 line = line.strip() if line.startswith("○"): oris = line[1:].strip().split("○", 1) if len(oris) == 1: raise ValueError("Invalid format: missing ○ after ○.") names = oris[0].split("|") if len(names) != 2: raise ValueError("Invalid format: expected exactly one | in the name section.") name = names[1].strip() if name == "V.O.": name = None ori = oris[1].strip() elif line.startswith("●"): dsts = line[1:].strip().split("●", 1) if len(dsts) == 1: raise ValueError("Invalid format: missing ● after ●.") dst_names = dsts[0].split("|") if len(dst_names) != 2: raise ValueError("Invalid format: expected exactly one | in the dst section.") dst_name = dst_names[1].strip() if dst_name == "V.O.": dst_name = None dst = dsts[1].strip() if ori is None: raise ValueError(f"Invalid format: found ● before ○ on line {line_num}.") target = { "ori": ori, "dst": dst, } if name is not None: target["name"] = name if dst_name is not None: target["dst_name"] = dst_name name = None ori = None mes.append(target) return mes def process_recursive(dltxt_path, m3t_path): for root, dirs, files in os.walk(dltxt_path): rel_dir = os.path.relpath(root, dltxt_path) target_dir = os.path.join(m3t_path, rel_dir) os.makedirs(target_dir, exist_ok=True) for file in files: ori = os.path.join(root, file) print("Processing:", ori) mes = load_dltxt(ori) save_m3t(os.path.join(target_dir, os.path.splitext(file)[0] + '.m3t'), mes) if __name__ == "__main__": import sys dltxt_path = sys.argv[1] m3t_path = sys.argv[2] if os.path.isdir(dltxt_path): process_recursive(dltxt_path, m3t_path) else: mes = load_dltxt(dltxt_path) save_m3t(m3t_path, mes)