File size: 3,046 Bytes
7fd1a0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import cv2
import sys
from tkinter import messagebox, Label
from tkinterdnd2 import TkinterDnD, DND_FILES

def extract_last_frame(video_path):
    # Remove chavetas que o Windows por vezes coloca em caminhos com espaços
    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")
        
        # Estilo simples
        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)
        
        # Registar o Drop
        self.label.drop_target_register(DND_FILES)
        self.label.dnd_bind('<<Drop>>', self.handle_drop)

    def handle_drop(self, event):
        # O event.data pode conter múltiplos ficheiros
        # No Windows, caminhos com espaços vêm entre chavetas ou aspas
        import re
        data = event.data
        # Expressão regular para capturar caminhos entre chavetas ou caminhos sem espaços
        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()