Recording video lets you capture moving images and sound to save memories or monitor places.
You want to make a short movie with your Raspberry Pi camera.
You need to record a time-lapse of a plant growing.
You want to monitor your home or garden with video.
You are making a project that needs video input for analysis.
Syntax
Raspberry Pi
import picamera
import time
with picamera.PiCamera() as camera:
camera.start_recording('video.h264')
time.sleep(10) # record for 10 seconds
camera.stop_recording()
Use picamera library to control the Raspberry Pi camera.
start_recording() begins saving video to a file.
Examples
This records a 5-second video and saves it as my_video.h264.
Raspberry Pi
import picamera
with picamera.PiCamera() as camera:
camera.start_recording('my_video.h264')
camera.wait_recording(5) # record for 5 seconds
camera.stop_recording()
This records video in 3 parts of 2 seconds each, printing progress.
Raspberry Pi
import picamera
import time
with picamera.PiCamera() as camera:
camera.start_recording('timelapse.h264')
for i inrange(3):
camera.wait_recording(2) # record 2 secondsprint(f'Captured {i+1} segments')
camera.stop_recording()
Sample Program
This program records a 3-second video and saves it. It prints messages before and after recording.
Raspberry Pi
import picamera
import time
with picamera.PiCamera() as camera:
print('Starting video recording for 3 seconds...')
camera.start_recording('test_video.h264')
time.sleep(3)
camera.stop_recording()
print('Recording stopped. Video saved as test_video.h264')
OutputSuccess
Important Notes
Make sure your Raspberry Pi camera is enabled in settings before running the code.
The video file is saved in .h264 format, which can be played with VLC or converted to MP4.
Use time.sleep() or camera.wait_recording() to control how long to record.
Summary
Use the picamera library to record video on Raspberry Pi.
Start recording with start_recording() and stop with stop_recording().
Control recording length with time.sleep() or wait_recording().
Practice
(1/5)
1. What is the main function of start_recording() in the picamera library on Raspberry Pi?
easy
A. To pause the video recording
B. To stop the video recording
C. To begin capturing video and save it to a file
D. To preview the camera feed without recording
Solution
Step 1: Understand the purpose of start_recording()
This function initiates the process of capturing video from the camera and saving it to a specified file.
Step 2: Differentiate from other functions
stop_recording() ends the recording, while start_recording() begins it. Previewing is done by start_preview().
Final Answer:
To begin capturing video and save it to a file -> Option C
Quick Check:
start_recording() starts video capture [OK]
Hint: Remember: start_recording() means begin saving video [OK]
Common Mistakes:
Confusing start_recording() with stop_recording()
Thinking start_recording() previews video
Assuming it pauses recording
2. Which of the following is the correct way to stop a video recording using the picamera library?
easy
A. camera.stop()
B. camera.stop_recording()
C. camera.end_recording()
D. camera.finish_recording()
Solution
Step 1: Recall the method to stop recording
The picamera library uses stop_recording() to end video capture.
Step 2: Verify method names
Other options like stop(), end_recording(), or finish_recording() are not valid methods in picamera.
Believing video is not saved without explicit save
4. Identify the error in this code snippet for recording video on Raspberry Pi:
from picamera import PiCamera
import time
camera = PiCamera()
camera.start_recording('video.h264')
time.wait(3)
camera.stop_recording()
medium
A. stop_recording() should be called before start_recording()
B. Missing camera initialization
C. File name must be .mp4 not .h264
D. Incorrect method name 'time.wait' instead of 'time.sleep'
Solution
Step 1: Check the time delay function
The code uses time.wait(3), but the correct function to pause is time.sleep(3).
Step 2: Confirm other parts are correct
Camera is initialized, start and stop recording are in correct order, and .h264 is a valid format.
Final Answer:
Incorrect method name 'time.wait' instead of 'time.sleep' -> Option D
Quick Check:
Use sleep() to pause, not wait() [OK]
Hint: Use time.sleep() to pause, not time.wait() [OK]
Common Mistakes:
Using time.wait() instead of time.sleep()
Thinking .h264 is invalid file extension
Calling stop_recording() before start_recording()
5. You want to record a video on Raspberry Pi that automatically stops after 10 seconds without using time.sleep(). Which method should you use to control recording duration?
hard
A. Use camera.wait_recording(10) after start_recording()
B. Use time.sleep(10) before start_recording()
C. Call stop_recording() immediately after start_recording()
D. Use camera.record_for(10) method
Solution
Step 1: Understand wait_recording() usage
This method pauses the program while continuing recording, allowing precise control without blocking the whole program.
Step 2: Compare with other options
time.sleep(10) pauses the entire program, stop_recording() immediately stops recording, and record_for() does not exist.
Final Answer:
Use camera.wait_recording(10) after start_recording() -> Option A
Quick Check:
wait_recording() controls duration without sleep [OK]
Hint: Use wait_recording() to record timed video without sleep [OK]
Common Mistakes:
Using sleep() which blocks whole program
Calling stop_recording() too soon
Assuming record_for() method exists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.