Skip to content Skip to sidebar Skip to footer

Editing Frames While Recording A Video With Opencv

Is it possible to get frames while recording a video and writing current time on those images at the same? I've been looking for this but I couldn't find anything. I want to write

Solution 1:

Yes it is possible. The cap.get(0) flag, (where cap is a cv2.VideoCapture object), gives you the timestamp of a frame in milliseconds. You can do it as follows:

import cv2
# If you want to write system time instead of frame timestamp then import datetime# import datetime

filepath = '.../video.mp4'
cap = cv2.VideoCapture(filepath)
# If capturing from webcam then as follows:# cap = cv2.VideoCapture(0)while(True):

    # Capture frame-by-frame
    ret, frame = cap.read()

    if(ret== False):
        break

    current_time = cap.get(0)
    # If you want system time then replace above line with the following:# current_time = datetime.datetime.now()

    cv2.putText(frame,'Current time:'+str(current_time), 
        (10, 100), 
        cv2.FONT_HERSHEY_SIMPLEX, 
        1,
        (255,255,255),
        2)

    # Display the resulting frame
    cv2.namedWindow('Frame with timestamp')
    cv2.imshow('Frame with timestamp',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()


Hope this helps :)

Post a Comment for "Editing Frames While Recording A Video With Opencv"