0% found this document useful (0 votes)
11 views3 pages

Extract Frames from Video at 1 FPS

The document contains a Python script that extracts one frame per second from a specified video file and saves them as JPEG images in an output folder. It uses the OpenCV library to read the video and Matplotlib to display the first nine extracted frames. The script also prints the frames per second (FPS) of the video and confirms the completion of the extraction process.

Uploaded by

likhithgowdas24
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Extract Frames from Video at 1 FPS

The document contains a Python script that extracts one frame per second from a specified video file and saves them as JPEG images in an output folder. It uses the OpenCV library to read the video and Matplotlib to display the first nine extracted frames. The script also prints the frames per second (FPS) of the video and confirms the completion of the extraction process.

Uploaded by

likhithgowdas24
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Untitled84

August 19, 2025

[32]: import cv2


import os
import [Link] as plt

[33]: # Input video path (your path)


video_path = r"R:\DL_Lab\3\Input\Video.mp4"
output_folder = r"R:\DL_Lab\3\Output_Frames"

[34]: # Create output folder if not exists


if not [Link](output_folder):
[Link](output_folder)

[35]: # Load the video


cap = [Link](video_path)

[36]: # Get FPS (frames per second) of the video


fps = int([Link](cv2.CAP_PROP_FPS))
print(f"FPS of video: {fps}")

frame_count = 0
saved_count = 0
saved_frames = []

while True:
ret, frame = [Link]()
if not ret:
break

# Save 1 frame every second


if frame_count % fps == 0:
rgb_frame = [Link](frame, cv2.COLOR_BGR2RGB)
frame_filename = [Link](output_folder, f"frame_{saved_count}.jpg")
[Link](frame_filename, frame)
print(f"Saved {frame_filename}")

# Store first 9 frames for display


if saved_count < 9:
saved_frames.append(rgb_frame)

1
saved_count += 1

frame_count += 1

[Link]()
print("Frame extraction (1 frame/sec) completed.")

FPS of video: 30
Saved R:\DL_Lab\3\Output_Frames\frame_0.jpg
Saved R:\DL_Lab\3\Output_Frames\frame_1.jpg
Saved R:\DL_Lab\3\Output_Frames\frame_2.jpg
Saved R:\DL_Lab\3\Output_Frames\frame_3.jpg
Saved R:\DL_Lab\3\Output_Frames\frame_4.jpg
Saved R:\DL_Lab\3\Output_Frames\frame_5.jpg
Saved R:\DL_Lab\3\Output_Frames\frame_6.jpg
Saved R:\DL_Lab\3\Output_Frames\frame_7.jpg
Saved R:\DL_Lab\3\Output_Frames\frame_8.jpg
Frame extraction (1 frame/sec) completed.

[37]: # Display extracted frames


[Link](figsize=(10, 10))
for i, img in enumerate(saved_frames):
[Link](3, 3, i + 1) # 3x3 grid
[Link](img)
[Link]("on")
[Link](f"Frame {i} (at {i} sec)")
plt.tight_layout()
[Link]()

2
[ ]:

You might also like