[Python] Raspberry Pi 5 takes more than 1:30 seconds to capture 40s video using IP Camera

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I am capturing video using OpenCV in Python in Raspberry PI 5 B+, but I'm encountering significant issues with the duration and quality of the video.

    Details:

    Duration Issue:

    I set the duration for video capture to 40 seconds, but the total time taken for the script—from the log message logging.info(f"Capturing video at {video_path}") to logging.info(f"Finished Capturing video at {video_path}")—is approximately 1 minute and 40 seconds each time. This is much longer than expected.

    Frame Quality Issue:

    During the first 10 seconds of the captured video, the frames appear good and the video runs smoothly. However, after that initial period, the video becomes abrupt and skips frames. This degradation in quality affects the usability of the recorded video.

    def capture_video(cap, video_base_directory, timezone, frame_width, frame_height, fps = 20, duration=40):
    os.makedirs(video_base_directory, exist_ok=True)
    video_filename = f"video_capture_{datetime.now(timezone).strftime('%Y-%m-%d_%H-%M-%S')}.mp4"
    video_path = os.path.join(video_base_directory, video_filename)
    video_codec = cv2.VideoWriter_fourcc(*'mp4v')
    video_output = cv2.VideoWriter(video_path, video_codec, fps, (frame_width, frame_height))

    total_frames = int(fps * duration)
    buffer_size = fps * duration * 0.5
    frame_buffer = []

    logging.info(f"Capturing video at {video_path}")

    for _ in range(total_frames):
    frame = cap.read()[1] # Directly read the frame
    frame_buffer.append(frame)

    if len(frame_buffer) >= buffer_size:
    for buffered_frame in frame_buffer:
    video_output.write(buffered_frame)
    frame_buffer.clear()

    logging.info(f"Finished Capturing video at {video_path}")
    video_output.release()
    return video_path, video_filename


    I was expecting 40 seconds smooth video. At least when I ran the code on my PC it used to take approximately 40 seconds and the video was smooth.

    I have experimented with multiple buffer size (20 frames - 400 frames), also directly writing to vidoe file and not having any buffer as well. The issue is same.

    Continue reading...

Compartilhe esta Página