加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
osqo.py 5.26 KB
一键复制 编辑 原始数据 按行查看 历史
愚人猫 提交于 2024-07-28 01:59 . no dependence of ffmpeg's exe file
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()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化