Luis-Filipe commited on
Commit
438480d
·
verified ·
1 Parent(s): 4640c05

Upload 5 files

Browse files
ExtratorLastFrame.bat ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ @echo off
2
+ start /b pythonw "C:\Users\luisf\extract_last_frame_gui.py"
extract_last_frame.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import sys
3
+ import os
4
+
5
+ def extract_last_frame(video_path):
6
+ if not os.path.exists(video_path):
7
+ print(f"Erro: O ficheiro {video_path} não existe.")
8
+ return
9
+
10
+ # Abre o vídeo
11
+ cap = cv2.VideoCapture(video_path)
12
+
13
+ if not cap.isOpened():
14
+ print(f"Erro: Não foi possível abrir o vídeo {video_path}.")
15
+ return
16
+
17
+ # Obtém o número total de frames
18
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
19
+
20
+ if total_frames <= 0:
21
+ print("Erro: O vídeo parece não ter frames.")
22
+ cap.release()
23
+ return
24
+
25
+ # Define a posição para o último frame
26
+ cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1)
27
+
28
+ # Lê o frame
29
+ success, frame = cap.read()
30
+
31
+ if success:
32
+ # Constrói o nome do ficheiro de saída
33
+ base_name, ext = os.path.splitext(video_path)
34
+ output_path = f"{base_name}-last-frame.jpg"
35
+
36
+ # Guarda o frame
37
+ cv2.imwrite(output_path, frame)
38
+ print(f"Sucesso! Último frame guardado em: {output_path}")
39
+ else:
40
+ print("Erro: Não foi possível ler o último frame.")
41
+
42
+ cap.release()
43
+
44
+ if __name__ == "__main__":
45
+ if len(sys.argv) < 2:
46
+ print("Uso: python extract_last_frame.py <caminho_do_video>")
47
+ else:
48
+ for arg in sys.argv[1:]:
49
+ extract_last_frame(arg)
extract_last_frame_gui.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import sys
4
+ from tkinter import messagebox, Label
5
+ from tkinterdnd2 import TkinterDnD, DND_FILES
6
+
7
+ def extract_last_frame(video_path):
8
+ # Remove chavetas que o Windows por vezes coloca em caminhos com espaços
9
+ video_path = video_path.strip('{}').strip('"')
10
+
11
+ if not os.path.exists(video_path):
12
+ return False, f"O ficheiro não existe: {video_path}"
13
+
14
+ try:
15
+ cap = cv2.VideoCapture(video_path)
16
+ if not cap.isOpened():
17
+ return False, f"Não foi possível abrir o vídeo: {os.path.basename(video_path)}"
18
+
19
+ total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
20
+ if total_frames <= 0:
21
+ cap.release()
22
+ return False, "O vídeo parece não ter frames."
23
+
24
+ cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1)
25
+ success, frame = cap.read()
26
+
27
+ if success:
28
+ base_name, _ = os.path.splitext(video_path)
29
+ output_path = f"{base_name}-last-frame.jpg"
30
+ cv2.imwrite(output_path, frame)
31
+ cap.release()
32
+ return True, output_path
33
+ else:
34
+ cap.release()
35
+ return False, "Não foi possível ler o último frame."
36
+ except Exception as e:
37
+ return False, str(e)
38
+
39
+ class App(TkinterDnD.Tk):
40
+ def __init__(self):
41
+ super().__init__()
42
+ self.title("Extrator de Último Frame")
43
+ self.geometry("400x300")
44
+
45
+ # Estilo simples
46
+ self.configure(bg='#f0f0f0')
47
+
48
+ self.label = Label(
49
+ self,
50
+ text="Arraste o vídeo para aqui\n(MP4, AVI, MKV, etc.)",
51
+ padx=20, pady=20,
52
+ bg="white", relief="groove",
53
+ font=("Arial", 12),
54
+ cursor="hand2"
55
+ )
56
+ self.label.pack(expand=True, fill="both", padx=40, pady=40)
57
+
58
+ # Registar o Drop
59
+ self.label.drop_target_register(DND_FILES)
60
+ self.label.dnd_bind('<<Drop>>', self.handle_drop)
61
+
62
+ def handle_drop(self, event):
63
+ # O event.data pode conter múltiplos ficheiros
64
+ # No Windows, caminhos com espaços vêm entre chavetas ou aspas
65
+ import re
66
+ data = event.data
67
+ # Expressão regular para capturar caminhos entre chavetas ou caminhos sem espaços
68
+ files = re.findall(r'\{.*?\}|\S+', data)
69
+
70
+ for file_path in files:
71
+ file_path = file_path.strip('{}').strip('"')
72
+ success, result = extract_last_frame(file_path)
73
+ if success:
74
+ self.label.config(bg="#d4edda", text=f"Sucesso!\n{os.path.basename(result)}")
75
+ self.after(3000, self.reset_label)
76
+ else:
77
+ messagebox.showerror("Erro", f"Falha ao processar:\n{os.path.basename(file_path)}\n\nErro: {result}")
78
+
79
+ def reset_label(self):
80
+ self.label.config(bg="white", text="Arraste o vídeo para aqui\n(MP4, AVI, MKV, etc.)")
81
+
82
+ if __name__ == "__main__":
83
+ app = App()
84
+ app.mainloop()
extrair_ultimo_frame.bat ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @echo off
2
+ setlocal enabledelayedexpansion
3
+
4
+ if "%~1"=="" (
5
+ echo Arraste e solte um video para este ficheiro .bat para extrair o ultimo frame.
6
+ pause
7
+ exit /b
8
+ )
9
+
10
+ :loop
11
+ if "%~1"=="" goto end
12
+ echo Processando: %~1
13
+ python "C:\Users\luisf\extract_last_frame.py" "%~1"
14
+ shift
15
+ goto loop
16
+
17
+ :end
18
+ echo Concluido!
19
+ pause