1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| import tkinter as tk from tkinter import filedialog, messagebox import pysrt
class SRTtoLRCConverter: def __init__(self, master): self.master = master self.master.title("SRT 到 LRC 转换器") self.master.configure(bg="#E6E6FA")
self.srt_file_path = "" self.lrc_file_path = ""
self.srt_textbox = tk.Text(self.master, wrap="word", width=50, height=10, bg="#BEBEBE") self.srt_textbox.grid(row=0, column=0, columnspan=2)
self.lrc_textbox = tk.Text(self.master, wrap="word", width=50, height=10, bg="#BEBEBE") self.lrc_textbox.grid(row=1, column=0, columnspan=2)
self.open_srt_button = tk.Button(self.master, text="打开SRT文件", command=self.open_srt) self.open_srt_button.grid(row=2, column=0)
self.convert_button = tk.Button(self.master, text="转换", command=self.convert) self.convert_button.grid(row=2, column=1)
self.save_lrc_button = tk.Button(self.master, text="保存LRC文件", command=self.save_lrc) self.save_lrc_button.grid(row=3, column=0)
self.save_lrc_entry = tk.Entry(self.master, width=50, bg="#CDC1C5") self.save_lrc_entry.grid(row=3, column=1)
def open_srt(self): self.srt_file_path = filedialog.askopenfilename(filetypes=[("SRT Files", "*.srt")])
if self.srt_file_path: with open(self.srt_file_path, "r", encoding="utf-8") as f: srt_content = f.read() self.srt_textbox.insert("1.0", srt_content)
def convert(self): if not self.srt_file_path: messagebox.showerror("Error", "请先打开一个SRT文件。") return
subs = pysrt.open(self.srt_file_path) lrc_content = ""
for sub in subs: time_str = sub.start.to_time().strftime('%H:%M:%S,%f')[:-3] hh_mm_ss_ms = time_str.split(':') hh, mm, ss, ms = int(hh_mm_ss_ms[0]), int(hh_mm_ss_ms[1]), int(hh_mm_ss_ms[2].split(',')[0]), int(hh_mm_ss_ms[2].split(',')[1])
mm += hh * 60
ms = f"{ms:03d}"
lrc_time = f"{mm:02d}:{ss:02d}.{ms}"
lrc_content += f"[{lrc_time}]{sub.text}\n"
self.lrc_textbox.delete("1.0", tk.END) self.lrc_textbox.insert("1.0", lrc_content)
def save_lrc(self): if not self.lrc_file_path: self.lrc_file_path = filedialog.asksaveasfilename( defaultextension=".lrc", filetypes=[("LRC Files", "*.lrc")], initialfile="output.lrc", initialdir=".", )
if self.lrc_file_path: self.save_lrc_entry.delete(0, tk.END) self.save_lrc_entry.insert(0, self.lrc_file_path) with open(self.lrc_file_path, "w", encoding="utf-8") as f: f.write(self.lrc_textbox.get("1.0", tk.END))
root = tk.Tk()
app = SRTtoLRCConverter(root)
root.mainloop()
|