# Get framerate and frame timestamp from live webcam stream?

**URL:** <https://forum.opencv.org/t/get-framerate-and-frame-timestamp-from-live-webcam-stream/14475>\
**Category:** Uncategorized\
**Tags:** videoio\
**Created:** [September 7, 2023, 9:32am UTC](https://forum.opencv.org/t/get-framerate-and-frame-timestamp-from-live-webcam-stream/14475 "2023-09-07T09:32:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fra](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/fra/32/8665_2.png) [@Fra](https://forum.opencv.org/u/Fra)\
**Post date:** [September 7, 2023, 9:32am UTC](https://forum.opencv.org/t/get-framerate-and-frame-timestamp-from-live-webcam-stream/14475/1 "2023-09-07T09:32:03Z")

</div>

Hi all,  
I’m struggling a bit trying to read/set the fps for my webcam and to read the timestamp for specific frames captured from my webcam. Specifically when I try to use `vc.get(cv2.CAP_PROP_POS_MSEC)`, `vc.get(cv2.CAP_PROP_FPS)`, `vc.get(cv2.CAP_PROP_FRAME_COUNT)` they return respectively -1, 0, -1. Clearly there’s something that I’m missing. Can someone help? The code looks like this:

```auto
import os
import time

import cv2
import numpy as np
[...]
# Create a new VideoCapture object
vc = cv2.VideoCapture(0, cv2.CAP_DSHOW)
vc.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
vc.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

# Initialise variables to store current time difference as well as previous time call value
previous = time.time()
delta = 0
n = len(os.listdir("directory"))

# Keep looping
while True:
    timem = vc.get(cv2.CAP_PROP_POS_MSEC)
    fps = vc.get(cv2.CAP_PROP_FPS)
    total_frames = vc.get(cv2.CAP_PROP_FRAME_COUNT)

    print(timem, fps, total_frames)
    # Get the current time, increase delta and update the previous variable
    current = time.time()
    delta += current - previous
    previous = current

    # Check if 3 (or some other value) seconds passed
    if delta > 3:
        # Operations on image
        # Reset the time counter
        delta = 0

        _, img = vc.read()
        [...]
        # press esc to exit
    if cv2.waitKey(20) == 27:
        break

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 7, 2023, 2:43pm UTC](https://forum.opencv.org/t/get-framerate-and-frame-timestamp-from-live-webcam-stream/14475/2 "2023-09-07T14:43:28Z")

</div>

apparently the OpenCV backend for cameras on your operating system does not keep track of frame timestamps.

after a `read()`, just use `time.perf_counter()` or a sibling function. it’ll be close enough, unless you throttle the reading, in which case the frames would be stale.

you could open an issue on the github and request such a feature for DSHOW/MSMF. one would expect such a timestamp to represent the time the frame was taken, not the time it was finally read by the user program.

---

<div class="post-metadata">

**Author:** ![Fra](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/fra/32/8665_2.png) [@Fra](https://forum.opencv.org/u/Fra)\
**Post date:** [September 7, 2023, 3:03pm UTC](https://forum.opencv.org/t/get-framerate-and-frame-timestamp-from-live-webcam-stream/14475/3 "2023-09-07T15:03:40Z")

</div>

Thanks so much. Using `time.perf_counter()` is the best solution I tried so far.
