-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
92 lines (71 loc) · 2.73 KB
/
example.py
File metadata and controls
92 lines (71 loc) · 2.73 KB
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
86
87
88
89
90
91
92
import pathlib
import time
import mlx.core as mx
import viteo
import cv2
def benchmark_extraction(video_path):
"""Test basic frame extraction."""
with viteo.open(video_path) as video:
print(f"---- {video_path.name} ----")
print(f"> Resolution: {video.width}x{video.height}")
print(f"> FPS: {video.fps:.2f}")
print(f"> Total frames: {video.total_frames}")
print("* Running benchmark...", end='\r')
frames_extracted = 0
num_frames = min(256, video.total_frames)
start = time.time()
for frame in video:
frames_extracted += 1
mx.eval(frame)
if frames_extracted >= num_frames:
break
elapsed = time.time() - start
fps = frames_extracted / elapsed
print(f"> {frames_extracted} frames extracted in {elapsed:.3f}s")
print(f"> {fps:.1f} fps / {1000*elapsed/frames_extracted:.3f}ms per frame\n")
def benchmark_extraction_opencv(video_path):
"""Benchmark frame extraction speed using OpenCV."""
cap = cv2.VideoCapture(str(video_path))
if not cap.isOpened():
raise RuntimeError(f"Failed to open video: {video_path}")
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
print(f"---- {video_path.name} (OpenCV) ----")
print(f"> Resolution: {width}x{height}")
print(f"> FPS: {fps:.2f}")
print(f"> Total frames: {total_frames}")
print("* Running benchmark...", end='\r')
frames_extracted = 0
num_frames = min(256, total_frames)
start = time.time()
while frames_extracted < num_frames:
ret, frame = cap.read()
if not ret:
break
frames_extracted += 1
elapsed = time.time() - start
fps = frames_extracted / elapsed
cap.release()
print(f"> {frames_extracted} frames extracted in {elapsed:.3f}s")
print(f"> {fps:.1f} fps / {1000*elapsed/frames_extracted:.3f}ms per frame\n")
if __name__ == "__main__":
import sys
if not len(sys.argv) > 1:
print("Usage: python test_viteo.py <video_file> [<video_file> ...]")
print("x No video file provided.")
sys.exit(1)
for input_path in sys.argv[1:]:
video_path = pathlib.Path(input_path)
if not video_path.is_file() or not video_path.exists():
print("x Invalid path:", str(video_path))
continue
try:
benchmark_extraction(video_path)
benchmark_extraction_opencv(video_path)
except Exception as e:
print(f"\n✗ Error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)