Python Forum
Video recording with Raspberry Pi - What´s wrong with my python code? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Video recording with Raspberry Pi - What´s wrong with my python code? (/thread-39484.html)



Video recording with Raspberry Pi - What´s wrong with my python code? - Montezuma1502 - Feb-24-2023

Dear everyone,

for an ecological study I recorded insects in front of a nesting aid with a Raspberry Pi. I wrote a simple python code to do this which I looped via a crontab (at least I thought it was simple). After I rewatched my videos, I noticed that the recordings did not stop after one hour (as I intended) but recorded on, until my energy saving tool (a wittyPi) powered the Raspberry Pi down (and I wondered why a couple of USB-sticks kicked the bucket LOL) . Although I got my videos, it still bugs me - what is wrong with my code? Maybe anyone knows?

import datetime
import time
date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M_%S")
import picamera
camera = picamera.PiCamera()

camera.annotate_background = picamera.Color('black') #this determines the background color
camera.annotate_text = date #places timestamp on first frame of video
camera.start_recording("/media/pi/SD_02/"+ "Plotxx_" + date + ".h264")
start = datetime.datetime.now()
while (datetime.datetime.now() - start).seconds < (3600):#this indicates to continue filming until you reach the maximize time for a given recording segment
    camera.annotate_text = datetime.datetime.now().strftime('%d-%m-%Y %H:%M:%S')
    camera.wait_recording(0.2)
camera.wait_recording(3600)
camera.stop_recording() 



RE: Video recording with Raspberry Pi - What´s wrong with my python code? - deanhystad - Feb-24-2023

seconds returns the seconds component of a Datetime and will never exceed 59. One second after 9:50:59 is 9:51:00. Use time.time() instead of DateTime.seconds()


RE: Video recording with Raspberry Pi - What´s wrong with my python code? - Montezuma1502 - Feb-24-2023

My video output is as follows: It correctly records and prints the timestamp on the videoframes. After one hour, the timestamp stops showing the accurate time (i.e. freezes at at the timepoint the camera should have stopped recording), but the camera continues to record.

UPDATE and SOLUTION
: I played around with the code a bit and stumbled about a person doing similar recordings as me. After combining the both the comment of deanhystad and the other one I finally found out that the problem is actually this:
while (datetime.datetime.now() - start).seconds < (3600):#this indicates to continue filming until you reach the maximize time for a given recording sgement
    camera.annotate_text = datetime.datetime.now().strftime('%d-%m-%Y %H:%M:%S')
    camera.wait_recording(0.2)
##############camera.wait_recording(3600)############
camera.stop_recording()
When I delete this line, it perfectly runs the exact amount of time it should have. Seems that I told python to run the while-loop for 3600 seconds and then record another 3600 seconds. Sometimes it is as simple as that. I´ll leave my problem description in here in case someone else manages to do this weird mistake... Doh


RE: Video recording with Raspberry Pi - What´s wrong with my python code? - deanhystad - Feb-24-2023

I am off my game today. Didn't think about subtracting datetime objects producing a timedelta object (where seconds is the duration of the timedelta measured in seconds) and then I didn't even notice the extra after the loop. The extra wait would certainly do it.