From RidgeRun Developer Wiki
import cv2
TOTAL_NUM_FRAMES = 100
FRAMERATE = 30.0
PIPELINE = "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! appsink"
def is_camera_available():
cap = cv2.VideoCapture(PIPELINE)
if not cap.isOpened():
return False
cap.release()
return True
def main():
if is_camera_available():
print("A camera is available.")
else:
print("No camera available.")
exit()
cap = cv2.VideoCapture(PIPELINE)
if not cap.isOpened():
print("Error: Could not open video stream.")
exit()
print("Camera opened successfully.")
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, FRAMERATE, (frame_width, frame_height))
num_frames = 0
while True:
ret, frame = cap.read()
if not ret:
print("Error: Failed to capture image.")
break
out.write(frame)
print(f"Recording: {num_frames} / {TOTAL_NUM_FRAMES}", end='\r')
if num_frames >= TOTAL_NUM_FRAMES:
print("\nCapture duration reached, stopping recording.")
break
num_frames += 1
cap.release()
out.release()
if __name__ == '__main__':
main()