Home > Mobile >  OpenCV How to add start time, end time on video recorded from webcam
OpenCV How to add start time, end time on video recorded from webcam

Time:04-20

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.

Edited content below, in defense of question asked has focus and must not be closed.

I'm able to add only a continuous running timestamp on the video. I require static timestamps on the video, i.e., As soon as the video begins, a start timestamp to be placed on the beginning video-frame and when the video ends, another time-stamp is added that reflects the time the video ended.

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:

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()

CodePudding user response:

My apologized, I misread your putText. I am using raspberry pi 4, Bullseye. Using same code. Hold 'q' keydown for 5 second and release it. I added some snippet if-else in block condition.

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))))
#cv2.namedWindow('frame',cv2.WINDOW_AUTOSIZE)
# 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:
            cv2.imshow('frame', frame)
            cv2.waitKey(1)
            #break
    else:
       #break
       cv2.imshow('frame', frame)
       cv2.waitKey(1)
       #break
# release the vid object
cap.release()
# close all the opened windows.
cv2.destroyAllWindows()

Edit: I found better solution. Do not used same duplicated and also for break. I am attempting reducing coding. I will attempting to using threading. that will be helpfully.

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':
            #video_writer.write(frame)
            cv2.imshow('frame', frame)
            cv2.waitKey(5)
       
        if key == 27:
            break
 
# release the vid object
cap.release()
# close all the opened windows.
cv2.destroyAllWindows()
  • Related