-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextractfirst5mins.py
49 lines (36 loc) · 1.3 KB
/
extractfirst5mins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# from moviepy.editor import *
# # loading video gfg
# clip = VideoFileClip("/Users/jenish/Desktop/000112.mp4")
# # getting only first 5 seconds
# clip = clip.subclip(0, 5*60)
# # showing clip
# clip.write_videofile("12151_112.mp4")
import cv2
# Define the input video path
input_video_path = '/Users/jenish/Desktop/000111.mp4'
# Define the output video path
output_video_path = '/Users/jenish/Desktop/12151_111.mp4'
# Open the video file
cap = cv2.VideoCapture(input_video_path)
# Get the video properties
fps = cap.get(cv2.CAP_PROP_FPS)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Calculate the total number of frames
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Calculate the number of frames for the first 5 minutes
frames_to_extract = int(fps * 60 * 5)
# Create VideoWriter object for the output video
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
# Read and write the first 5 minutes of frames
for i in range(frames_to_extract):
ret, frame = cap.read()
if ret:
out.write(frame)
else:
break
# Release the video capture and writer objects
cap.release()
out.release()
print(f"First 5 minutes extracted and saved to {output_video_path}")