I'm using OpenCV
in Python to record video from webcam. I'm trying to add timestamp to video-recording.
Problem: What I want to achieve is when the video recording begins, current system timestamp should be placed on the video. And when the recording ends, another timestamp should be added on the video. This way, the video recording will have two timestamps (a start time, and a end time) on the recorded video.
I'm able to add only a continuous running timestamp on the video. Here is my code so far,
import cv2
import time
import datetime
fps = 24
width = 864
height = 640
# Define the codec and create VideoWriter object
font = cv2.FONT_HERSHEY_SIMPLEX
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video_codec=cv2.VideoWriter_fourcc('D','I','V','X')
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
ret = cap.set(3, 864)
timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]
video_file = "outputFile.avi"
# Create a video write before entering the loop
video_writer = cv2.VideoWriter(
video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4))))
# Process until end.
while (cap.isOpened()):
ret, frame = cap.read()
if ret:
# describe the type of font you want to display
font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
# Get date and time and save it inside a variable
dt = str(datetime.datetime.now())
# put the dt variable over the video frame
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
# write video to disk
video_writer.write(frame)
# show the video
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
# define the key to close the window
if key == 'q' or key == 27:
break
else:
break
# release the vid object
cap.release()
# close all the opened windows.
cv2.destroyAllWindows()
CodePudding user response:
You do this simply like this.
#Set this for 10 seconds
END_TIME = 10
#Set this for current time
START_TIME = time.time()
while(int(time.time() - START_TIME) < END_TIME):
ret, frame = cap.read()
CodePudding user response:
Assume we don't know when comes the last frame, we may keep writing the previous frame to the file, and when ret == False
(last frame), put the text on the last frame and write the last frame.
Identifying the first frame is simple.
Identifying the last frame when key == 'q' or key == 27
is also simple.
The only issue is identifying the last frame when ret == False
.
For handling that case, we may use a buffer of one frame.
Keep writing the previous frame...
When ret == False
, we know that the latest frame is the last frame, so we add the text to the last frame and write it to the video file.
Note:
We need to add video_writer.release()
at the end.
Code sample:
import cv2
import time
import datetime
fps = 24
width = 864
height = 640
# Define the codec and create VideoWriter object
font = cv2.FONT_HERSHEY_SIMPLEX
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video_codec=cv2.VideoWriter_fourcc('D','I','V','X')
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
ret = cap.set(3, 864)
#cap = cv2.VideoCapture('input.mp4') # Read from file instead of Camera - simpler to debug.
timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]
video_file = "outputFile.avi"
# Create a video write before entering the loop
video_writer = cv2.VideoWriter(
video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4))))
dt = None
frame = None
prev_frame = None
# Process until end.
while (cap.isOpened()):
if frame is not None:
prev_frame = frame.copy()
ret, frame = cap.read()
if ret:
# describe the type of font you want to display
font = cv2.FONT_HERSHEY_SCRIPT_COMPLEX
# Get date and time and save it inside a variable
prev_dt = dt # Keep the previous dt
dt = str(datetime.datetime.now())
# put the dt variable over the video frame
if prev_frame is None:
# Firt frame:
# If prev_frame is None, this is the first frame.
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
else:
# Write the previous frame instead of the new frame.
video_writer.write(prev_frame)
# write video to disk
#video_writer.write(frame)
# show the video
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
# define the key to close the window
if key == 'q' or key == 27:
# Last frame:
# Add the time, and write the last last frame.
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
video_writer.write(frame)
break
else:
# Last frame:
# Add the time, and write the last last frame.
frame = cv2.putText(frame, dt,
(10, 100),
font, 1,
(210, 155, 155),
4, cv2.LINE_8)
video_writer.write(frame)
break
# release the vid object
cap.release()
video_writer.release() # Important
# close all the opened windows.
cv2.destroyAllWindows()