Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
3.1.2 Video Module
Introduction
In addition to taking photos, the Camera Module can also be used to record videos.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
|---|---|
Experimental Procedures
Step 1: Go into the Raspberry Pi Desktop. You may need a screen for a better experience, refer to: Connect your Raspberry Pi. Or access the Raspberry Pi desktop remotely, for a detailed tutorial please refer to Remote Desktop.
Step 2: Open a Terminal and get into the folder of the code.
cd ~/raphael-kit/python/
Step 3: Run.
sudo python3 3.1.2_VideoModule.py
Run the code to start recording. Press Ctrl+C to end the recording. Name the video my_video.h264 and store it in the ~ directory.
Note
You can also open 3.1.2_PhotographModule.py in the ~/raphael-kit/python/ path with a Python IDE, click Run button to run, and stop the code with Stop button.
If you want to send photos to your PC, please refer to FileZilla Software.
Code
#!/usr/bin/env python3
import time
from picamera2 import Picamera2, Preview
from picamera2.encoders import H264Encoder
from picamera2.outputs import FfmpegOutput
import os
# Get the current user's login name
user = os.getlogin()
# Get the path to the user's home directory
user_home = os.path.expanduser(f'~{user}')
# Create a Picamera2 instance
camera = Picamera2()
# Retrieve the default preview configuration
preview_config = camera.preview_configuration
try:
# Configure preview size and format
preview_config.size = (800, 600)
preview_config.format = 'XRGB8888'
# Start the camera preview in QTGL mode
camera.start_preview(Preview.QTGL)
# Define video configuration with size, frame rate, and buffer count
conf = {'size': (800, 600)}
controls = {'FrameRate': 40}
config = camera.create_video_configuration(main=conf, controls=controls, buffer_count=12)
# Create a video encoder with a specified bitrate
encoder = H264Encoder(bitrate=10000000)
# Define output file for the video
output = FfmpegOutput(f'{user_home}/my_video.mp4')
# Configure and start recording
camera.configure(config)
camera.start_recording(encoder, output)
# Record for 10 seconds
time.sleep(10)
# Stop the recording
camera.stop_recording()
except KeyboardInterrupt:
# Stop the camera preview if a KeyboardInterrupt (e.g., Ctrl+C) occurs
camera.stop_preview()
pass
Code Explanation
Imports necessary libraries and classes. The
picamera2library is used for camera control,H264Encoderfor video encoding, andFfmpegOutputfor defining the video output file.#!/usr/bin/env python3 import time from picamera2 import Picamera2, Preview from picamera2.encoders import H264Encoder from picamera2.outputs import FfmpegOutput import os
Retrieves the current user’s login name and the path to their home directory.
# Get the current user's login name user = os.getlogin() # Get the path to the user's home directory user_home = os.path.expanduser(f'~{user}')
Creates an instance of the
Picamera2class and retrieves the default preview configuration.# Create a Picamera2 instance camera = Picamera2() # Retrieve the default preview configuration preview_config = camera.preview_configuration
Sets the size and format of the camera preview. The size is set to 800x600 pixels, and the format is set to
XRGB8888.try: # Configure preview size and format preview_config.size = (800, 600) preview_config.format = 'XRGB8888'
Starts the camera preview in QTGL mode, a graphical preview mode.
try: ... # Start the camera preview in QTGL mode camera.start_preview(Preview.QTGL) ...
Defines the video configuration with a frame size of 800x600 pixels and a frame rate of 40 frames per second.
try: ... # Define video configuration with size, frame rate, and buffer count conf = {'size': (800, 600)} controls = {'FrameRate': 40} config = camera.create_video_configuration(main=conf, controls=controls, buffer_count=12) ...
Creates a video encoder with a specified bitrate of 10 Mbps, using the H.264 encoding format. Sets up the output file path for the video, saving it as
my_video.mp4in the user’s home directory.try: ... # Create a video encoder with a specified bitrate encoder = H264Encoder(bitrate=10000000) # Define output file for the video output = FfmpegOutput(f'{user_home}/my_video.mp4') ...
Configures the camera with the defined video settings, starts recording using the specified encoder and output file, records for 10 seconds, and then stops the recording.
try: ... # Configure and start recording camera.configure(config) camera.start_recording(encoder, output) # Record for 10 seconds time.sleep(10) # Stop the recording camera.stop_recording()
This block of code handles a KeyboardInterrupt (such as Ctrl+C) by stopping the camera preview. The
passstatement is used to handle the exception without doing anything else.except KeyboardInterrupt: # Stop the camera preview if a KeyboardInterrupt (e.g., Ctrl+C) occurs camera.stop_preview() pass