Created
July 18, 2020 04:14
-
-
Save constructor-s/850a36279a8e5a8a101ccb83ef5459b9 to your computer and use it in GitHub Desktop.
Raspberry Pi continous camera recording with built-in picamera and frame timestamp in Python (very low CPU usage)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import picamera | |
from picamera import PiCamera | |
import time | |
import signal | |
import sys | |
import threading | |
from datetime import datetime | |
CAMERA_WAIT_SEC = 1 | |
CAMERA_ROTATE_SEC = 1800 | |
CAMERA_H264_QUALITY = 25 | |
CAMERA_ANNOTATE_TEXT_SIZE = 16 | |
CAMERA_FPS = 24 | |
camera = PiCamera(sensor_mode=4, framerate=24) | |
# camera.rotation = 180 | |
camera.resolution = (640,480) | |
camera.annotate_text_size = CAMERA_ANNOTATE_TEXT_SIZE | |
camera.annotate_background = picamera.Color('black') | |
print(camera.sensor_mode, camera.resolution, camera.framerate) | |
stop_event = threading.Event() | |
def signal_handler(signal, frame): | |
print('Stopping...') | |
stop_event.set() | |
signal.signal(signal.SIGINT, signal_handler) | |
#now = datetime.now() | |
#file = 'Videos/' + now.strftime("%Y%m%d_%H%M%S") + '.h264' | |
#print('Starting recording to:', file) | |
#camera.start_recording(file, quality=40) | |
counter = 0 | |
while not stop_event.is_set(): | |
now = datetime.now() | |
file = 'Videos/' + now.strftime("%Y%m%d_%H%M%S") + '.h264' | |
print() | |
print('Recording to file', counter, '; filename:', file, end='') | |
sys.stdout.flush() | |
if counter == 0: | |
camera.start_recording(file, quality=CAMERA_H264_QUALITY) | |
else: | |
camera.split_recording(file, quality=CAMERA_H264_QUALITY) | |
for _ in range(round(CAMERA_ROTATE_SEC * 1.0 / CAMERA_WAIT_SEC)): | |
print('.', end='') | |
camera.annotate_text = datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
camera.wait_recording(CAMERA_WAIT_SEC) | |
if stop_event.is_set(): | |
break | |
counter += 1 | |
camera.stop_recording() |
Author
constructor-s
commented
Sep 28, 2020
@reboot screen -dmS cam bash -c 'cd /home/pi/ && python3 video_cam.py; exec bash'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment