这个Fitten Code Chat是pycharm里的一个插件,官网https://code.fittentech.com/
诉求,如上图,从网上下的歌曲,其CUE因抓轨的人没写歌名,但提供了歌名,需要自己修改。
程序成功了一半,使用“浏览”按键可以获取正确的文件。
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinterdnd2 import TkinterDnD, DND_FILES
def read_file(file_path):
# 尝试使用不同的编码读取文件
for encoding in ['utf-8', 'ansi']:
try:
with open(file_path, 'r', encoding=encoding) as file:
song_names = []
for line in file:
# 移除行首的2位数字和".",再去除两端的空白字符
clean_name = line.strip()[3:]
song_names.append(clean_name)
return song_names
except (UnicodeDecodeError, LookupError):
continue
raise ValueError("无法读取文件,文件编码不受支持")
def replace_track_names(cue_content, song_names):
for i, song_name in enumerate(song_names, start=1):
old_track = f"音轨{i:02}"
new_track = song_name
cue_content = cue_content.replace(old_track, new_track)
return cue_content
def process_files():
song_file_path = song_file_entry.get()
cue_file_path = cue_file_entry.get()
if not song_file_path or not cue_file_path:
messagebox.showerror("错误", "请选择文件")
return
try:
song_names = read_file(song_file_path)
# 尝试以不同编码读取CUE文件
cue_content = None
for encoding in ['utf-8', 'ansi']:
try:
with open(cue_file_path, 'r', encoding=encoding) as cue_file:
cue_content = cue_file.read()
break
except (UnicodeDecodeError, LookupError):
continue
if cue_content is None:
raise ValueError("无法读取CUE文件,文件编码不受支持")
new_cue_content = replace_track_names(cue_content, song_names)
with open(cue_file_path, 'w', encoding='utf-8') as cue_file:
cue_file.write(new_cue_content)
messagebox.showinfo("成功", "文件处理完成")
except Exception as e:
messagebox.showerror("错误", f"处理文件时出错: {e}")
def browse_song_file():
file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
song_file_entry.delete(0, tk.END)
song_file_entry.insert(0, file_path)
def browse_cue_file():
file_path = filedialog.askopenfilename(filetypes=[("CUE files", "*.cue")])
cue_file_entry.delete(0, tk.END)
cue_file_entry.insert(0, file_path)
def drop_song_file(event):
file_path = event.data.strip('{}') # 删除路径中的花括号{}
song_file_entry.delete(0, tk.END)
song_file_entry.insert(0, file_path)
def drop_cue_file(event):
file_path = event.data.strip('{}') # 删除路径中的花括号{}
cue_file_entry.delete(0, tk.END)
cue_file_entry.insert(0, file_path)
# 创建主窗口
root = TkinterDnD.Tk()
root.title("CUE文件音轨替换工具")
# 创建并放置标签和输入框
tk.Label(root, text="歌曲名列表文件:").grid(row=0, column=0, padx=10, pady=10)
song_file_entry = tk.Entry(root, width=50)
song_file_entry.grid(row=0, column=1, padx=10, pady=10)
song_file_entry.drop_target_register(DND_FILES)
song_file_entry.dnd_bind('<<Drop>>', drop_song_file)
tk.Button(root, text="浏览", command=browse_song_file).grid(row=0, column=2, padx=10, pady=10)
tk.Label(root, text="CUE文件:").grid(row=1, column=0, padx=10, pady=10)
cue_file_entry = tk.Entry(root, width=50)
cue_file_entry.grid(row=1, column=1, padx=10, pady=10)
cue_file_entry.drop_target_register(DND_FILES)
cue_file_entry.dnd_bind('<<Drop>>', drop_cue_file)
tk.Button(root, text="浏览", command=browse_cue_file).grid(row=1, column=2, padx=10, pady=10)
# 创建并放置处理按钮
tk.Button(root, text="处理文件", command=process_files).grid(row=2, column=1, padx=10, pady=20)
# 运行主循环
root.mainloop()