Take a snapshot of a video using Python

This can be done by using the ‘opencv-python’ library.

Install opencv-python

pip install opencv-python

Take the first frame of a video

import cv2

def generate_video_cover(video_path, output_path='video_cover.png'):
    # Open the video file
    cap = cv2.VideoCapture(video_path)

    # Read the first frame
    ret, frame = cap.read()

    # Check if the video file is opened successfully
    if not ret:
        print("Error: Couldn't open video file.")
        return

    # Save the first frame as an image
    cv2.imwrite(output_path, frame)

    # Release the video capture object
    cap.release()

    print(f"Video cover generated and saved as {output_path}")

# Replace 'your_video.mp4' with the path to your video file
generate_video_cover('your_video.mp4')

To capture a screenshot at 95% of the video duration

import cv2

def generate_video_cover(video_path, output_path='video_cover.png', percentage=95):
    # Open the video file
    cap = cv2.VideoCapture(video_path)

    # Get the total number of frames and frames per second
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = cap.get(cv2.CAP_PROP_FPS)

    # Calculate the target frame number at the specified percentage
    target_frame = int(total_frames * percentage / 100)

    # Set the video capture to the target frame
    cap.set(cv2.CAP_PROP_POS_FRAMES, target_frame)

    # Read the frame at the target position
    ret, frame = cap.read()

    # Check if the video file is opened successfully
    if not ret:
        print("Error: Couldn't open video file.")
        return

    # Save the frame as an image
    cv2.imwrite(output_path, frame)

    # Release the video capture object
    cap.release()

    print(f"Video cover generated at {percentage}% time and saved as {output_path}")

# Replace 'your_video.mp4' with the path to your video file
generate_video_cover('your_video.mp4')

To capture a screenshot at specific time like 3:24

import cv2

def generate_video_cover_at_time(video_path, output_path='video_cover.png', target_time='3:24'):
    # Open the video file
    cap = cv2.VideoCapture(video_path)

    # Get the frames per second (fps) of the video
    fps = cap.get(cv2.CAP_PROP_FPS)

    # Convert the target time to seconds
    minutes, seconds = map(int, target_time.split(':'))
    target_seconds = minutes * 60 + seconds

    # Calculate the target frame number
    target_frame = int(target_seconds * fps)

    # Set the video capture to the target frame
    cap.set(cv2.CAP_PROP_POS_FRAMES, target_frame)

    # Read the frame at the target position
    ret, frame = cap.read()

    # Check if the video file is opened successfully
    if not ret:
        print("Error: Couldn't open video file.")
        return

    # Save the frame as an image
    cv2.imwrite(output_path, frame)

    # Release the video capture object
    cap.release()

    print(f"Video cover generated at {target_time} and saved as {output_path}")

# Replace 'your_video.mp4' with the path to your video file
generate_video_cover_at_time('your_video.mp4', target_time='3:24')

Percentage to Screenshot time conversion

To capture a snapshot at 95% of the video duration and then compute the corresponding time, you can modify the previous script to calculate the time based on the frame at 95%. Here’s an updated script

import cv2

def generate_video_cover_and_time(video_path, output_path='video_cover.png', percentage=95):
    # Open the video file
    cap = cv2.VideoCapture(video_path)

    # Get the total number of frames and frames per second (fps)
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = cap.get(cv2.CAP_PROP_FPS)

    # Calculate the target frame number at the specified percentage
    target_frame = int(total_frames * percentage / 100)

    # Set the video capture to the target frame
    cap.set(cv2.CAP_PROP_POS_FRAMES, target_frame)

    # Read the frame at the target position
    ret, frame = cap.read()

    # Check if the video file is opened successfully
    if not ret:
        print("Error: Couldn't open video file.")
        return

    # Calculate the time corresponding to the target frame
    target_time_seconds = target_frame / fps
    target_minutes = int(target_time_seconds // 60)
    target_seconds = int(target_time_seconds % 60)
    target_time = f"{target_minutes}:{target_seconds:02d}"

    # Save the frame as an image
    cv2.imwrite(output_path, frame)

    # Release the video capture object
    cap.release()

    print(f"Video cover generated at {percentage}% time ({target_time}) and saved as {output_path}")

# Replace 'your_video.mp4' with the path to your video file
generate_video_cover_and_time('your_video.mp4', percentage=95)