| import os
|
| import cv2
|
| import sys
|
| from tkinter import messagebox, Label
|
| from tkinterdnd2 import TkinterDnD, DND_FILES
|
|
|
| def extract_last_frame(video_path):
|
|
|
| video_path = video_path.strip('{}').strip('"')
|
|
|
| if not os.path.exists(video_path):
|
| return False, f"O ficheiro não existe: {video_path}"
|
|
|
| try:
|
| cap = cv2.VideoCapture(video_path)
|
| if not cap.isOpened():
|
| return False, f"Não foi possível abrir o vídeo: {os.path.basename(video_path)}"
|
|
|
| total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| if total_frames <= 0:
|
| cap.release()
|
| return False, "O vídeo parece não ter frames."
|
|
|
| cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1)
|
| success, frame = cap.read()
|
|
|
| if success:
|
| base_name, _ = os.path.splitext(video_path)
|
| output_path = f"{base_name}-last-frame.jpg"
|
| cv2.imwrite(output_path, frame)
|
| cap.release()
|
| return True, output_path
|
| else:
|
| cap.release()
|
| return False, "Não foi possível ler o último frame."
|
| except Exception as e:
|
| return False, str(e)
|
|
|
| class App(TkinterDnD.Tk):
|
| def __init__(self):
|
| super().__init__()
|
| self.title("Extrator de Último Frame")
|
| self.geometry("400x300")
|
|
|
|
|
| self.configure(bg='#f0f0f0')
|
|
|
| self.label = Label(
|
| self,
|
| text="Arraste o vídeo para aqui\n(MP4, AVI, MKV, etc.)",
|
| padx=20, pady=20,
|
| bg="white", relief="groove",
|
| font=("Arial", 12),
|
| cursor="hand2"
|
| )
|
| self.label.pack(expand=True, fill="both", padx=40, pady=40)
|
|
|
|
|
| self.label.drop_target_register(DND_FILES)
|
| self.label.dnd_bind('<<Drop>>', self.handle_drop)
|
|
|
| def handle_drop(self, event):
|
|
|
|
|
| import re
|
| data = event.data
|
|
|
| files = re.findall(r'\{.*?\}|\S+', data)
|
|
|
| for file_path in files:
|
| file_path = file_path.strip('{}').strip('"')
|
| success, result = extract_last_frame(file_path)
|
| if success:
|
| self.label.config(bg="#d4edda", text=f"Sucesso!\n{os.path.basename(result)}")
|
| self.after(3000, self.reset_label)
|
| else:
|
| messagebox.showerror("Erro", f"Falha ao processar:\n{os.path.basename(file_path)}\n\nErro: {result}")
|
|
|
| def reset_label(self):
|
| self.label.config(bg="white", text="Arraste o vídeo para aqui\n(MP4, AVI, MKV, etc.)")
|
|
|
| if __name__ == "__main__":
|
| app = App()
|
| app.mainloop()
|
|
|