My knolowedge on this is very poor.That is normal, your script is running in a loop which does not allow the CPU to "breathe" and that causes a lot of CPU-time. I've made some untested modifications to your "File1.py" which will allow the script to both handle conversion with ffmpeg and deferring uploads to and external script. By doing it like this, everything can be handled from a single script. See it as inspiration....Code:
from picamera2.encoders import H264Encoder, Qualityfrom picamera2.outputs import CircularOutputfrom picamera2 import Picamera2import time, os, subprocessfrom datetime import datetimeimport RPi.GPIO as GPIOpicam2 = Picamera2()video_config = picam2.create_video_configuration(main={"size":(1280,960)})picam2.configure(video_config)encoder = H264Encoder(bitrate=10000000)output = CircularOutput()picam2.start_recording(encoder, output) #####GPIO.setmode(GPIO.BCM)GPIO_PIR = 4#print("PIR Module Holding Time Test (CTRL-C to exit)")#GPIO.setup(GPIO_PIR,GPIO.IN)Current_State = 0Previous_State = 0# Start: used for remux and uploadffmpeg = Nonemotions = []uploading = False# Initialize unconverted motionswith os.scandir('/home/pi/Video/') as items: for item in items: if item.is_file() and item.name.endswith('.h264'): motions += [item.path]def remux_and_upload(): global ffmpeg, motions, uploading if (ffmpeg == None) and (len(motions) > 0): #Start conversion ffmpeg = subprocess.Popen(['/usr/bin/ffmpeg', '-i', motions[0], '-c copy', motions[0][:-4] + 'mp4']) elif (ffmpeg != None) and (ffmpeg.poll() != None): if ffmpeg.returncode == 0: #Conversion succeeded, delete .h264 and upload .mp4 uploading = True os.remove(motions[0]) subprocess.Popen(['/path/to/uploadscript.py', motions[0][:-4] + 'mp4']) else: #Conversion failed print('Conversion of "' + motions[0] + '" failed with exit code ' + str(ffmpeg.returncode)) #Set ffmpeg to none to allow next conversion and remove the processed item from the list ffmpeg = None del motions[0]# End: used for remux and upload try: print("Waiting for PIR to settle ...") # Loop until PIR output is 0 while GPIO.input(GPIO_PIR)==1: Current_State = 0 print(" Ready") # Loop until users quits with CTRL-C while True : # Read PIR state Current_State = GPIO.input(GPIO_PIR) if Current_State==1 and Previous_State==0: # PIR is triggered if not uploading: start_time=time.time() print(" Motion detected!") fecha = datetime.now().strftime("%a-%d.%m.%Y-%H_%M_%S") nombre = '/home/pi/Videos/PIR-IR-' + fecha + '.h264' nombre2 = '/home/pi/Videos/PIR-IR-' + fecha + '.mp4' output.fileoutput = nombre #### output.start() # Record previous state Previous_State=1 elif Current_State==0 and Previous_State==1: # PIR has returned to ready state if not uploading: stop_time=time.time() ####picam2.stop_recording() output.stop() motions += [output.fileoutput] elapsed_time=int(stop_time-start_time) print(" (Elapsed time : " + str(elapsed_time) + " secs)") print(" Ready ") Previous_State=0 uploading=False else: remux_and_upload() #Allow CPU to breathe for 1/4 second time.sleep(.25)except KeyboardInterrupt: print(" Quit") # Reset GPIO settings GPIO.cleanup()
I get the idea to build an array with all h264 files to be converted
I had to add "import subprocess"
With an empty Videos dir I start running File1.py and make some motion for a short time.
I get
ffmpeg = subprocess.Popen(['/usr/bin/ffmpeg', '-i', motions[0], '-c copy', motions[0][:-4] + 'mp4'])
~~~~~~~~~~^^^^^
TypeError: '_io.BufferedWriter' object is not subscriptable
So here it is not possible to catch the string before 'h264'
There's also a need to delete the just converted h264 file
Thanks
Statistics: Posted by marciano — Wed May 01, 2024 12:22 am