RTP Timestamps Are Not Monotonically increasing

0

I am finding it a bit difficult trying to understand whether or not the hack around with FFmpeg and OpenCV really provided a RTP timestamp. My last post helped a little bit but got me stuck in trying to validate the timestamps obtained through this work around by modifying ffmpeg and opencv.

FFmpeg version : 4.1.0 OpenCV version : 3.4.1

import cv2
import time
from datetime import datetime, date

uri = 'rtsp://admin:password@192.168.1.66:554/Streaming/Channels/101'
cap = cv2.VideoCapture(uri)
'''One is the offset between the two epochs. Unix uses an epoch located at 1/1/1970-00:00h (UTC) and NTP uses 1/1/1900-00:00h.
This leads to an offset equivalent to 70 years in seconds (there are 17 leap years between the two dates so the offset is'''
time_offset = 2208988800 # (70*365 + 17)*86400 = 2208988800 (in seconds)
# offset = 3775484294
days = 43697
pdat = "1900-01-01 00:00:00:00"
mdat = "2019-08-23 22:02:44:00" # str(datetime.now()) + str(datetime.now().time())
pdate = datetime.strptime(pdat, "%Y-%m-%d %H:%M:%S:%f").date()
mdate = datetime.strptime(mdat, "%Y-%m-%d %H:%M:%S:%f").date()
delta = (mdate - pdate).days
offset = delta * 86400
def time_delta(s):
    return (s - time_offset)

while True:
    frame_exists, curr_frame = cap.read()
    if frame_exists:
        seconds = cap.getRTPTimeStampSeconds()
        fraction = cap.getRTPTimeStampFraction()
        timestamp = cap.getRTPTimeStampTs()
        unix_offset = seconds - time_offset
        msec = int((int(fraction) / 0xFFFFFFFF) * 1000.0)
        ts = float(str(unix_offset) + "." + str(msec))
        # print("Timestamp per Frame:%i" % timestamp)
        print((datetime.fromtimestamp(float(ts) + offset)))
cap.release()

My Output:

On August 23, 2019 at 22:02

...
2019-08-23 13:59:52.781000
2019-08-23 13:59:52.726000
2019-08-23 13:59:52.671000
2019-08-23 13:59:52.616000
2019-08-23 13:59:52.561000
2019-08-23 13:59:52.506000
2019-08-23 13:59:52.451000
2019-08-23 13:59:52.396000
2019-08-23 13:59:52.342000
2019-08-23 13:59:52.287000
2019-08-23 13:59:52.232000
2019-08-23 13:59:52.177000
2019-08-23 13:59:52.122000
2019-08-23 13:59:52.067000
2019-08-23 13:59:52.012000
2019-08-23 13:59:53.570000
2019-08-23 13:59:53.020000
2019-08-23 13:59:53.847000
2019-08-23 13:59:53.792000

I've noticed how the time increments weirdly (that's not suppose to happen in the real, current time), such as the last two lines and a few others in between in the output. A bit flabbergasted as to what went wrong. Also trying this out on multiple IP cameras, with each showing a different timestamp probably related to when they were turned on.

python
opencv
ffmpeg
timestamp
ntp
asked on Stack Overflow Aug 23, 2019 by m00ncake • edited Aug 25, 2019 by m00ncake

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0