geekcomputers/Python · error · Exception

Could not extract frame

Error message

Could not extract frame

What it means

Raised by extract_thumbnail_from_video when cv2.VideoCapture.read() fails to return a frame, meaning a usable frame could not be grabbed from the video stream. It uses a bare Exception because OpenCV gives no specific error type for this failure.

Source

Thrown at ExtractThumbnailFromVideo/extract_thumbnail_from_video.py:46

    video_capture = cv2.VideoCapture(video_path)  # Open the video file for reading
    total_frames = int(
        video_capture.get(cv2.CAP_PROP_FRAME_COUNT)
    )  # Get the total number of frames in the video
    middle_frame_index = total_frames // 2  # Calculate the index of the middle frame
    video_capture.set(
        cv2.CAP_PROP_POS_FRAMES, middle_frame_index
    )  # Seek to the middle frame
    success, frame = video_capture.read()  # Read the middle frame
    video_capture.release()  # Release the video capture object

    if success:
        frame = cv2.resize(
            frame, frame_size
        )  # Resize the frame to the specified dimensions
        thumbnail_filename = f"{os.path.basename(video_path)}_thumbnail.jpg"  # Create a filename for the thumbnail
        cv2.imwrite(thumbnail_filename, frame)  # Save the thumbnail frame as an image
    else:
        raise Exception(
            "Could not extract frame"
        )  # Raise an exception if frame extraction fails

View on GitHub (pinned to 40f4cd2652)

Solutions

  1. Verify the file exists and is a valid, non-empty video before calling (ffprobe or os.path.getsize)
  2. Check cap.isOpened() and cap.get(cv2.CAP_PROP_FRAME_COUNT) > 0 after opening the capture
  3. Install/rebuild OpenCV with ffmpeg support if codec errors are suspected
  4. Catch Exception and skip/log the failing file in batch pipelines

Example fix

# before
thumbnail = extract_thumbnail(video_path)
# after
if not os.path.isfile(video_path) or os.path.getsize(video_path) == 0:
    skip()
thumbnail = extract_thumbnail(video_path)
Defensive patterns

Strategy: validation

Validate before calling

import os
def can_extract(path):
    return os.path.isfile(path) and os.path.getsize(path) > 0

Try / catch

try:
    extract_thumbnail(video_path, ...)
except Exception:
    log.warning('skipping unreadable video: %s', video_path)

Prevention

When it happens

Trigger: Passing a corrupt/truncated video file, a path to a nonexistent video (VideoCapture opens but reads nothing), an unsupported codec, or a video with zero readable frames; frame is None after cap.read().

Common situations: Batch-processing user-uploaded videos where some are partially uploaded or encoded with an exotic codec; missing OpenCV codec support (ffmpeg backing) in the environment; wrong file path or a 0-byte file.

Related errors


AI-assisted analysis of geekcomputers/Python@40f4cd2652 (2026-08-27). Data as JSON: /api/errors/bb3d7e069c4c444f. Report an issue: GitHub.