Bird
0
0
Raspberry Piprogramming~5 mins

Recording video in Raspberry Pi

Choose your learning style9 modes available
Introduction

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 in range(3):
        camera.wait_recording(2)  # record 2 seconds
        print(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().