import av import time def sample(N, K): array = list(range(N)) length = len(array) if K >= length or K<2: return array k = length // K sampled_points = [array[i] for i in range(0, length, k)][:K-1] sampled_points.append(array[-1]) return sampled_points start = time.time() with av.open('/Users/monius/Documents/YueZhang/Video/Sora/1.mp4') as container: frames = [] src = container.streams.video[0] time_base = src.time_base framerate = src.average_rate N = src.frames K = 10 for i in sample(N, K): n = round((i / framerate) / time_base) container.seek(n, backward=True, stream=src) frame = next(container.decode(video=0)) im = frame.to_image() frames.append(im) elapsed = time.time() - start print(elapsed)