-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.py
More file actions
55 lines (41 loc) · 1.5 KB
/
Copy pathTest.py
File metadata and controls
55 lines (41 loc) · 1.5 KB
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
50
51
52
53
54
55
import cv2
# Set desired frame rate and total number of frames
frame_rate = 30 # 30 frames per second
total_frames = 900
# Set video dimensions (width, height)
frame_width = 640
frame_height = 480
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('captured_video.avi', fourcc, frame_rate, (frame_width, frame_height))
# Initialize video capture
cap = cv2.VideoCapture(1, cv2.CAP_DSHOW) # Change 0 to the video file path if you want to capture from a file
# Check if the camera/video file is opened successfully
if not cap.isOpened():
print("Error: Could not open camera or video file.")
exit()
# Variables to keep track of frames captured
frame_count = 0
# Loop to capture frames
while frame_count < total_frames:
# Capture frame-by-frame
ret, frame = cap.read()
# Check if frame is read correctly
if not ret:
print("Error: Could not read frame.")
break
# Write the frame to the video file
out.write(frame)
# Display the frame (optional)
cv2.imshow('Frame', frame)
# Increment frame count
frame_count += 1
# Wait for a short duration to achieve the desired frame rate
cv2.waitKey(int(1000 / frame_rate))
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release video capture object, VideoWriter object, and close any open windows
cap.release()
out.release()
cv2.destroyAllWindows()