[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import filedialog, messagebox
from moviepy.editor import VideoFileClip
from pydub import AudioSegment
class VideoAudioExtractor:
def __init__(self, root):
self.root = root
self.root.title("视频音频提取器")
# 设置窗口大小
self.root.geometry("350x250")
self.video_label = tk.Label(root, text="选择视频文件:")
self.video_label.grid(row=0, column=0, padx=10, pady=5, sticky="w")
self.video_path = tk.StringVar()
self.video_entry = tk.Entry(root, textvariable=self.video_path, width=20)
self.video_entry.grid(row=0, column=1, padx=10, pady=5)
self.browse_button = tk.Button(root, text="浏览", command=self.browse_video)
self.browse_button.grid(row=0, column=2, padx=10, pady=5)
self.extract_button = tk.Button(root, text="提取音频", command=self.extract_audio)
self.extract_button.grid(row=1, column=1, padx=10, pady=5)
self.result_label = tk.Label(root, text="提取结果:")
self.result_label.grid(row=2, column=0, padx=10, pady=5, sticky="w")
self.result_text = tk.Text(root, width=30, height=8) # 调整高度为8
self.result_text.grid(row=2, column=1, columnspan=2, padx=10, pady=5)
self.result_text.insert(tk.END, "欢迎使用视频音频提取器!")
self.result_text.config(state="disabled")
def browse_video(self):
file_path = filedialog.askopenfilename(filetypes=[("Video Files", "*.mp4;*.avi;*.mkv")])
if file_path:
self.video_path.set(file_path)
def extract_audio(self):
video_path = self.video_path.get()
if video_path:
try:
video_clip = VideoFileClip(video_path)
audio = video_clip.audio
audio_path = video_path.replace(".mp4", ".mp3") # 修改文件格式为 mp3
audio.write_audiofile(audio_path)
audio.close()
video_clip.close()
self.result_text.config(state="normal")
self.result_text.delete(1.0, tk.END)
self.result_text.insert(tk.END, "音频提取完成!\n")
self.result_text.insert(tk.END, f"音频文件保存为: {audio_path}\n")
self.result_text.config(state="disabled")
messagebox.showinfo("完成", "音频提取完成!")
except Exception as e:
messagebox.showerror("错误", f"提取音频时出现错误:\n{str(e)}")
if __name__ == "__main__":
root = tk.Tk()
app = VideoAudioExtractor(root)
root.mainloop()