74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
|
import tkinter as tk
|
||
|
from tkinter import filedialog, messagebox
|
||
|
import cv2
|
||
|
import os
|
||
|
|
||
|
|
||
|
# GUI 界面
|
||
|
class VideoToFramesApp:
|
||
|
def __init__(self, root):
|
||
|
self.root = root
|
||
|
self.root.title("Video to Frames Converter")
|
||
|
|
||
|
# 文件选择按钮
|
||
|
self.select_button = tk.Button(
|
||
|
root, text="选择视频文件", command=self.select_video
|
||
|
)
|
||
|
self.select_button.pack(pady=10)
|
||
|
|
||
|
# 选择输出文件夹按钮
|
||
|
self.select_folder_button = tk.Button(
|
||
|
root, text="选择输出文件夹", command=self.select_output_folder
|
||
|
)
|
||
|
self.select_folder_button.pack(pady=10)
|
||
|
|
||
|
# 开始转换按钮
|
||
|
self.convert_button = tk.Button(
|
||
|
root, text="开始转换", command=self.convert_video
|
||
|
)
|
||
|
self.convert_button.pack(pady=10)
|
||
|
|
||
|
self.video_path = None
|
||
|
self.output_folder = None
|
||
|
|
||
|
def select_video(self):
|
||
|
self.video_path = filedialog.askopenfilename(
|
||
|
title="选择视频文件",
|
||
|
filetypes=(("MP4 files", "*.mp4"), ("All files", "*.*")),
|
||
|
)
|
||
|
if self.video_path:
|
||
|
messagebox.showinfo("文件已选择", f"已选择视频文件:{self.video_path}")
|
||
|
|
||
|
def select_output_folder(self):
|
||
|
self.output_folder = filedialog.askdirectory(title="选择输出文件夹")
|
||
|
if self.output_folder:
|
||
|
messagebox.showinfo("文件夹已选择", f"输出文件夹:{self.output_folder}")
|
||
|
|
||
|
def convert_video(self):
|
||
|
if not self.video_path or not self.output_folder:
|
||
|
messagebox.showwarning("错误", "请先选择视频文件和输出文件夹!")
|
||
|
return
|
||
|
|
||
|
try:
|
||
|
cap = cv2.VideoCapture(self.video_path)
|
||
|
count = 0
|
||
|
while True:
|
||
|
ret, frame = cap.read()
|
||
|
if not ret:
|
||
|
break
|
||
|
frame_filename = os.path.join(self.output_folder, f"frame_{count}.jpg")
|
||
|
cv2.imwrite(frame_filename, frame)
|
||
|
count += 1
|
||
|
|
||
|
cap.release()
|
||
|
messagebox.showinfo("成功", f"成功提取了 {count} 帧图片")
|
||
|
except Exception as e:
|
||
|
messagebox.showerror("错误", f"发生错误:{e}")
|
||
|
|
||
|
|
||
|
# 启动 GUI 应用
|
||
|
if __name__ == "__main__":
|
||
|
root = tk.Tk()
|
||
|
app = VideoToFramesApp(root)
|
||
|
root.mainloop()
|