Python Forum
Video recording with Raspberry Pi - What´s wrong with my python code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Video recording with Raspberry Pi - What´s wrong with my python code?
#1
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() 
Reply
#2
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()
Reply
#3
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
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 491 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Something wrong with my code FabianPruitt 5 869 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,586 Mar-27-2023, 07:38 AM
Last Post: buran
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,809 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,576 Nov-05-2022, 11:33 AM
Last Post: Mavoz
Smile How we can prevent screen recording murad_ali 3 1,845 Jul-29-2022, 10:29 AM
Last Post: DeaD_EyE
  How to output Python in Spout or any other video sharing network? buzzdarkyear 4 2,170 Jan-11-2022, 11:37 AM
Last Post: buzzdarkyear
  Wrong code in Python exercise MaartenRo 2 1,537 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,636 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  How to decrease latency while recording streaming video. unicorn2019 0 1,269 Nov-15-2021, 02:12 PM
Last Post: unicorn2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020