OpenCV 如何在网络摄像头录制的视频上添加开始时间和结束时间

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

我在 Python 中使用 OpenCV 从网络摄像头录制视频。我正在尝试为视频录制添加时间戳。

问题: 我想要实现的是当视频录制开始时,当前系统时间戳应该放在视频上。当录制结束时,应该在视频上添加另一个时间戳。这样,视频录制将在录制的视频上有两个时间戳(开始时间和结束时间)。

编辑了以下内容,以防所问的问题具有焦点,不得关闭。

我只能在视频上添加一个连续的 运行 时间戳。我需要在视频上添加 static 时间戳,即,一旦视频开始,就在 beginning video-frame 上放置一个开始时间戳,当视频结束时,添加另一个反映时间的时间戳视频结束。

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

假设我们不知道最后一帧是什么时候,我们可能会一直将前一帧写入文件,当ret == False(最后一帧)时,将文本放在最后一帧并写入最后一帧。

识别第一帧很简单。
key == 'q' or key == 27时识别最后一帧也很简单。

唯一的问题是在 ret == False 时识别最后一帧。
为了处理这种情况,我们可以使用一帧的缓冲区
继续写上一帧...

ret == False时,我们知道最新的一帧是最后一帧,所以我们将文本添加到最后一帧并写入视频文件。

注:
我们需要在最后添加video_writer.release()


代码示例:

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