Getting timestamp of each frame in a video Getting timestamp of each frame in a video python python

Getting timestamp of each frame in a video


You want cv2.CAP_PROP_POS_MSEC. See all the different capture properties here.

Edit: Actually, as Dan MaĊĦek pointed out to me, when you grab that property, it looks like OpenCV is exactly doing that calculation (at least assuming you're using FFMPEG):

case CV_FFMPEG_CAP_PROP_POS_MSEC:    return 1000.0*(double)frame_number/get_fps();

So it seems you're always going to rely on a constant frame rate assumption. However, even assuming a constant frame rate, it's important that you multiply by the frame number and not just keep adding 1000/fps. Errors will build up when you're repeatedly adding floats which, over a long video, can make a big difference. For example:

import cv2cap = cv2.VideoCapture('vancouver2.mp4')fps = cap.get(cv2.CAP_PROP_FPS)timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]calc_timestamps = [0.0]while(cap.isOpened()):    frame_exists, curr_frame = cap.read()    if frame_exists:        timestamps.append(cap.get(cv2.CAP_PROP_POS_MSEC))        calc_timestamps.append(calc_timestamps[-1] + 1000/fps)    else:        breakcap.release()for i, (ts, cts) in enumerate(zip(timestamps, calc_timestamps)):    print('Frame %d difference:'%i, abs(ts - cts))

Frame 0 difference: 0.0
Frame 1 difference: 0.0
Frame 2 difference: 0.0
Frame 3 difference: 1.4210854715202004e-14
Frame 4 difference: 0.011111111111091532
Frame 5 difference: 0.011111111111091532
Frame 6 difference: 0.011111111111091532
Frame 7 difference: 0.011111111111119953
Frame 8 difference: 0.022222222222183063
Frame 9 difference: 0.022222222222183063
...
Frame 294 difference: 0.8111111111411446

This is of course in milliseconds, so maybe it doesn't seem that big. But here I'm almost 1ms off in the calculation, and this is just for an 11-second video. And anyways, using this property is just easier.


I have use moviepy to get time in seconds of individual frame

pip install moviepy
import sysimport numpy as npimport cv2import moviepy.editor as mpyfrom matplotlib import pyplot as pltvid = mpy.VideoFileClip('input_video\\v3.mp4')for i, (tstamp, frame) in enumerate(vid.iter_frames(with_times=True)):    print(tstamp%60)    plt.imshow(frame)    plt.show()


This is a simplified version that just reads in a video and prints out a frame number with its timestamp.

import cv2cap = cv2.VideoCapture('path_to_video/video_filename.avi')frame_no = 0while(cap.isOpened()):    frame_exists, curr_frame = cap.read()    if frame_exists:        print("for frame : " + str(frame_no) + "   timestamp is: ", str(cap.get(cv2.CAP_PROP_POS_MSEC)))    else:        break    frame_no += 1cap.release()

This gives an output that looks like this:

for frame : 0   timestamp is:  0.0for frame : 1   timestamp is:  40.0for frame : 2   timestamp is:  80.0for frame : 3   timestamp is:  120.0for frame : 4   timestamp is:  160.0for frame : 5   timestamp is:  200.0for frame : 6   timestamp is:  240.0for frame : 7   timestamp is:  280.0for frame : 8   timestamp is:  320.0for frame : 9   timestamp is:  360.0for frame : 10   timestamp is:  400.0for frame : 11   timestamp is:  440.0for frame : 12   timestamp is:  480.0...