I have made good use of this forum over the past year as I have been creating a birdbox with camera for my mother. Until now I have been able to find an answer to all of my questions but this time I feel i need to pose a question directly, as I cannot find the correct answer. I am pretty green when it comes to programming so please be nice!!

I have picamera recording into 10 minute long files, each called vid0x.h264 up to a value of 6 (6x 10 minute video = 1 hour). Another script moves the files to a NAS and then I have a cron that kills the python process and restarts at the top of the hour. What I would like is for the script itself to restart at vid01 at he top of the hour.

Camera recording code that I would like to loop:

import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (1280, 1024)
    camera.framerate = 24
    camera.hflip = True
    for filename in camera.record_sequence([
            'vid%02d.h264' % (h + 1)
            for h in range(6)
            ], quality=20, bitrate=750000):
        camera.wait_recording(10 * 60)

Can anyone help me getting this to restart after vid06.h264 stops recording?


  • Why do you want it killed at the top of the hour?
    – Brick
    Commented Dec 20, 2017 at 14:52
  • Just to keep things neat. So I have 6 x10 minute videos, the first one starting at say 09:00. Commented Dec 20, 2017 at 23:21

1 Answer 1


You can do this with code something like this:

import datetime

for filename in camera.record_sequence( # Your code here 
    camera_wait_recording(10 * 60)
    t = datetime.now()
    if (top_of_hour(t)):

Now you'll check the current time at the end of each loop, and if you're at the top of the hour, you break the loop. Since you broke the loop, the script will continue - in this case to the end of the program.

You need to implement the function top_of_hour to meet your needs. You can use the functions in datetime to do it. For example, if the minute value is between 0 and 1, maybe return true.

See also https://stackoverflow.com/questions/415511/how-to-get-current-time-in-python.

  • Thanks so much for your suggestion, I'm just home from work so I will give this a go first thing. Commented Dec 20, 2017 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.