Recording video in Raspberry Pi - Time & Space Complexity
When recording video on a Raspberry Pi, the time it takes to process frames matters a lot.
We want to know how the time grows as the video length or frame count increases.
Analyze the time complexity of the following code snippet.
import picamera
import time
with picamera.PiCamera() as camera:
camera.start_recording('video.h264')
camera.wait_recording(10) # record for 10 seconds
camera.stop_recording()
This code records a video for a fixed time period using the Raspberry Pi camera.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Capturing and processing each video frame continuously during recording.
- How many times: Once per frame, repeated many times per second for the duration of recording.
As the recording time increases, the number of frames processed grows proportionally.
| Input Size (seconds) | Approx. Frames Processed |
|---|---|
| 10 | ~300 (assuming 30 fps) |
| 100 | ~3000 |
| 1000 | ~30000 |
Pattern observation: The total work grows linearly with the recording duration.
Time Complexity: O(n)
This means the time to record grows directly in proportion to how long you record.
[X] Wrong: "Recording longer doesn't affect processing time much because the camera handles it."
[OK] Correct: Each frame still needs to be processed and saved, so longer recording means more frames and more work.
Understanding how processing time grows with input size helps you design efficient video applications on Raspberry Pi and explain your reasoning clearly.
"What if we changed the frame rate from 30 fps to 60 fps? How would the time complexity change?"
