Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 6016

Camera board • Re: Picamera2 capture_circular no preview

$
0
0
I have managed to convert to mp4 with ffmpeg but have an issue with framerate.

I run the example below ( based on capture_circular example) but whatever I set fps to I always get 30fps, for both the h264 and the mp4, as shown by vlc or ffplay.

I am using a Pi4 , with 64bit bookworm, as the Pi5 gives an issue I assume due to no timestamps in the h264.

Any thoughts

Code:

#!/usr/bin/python3import timeimport osimport datetimeimport numpy as npfrom picamera2 import Picamera2, Previewfrom picamera2.encoders import H264Encoderfrom picamera2.outputs import CircularOutputfrom libcamera import controls# setup directoriesHome_Files  = []Home_Files.append(os.getlogin())pic_dir = "/home/" + Home_Files[0]+ "/Videos/"lsize = (640, 480)picam2 = Picamera2()video_config = picam2.create_video_configuration(main={"size": (1280, 720), "format": "RGB888"},                                                 lores={"size": lsize, "format": "YUV420"},                                                 display="lores")picam2.configure(video_config)picam2.start_preview(Preview.QTGL)encoder = H264Encoder(3000000, repeat=True)encoder.output = CircularOutput(buffersize = 150)picam2.start()picam2.start_encoder(encoder)picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})fps = 25picam2.set_controls({"FrameRate": fps})w, h = lsizeprev = Noneencoding = Falseltime = 0while True:    cur = picam2.capture_array("lores")    if prev is not None:        mse = np.square(np.subtract(cur, prev)).mean()        if mse > 50:            if not encoding:                now = datetime.datetime.now()                timestamp = now.strftime("%y%m%d%H%M%S")                encoder.output.fileoutput = pic_dir + str(timestamp) + '.h264'                encoder.output.start()                encoding = True                print("New Motion", mse)            ltime = time.time()        else:            if encoding and time.time() - ltime > 10.0:                encoder.output.stop()                encoding = False                print("Saved: ",pic_dir + str(timestamp) + '.h264')                # make mp4                cmd = 'ffmpeg -framerate ' + str(fps) + ' -i ' + pic_dir + str(timestamp) + '.h264'                 cmd += ' -c copy ' + pic_dir + str(timestamp) + '.mp4'                print(cmd)                os.system(cmd)                   prev = curpicam2.stop_encoder()

Statistics: Posted by gordon77 — Mon Feb 26, 2024 10:29 am



Viewing all articles
Browse latest Browse all 6016

Trending Articles