代码拉取完成,页面将自动刷新
import os
import subprocess
import webbrowser
from tkinter import Tk, Label, Entry, Button, Listbox, Frame, Menu, filedialog, messagebox, simpledialog, ttk
from tkinter import StringVar
import ffmpeg
class AudioOptimizerApp:
def __init__(self, root):
self.root = root
self.root.title("Open Sound File Quality Optimizer(OSQO)")
# 创建菜单
menubar = Menu(self.root)
self.root.config(menu=menubar)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=self.load_files)
filemenu.add_command(label="Exit", command=self.exit_app)
menubar.add_cascade(label="File", menu=filemenu)
# 创建文件列表
self.listbox = Listbox(self.root, width=80, height=10)
self.listbox.pack(side="left", fill="both", expand=True)
# 创建控制面板
self.frame = Frame(self.root)
self.frame.pack(side="right", fill="both", expand=True)
# 添加比特率、采样频率和音量增益的下拉菜单
self.bitrate_var = StringVar(self.root)
self.sampling_freq_var = StringVar(self.root)
# self.volume_var = StringVar(self.root)
self.create_dropdown(self.frame, "Select Bitrate:", ["320 Kbit/s", "256 Kbit/s", "192 Kbit/s", "128 Kbit/s"], self.bitrate_var)
self.create_dropdown(self.frame, "Select Sampling Frequency:", ["48 kHz", "44.1 kHz", "32 kHz"], self.sampling_freq_var)
# self.create_dropdown(self.frame, "Increase Audio Volume:", ["0 dB", "5 dB", "10 dB", "15 dB"], self.volume_var)
# 添加按钮
btn_frame = Frame(self.frame)
btn_frame.pack(fill="both", expand=True)
add_button = Button(btn_frame, text="Add File", command=self.add_file)
add_button.pack(side="left", padx=10, pady=5)
remove_button = Button(btn_frame, text="Remove File", command=self.remove_file)
remove_button.pack(side="left", padx=10, pady=5)
clear_button = Button(btn_frame, text="Clear List", command=self.clear_list)
clear_button.pack(side="left", padx=10, pady=5)
optimize_button = Button(btn_frame, text="Optimize", command=self.optimize_files)
optimize_button.pack(side="left", padx=10, pady=5)
stop_button = Button(btn_frame, text="Stop", command=self.stop_optimization)
stop_button.pack(side="left", padx=10, pady=5)
def create_dropdown(self, parent, label_text, options, var):
Label(parent, text=label_text).pack(side="top", fill="x")
dropdown = ttk.Combobox(parent, values=options, textvariable=var)
dropdown.pack(side="top", fill="x")
dropdown.current(0) # 默认选择第一项
def load_files(self):
filepaths = filedialog.askopenfilenames()
for filepath in filepaths:
self.listbox.insert("end", filepath)
def exit_app(self):
self.root.destroy()
def add_file(self):
filepath = filedialog.askopenfilename()
if filepath:
self.listbox.insert("end", filepath)
def remove_file(self):
try:
index = self.listbox.curselection()[0]
self.listbox.delete(index)
except IndexError:
pass
def clear_list(self):
self.listbox.delete(0, "end")
def stop_optimization(self):
# 这里实现停止优化的逻辑(示例中不实现)
messagebox.showinfo("Info", "Stop functionality is not implemented.")
def optimize_files(self):
selected_bitrate = self.bitrate_var.get().replace(" Kbit/s", "")
selected_sampling_freq = self.sampling_freq_var.get().replace(" kHz", "")
# selected_volume_gain = int(self.volume_var.get().replace(" dB", ""))
for index in range(self.listbox.size()):
input_file = self.listbox.get(index)
# audio = AudioSegment.from_file(input_file)
# # 使用 pydub 调整音量
# audio = audio + selected_volume_gain
# 构造输出文件名
output_file = os.path.splitext(input_file)[0] + "_optimized.mp3"
try:
# 将 pydub 音频转换为 ffmpeg 可处理的临时文件
temp_file = "temp_audio.wav"
# audio.export(temp_file, format="wav")
# 执行 FFmpeg 命令
print(input_file)
print(output_file)
print(selected_bitrate)
print(selected_sampling_freq)
# 使用 ffmpeg-python 调整比特率和采样频率
(ffmpeg.input(input_file).output(output_file, ab=selected_bitrate, ar=selected_sampling_freq).run())
except ffmpeg.Error as e:
messagebox.showerror("Error", f"FFmpeg process failed: {e}")
break # 如果出现错误,中断循环
finally:
# 清理临时文件
if os.path.exists(temp_file):
os.remove(temp_file)
messagebox.showinfo("Success", "Optimization completed!")
self.open_output_folder()
def open_output_folder(self):
folder_path = os.path.dirname(self.listbox.get(0))
if folder_path:
webbrowser.open(f"file://{folder_path}")
if __name__ == "__main__":
root = Tk()
app = AudioOptimizerApp(root)
root.mainloop()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。