User avatar
Alex_Landherr
Posts: 16
Joined: Mon May 18, 2020 3:14 pm
Location: Sweden
Contact: Twitter

How Do I Record A Video Between Two Given Dates? [SOLVED]

Mon May 25, 2020 6:41 pm

I have an issue with the PiCamera library. Namely, I need to record video between to set dates. The datetime library will be used.

The idea in more detail is to set two different dates formatted as 'yyyy-mm-dd hh:mm:ss' and then start a video recording when the time is greater than the start date and stop when the time is greater than the stop date. I've tried to make a while loop that has these conditions:

Code: Select all

from picamera import PiCamera, Color
from time import sleep
from datetime import datetime
from subprocess import call
import sys

camera = PiCamera()
camera.rotation = 180
camera.resolution = (1920, 1080)

# Default sleep time is 5 seconds, revert to this value when done modifying this example.
# In this case sleep means how long the video will be.
# Let the program sleep for at least 2 seconds before capturing an image,
# because this gives the camera's sensor time to sense the light levels.

recording_start_year = int(input("Set a recording start year value written as e.g. 2020 for a video, type 0 to exit program: "))
if recording_start_year == 0:
    exit()
recording_start_month = int(input("Set recording start month value written as e.g. 5, allowed values are 1 <= month <= 12: "))
recording_start_day = int(input("Set a recording start day value written as e.g. 11, allowed values are the days in the given month & year: "))
recording_start_hour = int(input("Set a recording start hour value written as e.g. 22, allowed values are 0 <= hour < 24: "))
recording_start_min = int(input("Set a recording start minute value written as e.g. 20, allowed values are 0 <= minute < 60: "))
recording_start_sec = int(input("Set a recording start second value written as e.g. 00, allowed values are 0 <= second < 60: "))

recording_stop_year = int(input("Set a recording stop year value written as e.g. 2020 for a video, type 0 to exit program: "))
recording_stop_month = int(input("Set recording stop month value written as e.g. 5, allowed values are 1 <= month <= 12: "))
recording_stop_day = int(input("Set a recording stop day value written as e.g. 11, allowed values are the days in the given month & year: "))
recording_stop_hour = int(input("Set a recording stop hour value written as e.g. 22, allowed values are 0 <= hour < 24: "))
recording_stop_min = int(input("Set a recording stop minute value written as e.g. 20, allowed values are 0 <= minute < 60: "))
recording_stop_sec = 1 + int(input("Set a recording stop second value written as e.g. 00, allowed values are 0 <= second < 60: "))

stop = datetime(recording_stop_year, recording_stop_month, recording_stop_day, hour=recording_stop_hour, minute=recording_stop_min, second=recording_stop_sec)
start = datetime(recording_start_year, recording_start_month, recording_start_day, hour=recording_start_hour, minute=recording_start_min, second=recording_start_sec)
camera.framerate = int(input("Set a frame rate for recording the video: "))
fps = str(camera.framerate)
file_name = str(input("Set a file name for the video: "))
camera.start_preview(alpha=200) # Alpha value adjusts the see-through, ranges from 0 to 255.
camera.annotate_background = Color('black')
camera.annotate_text = datetime.now().strftime('UTC: %Y-%m-%d %H:%M:%S')
camera.start_recording('/media/pi/RPI4B/Videos/Pi_Camera_Videos/Time_Stamp_Videos/' + file_name + '.h264')
#start = datetime.now()
while start < datetime.now() < stop:
    camera.annotate_text = datetime.now().strftime('UTC: %Y-%m-%d %H:%M:%S')
camera.stop_recording()
camera.stop_preview()

# The :fps= tells MP4Box to expect a file with a FPS that the user has defined.
print("Wrapping the video in mp4 container with h264 video codec...")
command = "MP4Box -flat -add /media/pi/RPI4B/Videos/Pi_Camera_Videos/Time_Stamp_Videos/" + file_name + ".h264:fps=" + fps + " /media/pi/RPI4B/Videos/Pi_Camera_Videos/Time_Stamp_Videos/" + file_name + ".mp4"
call([command], shell=True)
print("Video has been wrapped in mp4 container with h264 video codec!")
However this doesn't work and just results in the program skipping directly to the MP4Box section for wrapping the file in an mp4 container. I need to know how to trigger the recording at a time given in advance and stop it at a time given in advance. After trying multiple iterations I can't come to a solution; please help me.
Last edited by Alex_Landherr on Wed May 27, 2020 9:29 pm, edited 1 time in total.
Helpful answers are appreciated. I usually respond to Tweets directed at me. JWST is the Sagrada Família of spacecrafts. I code in Python on Windows 10 and Raspberry Pi OS.

ethanol100
Posts: 637
Joined: Wed Oct 02, 2013 12:28 pm

Re: How Do I Record A Video Between Two Given Dates?

Tue May 26, 2020 8:36 am

Your while loop is never true, if the start time is bigger then your current time. Therefore, the loop is skipped and the recording will directly be stopped. You need to first check if you want to start recording first, then(while recording) you need to check if it is late enough to stop.
While waiting, you usually try to use sleep to not continuously run your code. you can for example calculate the time between the current time and the event you are interested in, and then sleep for that duration.

User avatar
Alex_Landherr
Posts: 16
Joined: Mon May 18, 2020 3:14 pm
Location: Sweden
Contact: Twitter

Re: How Do I Record A Video Between Two Given Dates?

Wed May 27, 2020 12:32 pm

Thanks, will give it a try later tonight (27th May).
Helpful answers are appreciated. I usually respond to Tweets directed at me. JWST is the Sagrada Família of spacecrafts. I code in Python on Windows 10 and Raspberry Pi OS.

User avatar
Alex_Landherr
Posts: 16
Joined: Mon May 18, 2020 3:14 pm
Location: Sweden
Contact: Twitter

Re: How Do I Record A Video Between Two Given Dates?

Wed May 27, 2020 9:29 pm

I now have a piece of code that works, thanks to my father for helping me with the details of the program! Below is the working code with which I recorded a test video of 5 minutes in length:

Code: Select all

from picamera import PiCamera, Color
import time
from datetime import datetime, timedelta
from subprocess import call
import sys

camera = PiCamera()
camera.rotation = 180
camera.resolution = (1920, 1080)

# Default sleep time is 5 seconds, revert to this value when done modifying this example.
# In this case sleep means how long the video will be.
# Let the program sleep for at least 2 seconds before capturing an image,
# because this gives the camera's sensor time to sense the light levels.

recording_start_year = int(input("Set a recording start year value written as e.g. 2020 for a video, type 0 to exit program: "))
if recording_start_year == 0:
    exit()
recording_start_month = int(input("Set recording start month value written as e.g. 5, allowed values are 1 <= month <= 12: "))
recording_start_day = int(input("Set a recording start day value written as e.g. 11, allowed values are the days in the given month & year: "))
recording_start_hour = int(input("Set a recording start hour value written as e.g. 22, allowed values are 0 <= hour < 24: "))
recording_start_min = int(input("Set a recording start minute value written as e.g. 20, allowed values are 0 <= minute < 60: "))
recording_start_sec = int(input("Set a recording start second value written as e.g. 00, allowed values are 0 <= second < 60: "))

recording_stop_year = int(input("Set a recording stop year value written as e.g. 2020 for a video, type 0 to exit program: "))
recording_stop_month = int(input("Set recording stop month value written as e.g. 5, allowed values are 1 <= month <= 12: "))
recording_stop_day = int(input("Set a recording stop day value written as e.g. 11, allowed values are the days in the given month & year: "))
recording_stop_hour = int(input("Set a recording stop hour value written as e.g. 22, allowed values are 0 <= hour < 24: "))
recording_stop_min = int(input("Set a recording stop minute value written as e.g. 20, allowed values are 0 <= minute < 60: "))
recording_stop_sec = 1 + int(input("Set a recording stop second value written as e.g. 00, allowed values are 0 <= second < 60: "))

stop = datetime(recording_stop_year, recording_stop_month, recording_stop_day, hour=recording_stop_hour, minute=recording_stop_min, second=recording_stop_sec)
start = datetime(recording_start_year, recording_start_month, recording_start_day, hour=recording_start_hour, minute=recording_start_min, second=recording_start_sec)
camera.framerate = int(input("Set a frame rate for recording the video: "))
fps = str(camera.framerate)
file_name = str(input("Set a file name for the video: "))
camera.start_preview(alpha=200) # Alpha value adjusts the see-through, ranges from 0 to 255.
camera.annotate_background = Color('black')
camera.annotate_text = datetime.now().strftime('UTC: %Y-%m-%d %H:%M:%S')
wait = max(0, timedelta.total_seconds(start - datetime.now()))
time.sleep(wait)
camera.start_recording('/media/pi/RPI4B/Videos/Pi_Camera_Videos/Time_Stamp_Videos/' + file_name + '.h264')
while (start < datetime.now()) and (datetime.now() < stop):
    camera.annotate_text = datetime.now().strftime('UTC: %Y-%m-%d %H:%M:%S')
camera.stop_recording()
camera.stop_preview()

# The :fps= tells MP4Box to expect a file with a FPS that the user has defined.
print("Wrapping the video in mp4 container with h264 video codec...")
command = "MP4Box -flat -add /media/pi/RPI4B/Videos/Pi_Camera_Videos/Time_Stamp_Videos/" + file_name + ".h264:fps=" + fps + " /media/pi/RPI4B/Videos/Pi_Camera_Videos/Time_Stamp_Videos/" + file_name + ".mp4"
call([command], shell=True)
print("Video has been wrapped in mp4 container with h264 video codec!")

Thanks to ethanol100 for pointing out the error I made, very helpful answer. This should be easily adaptable for recording a time-lapse film.
Helpful answers are appreciated. I usually respond to Tweets directed at me. JWST is the Sagrada Família of spacecrafts. I code in Python on Windows 10 and Raspberry Pi OS.

Return to “Camera board”