File size: 1,404 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
import cv2
import sys
import os

def extract_last_frame(video_path):
    if not os.path.exists(video_path):
        print(f"Erro: O ficheiro {video_path} não existe.")
        return

    # Abre o vídeo
    cap = cv2.VideoCapture(video_path)
    
    if not cap.isOpened():
        print(f"Erro: Não foi possível abrir o vídeo {video_path}.")
        return

    # Obtém o número total de frames
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    
    if total_frames <= 0:
        print("Erro: O vídeo parece não ter frames.")
        cap.release()
        return

    # Define a posição para o último frame
    cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1)
    
    # Lê o frame
    success, frame = cap.read()
    
    if success:
        # Constrói o nome do ficheiro de saída
        base_name, ext = os.path.splitext(video_path)
        output_path = f"{base_name}-last-frame.jpg"
        
        # Guarda o frame
        cv2.imwrite(output_path, frame)
        print(f"Sucesso! Último frame guardado em: {output_path}")
    else:
        print("Erro: Não foi possível ler o último frame.")

    cap.release()

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Uso: python extract_last_frame.py <caminho_do_video>")
    else:
        for arg in sys.argv[1:]:
            extract_last_frame(arg)