Skip to content

Instantly share code, notes, and snippets.

@idriszmy
Last active May 23, 2023 02:41
Show Gist options
  • Save idriszmy/94b4c4fa55b1968668aeae0ed6ed1817 to your computer and use it in GitHub Desktop.
Save idriszmy/94b4c4fa55b1968668aeae0ed6ed1817 to your computer and use it in GitHub Desktop.
Capture video with custom settings (exposure, gain, white balance, brightness, saturation, etc) for Pi Camera Module 3.
#!/usr/bin/python3
# Video result:
# https://www.facebook.com/idriszmyblog/videos/6655806037765900
import time
from gpiozero import Button
from picamera2 import Picamera2
from picamera2.encoders import H264Encoder
from picamera2.outputs import FfmpegOutput
from libcamera import controls
SW1 = Button(21)
SW2 = Button(16)
SW3 = Button(20)
picam2 = Picamera2()
video_config = picam2.create_video_configuration({"size": (1920, 1080)})
picam2.configure(video_config)
picam2.set_controls({"AeEnable": False})
picam2.set_controls({"ExposureTime": 20000})
picam2.set_controls({"AnalogueGain": 2.0})
picam2.set_controls({"AwbEnable": False})
picam2.set_controls({"Contrast": 1.0})
picam2.set_controls({"Brightness": 0.1})
picam2.set_controls({"Saturation": 1.1})
picam2.set_controls({"AfMetering": controls.AfMeteringEnum.Auto})
picam2.set_controls({"AfMode": controls.AfModeEnum.Auto})
picam2.start_preview(True)
picam2.autofocus_cycle()
encoder = H264Encoder(10000000)
output = FfmpegOutput("test.mp4")
picam2.start_recording(encoder, output)
picam2.autofocus_cycle()
try:
while True:
if SW1.is_pressed:
picam2.autofocus_cycle()
elif SW2.is_pressed:
continue
elif SW3.is_pressed:
picam2.stop_recording()
picam2.stop_preview()
except KeyboardInterrupt:
picam2.stop_recording()
picam2.stop_preview()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment