使用OpenCV录制视频时编辑帧 [英] Editing Frames While Recording a Video with OpenCV

查看:119
本文介绍了使用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 time on each frame and/or save those frames with that timestamp. I couldn't get the time information from a video for each frame, so I came up with this solution. I am open for any ideas. Thanks in advance.

推荐答案

是的。 cap.get(0)标志(其中cap是 cv2.VideoCapture 对象),为您提供时间戳记帧的毫秒数。您可以执行以下操作:

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


希望这会有所帮助:)

这篇关于使用OpenCV录制视频时编辑帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆