📖 Scenario: You have a Raspberry Pi with a camera module attached. You want to record a short video using Python code on your Raspberry Pi.
🎯 Goal: Write a Python program that records a 5-second video using the Raspberry Pi camera and saves it as my_video.h264.
📋 What You'll Learn
Create a PiCamera object
Set the video resolution to 640x480
Record a 5-second video and save it as my_video.h264
Print a message when recording starts and ends
💡 Why This Matters
🌍 Real World
Recording videos on a Raspberry Pi is useful for security cameras, time-lapse projects, or capturing events automatically.
💼 Career
Understanding how to control hardware like cameras programmatically is important for roles in embedded systems, IoT development, and robotics.
Progress0 / 4 steps
1
Setup the camera object
Import PiCamera from picamera and create a camera object called camera.
Raspberry Pi
Hint
Use from picamera import PiCamera to import the camera class.
2
Configure the camera resolution
Set the resolution property of the camera object to (640, 480).
Raspberry Pi
Hint
Use camera.resolution = (640, 480) to set the video size.
3
Record a 5-second video
Use camera.start_recording('my_video.h264') to start recording, then wait 5 seconds using camera.wait_recording(5), and finally stop recording with camera.stop_recording().
Raspberry Pi
Hint
Use start_recording, wait_recording, and stop_recording methods in order.
4
Print messages before and after recording
Add print('Recording started') before starting the recording and print('Recording stopped') after stopping the recording.
Raspberry Pi
Hint
Use two print statements: one before start_recording and one after stop_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.