Add empty-frames guard to cv2 video loader

Summary:
## Context
When cv2.VideoCapture opens a video but cannot decode any frames (e.g., due to an unsupported codec), the frames list is empty and np.stack crashes with "need at least one array to stack" — an unhelpful error message that obscures the real problem.

## This Diff
- Add explicit check for empty frames list after cap.release() and before np.stack
- Raise RuntimeError with descriptive message about possible causes (corrupted file, empty video, or unsupported codec)

Differential Revision: D94142839

fbshipit-source-id: 89a973d067d83439039734712d18727b06d0706d
This commit is contained in:
Haoran Wang
2026-02-24 10:28:32 -08:00
committed by meta-codesync[bot]
parent 2d08d73120
commit f6e51f5950
+6
View File
@@ -300,6 +300,12 @@ def load_video_frames_from_video_file_using_cv2(
cap.release()
pbar.close()
if len(frames) == 0:
raise RuntimeError(
f"No frames could be decoded from video: {video_path}. "
f"The file may be corrupted, empty, or encoded with an unsupported codec."
)
# Convert to tensor
frames_np = np.stack(frames, axis=0).astype(np.float32) # (T, H, W, C)
video_tensor = torch.from_numpy(frames_np).permute(0, 3, 1, 2) # (T, C, H, W)