Spaces:
Running on Zero
Running on Zero
refactor: optimize video frame extraction by implementing direct seek-based sampling with duration-based fallback
Browse files
app.py
CHANGED
|
@@ -153,28 +153,26 @@ def log_raw_model_output(session_id: str, **record) -> None:
|
|
| 153 |
def load_video(video_path, max_frames=64):
|
| 154 |
try:
|
| 155 |
container = av.open(video_path)
|
| 156 |
-
frames = []
|
| 157 |
stream = container.streams.video[0]
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
| 162 |
for frame in container.decode(video=0):
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
indices = [int(i * len(temp_frames) / max_frames) for i in range(max_frames)]
|
| 166 |
-
frames = [temp_frames[i] for i in indices]
|
| 167 |
-
else:
|
| 168 |
-
frames = temp_frames
|
| 169 |
-
else:
|
| 170 |
-
indices = [int(i * total_frames / max_frames) for i in range(max_frames)]
|
| 171 |
-
current_idx = 0
|
| 172 |
-
for i, frame in enumerate(container.decode(video=0)):
|
| 173 |
-
if current_idx < len(indices) and i == indices[current_idx]:
|
| 174 |
-
frames.append(frame.to_image())
|
| 175 |
-
current_idx += 1
|
| 176 |
-
if current_idx >= len(indices):
|
| 177 |
-
break
|
| 178 |
container.close()
|
| 179 |
return frames
|
| 180 |
except Exception as e:
|
|
|
|
| 153 |
def load_video(video_path, max_frames=64):
|
| 154 |
try:
|
| 155 |
container = av.open(video_path)
|
|
|
|
| 156 |
stream = container.streams.video[0]
|
| 157 |
+
duration = stream.duration
|
| 158 |
+
if duration is None or duration <= 0:
|
| 159 |
+
# Fallback to full decode for short/broken streams
|
| 160 |
+
frames = []
|
| 161 |
+
for frame in container.decode(video=0):
|
| 162 |
+
frames.append(frame.to_image())
|
| 163 |
+
if len(frames) > max_frames:
|
| 164 |
+
indices = [int(i * len(frames) / max_frames) for i in range(max_frames)]
|
| 165 |
+
return [frames[i] for i in indices]
|
| 166 |
+
return frames
|
| 167 |
|
| 168 |
+
# Smart sampling
|
| 169 |
+
indices = [int(i * duration / max_frames) for i in range(max_frames)]
|
| 170 |
+
frames = []
|
| 171 |
+
for ts in indices:
|
| 172 |
+
container.seek(ts, stream=stream)
|
| 173 |
for frame in container.decode(video=0):
|
| 174 |
+
frames.append(frame.to_image())
|
| 175 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
container.close()
|
| 177 |
return frames
|
| 178 |
except Exception as e:
|