项目位置:OpenCV-Sample
代码位置:36-GetFrameTimestamp.py
我们在对视频文件做分析的时候,往往对某一帧数据进行标注,所以当Opencv读取帧数据的时候,要带有这帧数据的时间戳,OpenCV获取的时间戳与ffmpeg里面的时间戳相比还是简单很多,让用户知道这帧数据处在视频文件的那个位置。
代码如下:
import cv2
cameraCapture = cv2.VideoCapture('./res/2_003_013.mp4')
success, frame = cameraCapture.read()
while success:
if cv2.waitKey(1) == 27:
break
cv2.imshow('Test camera', frame)
success, frame = cameraCapture.read()
milliseconds = cameraCapture.get(cv2.CAP_PROP_POS_MSEC)
seconds = milliseconds//1000
milliseconds = milliseconds%1000
minutes = 0
hours = 0
if seconds >= 60:
minutes = seconds//60
seconds = seconds % 60
if minutes >= 60:
hours = minutes//60
minutes = minutes % 60
print(int(hours), int(minutes), int(seconds), int(milliseconds))
cv2.destroyAllWindows()
cameraCapture.release()