pen*_*uin 5 c++ python opencv timestamp ffmpeg
我正在尝试一个小实验,以便使用 Python 中 Opencv 源代码中的 VideoCapture 类获取 RTP 数据包的时间戳,还必须修改 FFmpeg 以适应 Opencv 中的更改。
因为我读到了有关RTP 数据包格式的信息,所以我想摆弄一下,看看是否能找到一种方法来获取 NTP 时间戳。在尝试获取 RTP 时间戳时无法找到任何可靠的帮助。所以尝试了这个小技巧。
修改后的代码归功于 github 上的 ryantheseer。
FFmpeg 版本:3.2.3 Opencv 版本:3.2.0
Opencv源代码中:
模块/videoio/include/opencv2/videoio.hpp:
为 RTP 时间戳添加了两个 getter:
.....
/** @brief Gets the upper bytes of the RTP time stamp in NTP format (seconds).
*/
CV_WRAP virtual int64 getRTPTimeStampSeconds() const;
/** @brief Gets the lower bytes of the RTP time stamp in NTP format (fraction of seconds).
*/
CV_WRAP virtual int64 getRTPTimeStampFraction() const;
.....
Run Code Online (Sandbox Code Playgroud)
模块/videoio/src/cap.cpp:
添加了导入并添加了时间戳 getter 的实现:
....
#include <cstdint>
....
....
static inline uint64_t icvGetRTPTimeStamp(const CvCapture* capture)
{
return capture ? capture->getRTPTimeStamp() : 0;
}
...
Run Code Online (Sandbox Code Playgroud)
在 VideoCapture 类中添加了 C++ 时间戳获取器:
....
/**@brief Gets the upper bytes of the RTP time stamp in NTP format (seconds).
*/
int64 VideoCapture::getRTPTimeStampSeconds() const
{
int64 seconds = 0;
uint64_t timestamp = 0;
//Get the time stamp from the capture object
if (!icap.empty())
timestamp = icap->getRTPTimeStamp();
else
timestamp = icvGetRTPTimeStamp(cap);
//Take the top 32 bytes of the time stamp
seconds = (int64)((timestamp & 0xFFFFFFFF00000000) / 0x100000000);
return seconds;
}
/**@brief Gets the lower bytes of the RTP time stamp in NTP format (seconds).
*/
int64 VideoCapture::getRTPTimeStampFraction() const
{
int64 fraction = 0;
uint64_t timestamp = 0;
//Get the time stamp from the capture object
if (!icap.empty())
timestamp = icap->getRTPTimeStamp();
else
timestamp = icvGetRTPTimeStamp(cap);
//Take the bottom 32 bytes of the time stamp
fraction = (int64)((timestamp & 0xFFFFFFFF));
return fraction;
}
...
Run Code Online (Sandbox Code Playgroud)
模块/videoio/src/cap_ffmpeg.cpp:
添加了导入:
...
#include <cstdint>
...
Run Code Online (Sandbox Code Playgroud)
添加了方法参考定义:
...
static CvGetRTPTimeStamp_Plugin icvGetRTPTimeStamp_FFMPEG_p = 0;
...
Run Code Online (Sandbox Code Playgroud)
将该方法添加到模块初始化方法中:
...
if( icvFFOpenCV )
...
...
icvGetRTPTimeStamp_FFMPEG_p =
(CvGetRTPTimeStamp_Plugin)GetProcAddress(icvFFOpenCV, "cvGetRTPTimeStamp_FFMPEG");
...
...
icvWriteFrame_FFMPEG_p != 0 &&
icvGetRTPTimeStamp_FFMPEG_p !=0)
...
icvGetRTPTimeStamp_FFMPEG_p = (CvGetRTPTimeStamp_Plugin)cvGetRTPTimeStamp_FFMPEG;
Run Code Online (Sandbox Code Playgroud)
实现getter接口:
...
virtual uint64_t getRTPTimeStamp() const
{
return ffmpegCapture ? icvGetRTPTimeStamp_FFMPEG_p(ffmpegCapture) : 0;
}
...
Run Code Online (Sandbox Code Playgroud)
FFmpeg的源代码中:
libavcodec/avcodec.h:
将 NTP 时间戳定义添加到 AVPacket 结构中:
typedef struct AVPacket {
...
...
uint64_t rtp_ntp_time_stamp;
}
Run Code Online (Sandbox Code Playgroud)
libavformat/rtpdec.c:
将 ntp 时间戳存储在 Finalize_packet 方法中的结构体中:
static void finalize_packet(RTPDemuxContext *s, AVPacket *pkt, uint32_t timestamp)
{
uint64_t offsetTime = 0;
uint64_t rtp_ntp_time_stamp = timestamp;
...
...
/*RM: Sets the RTP time stamp in the AVPacket */
if (!s->last_rtcp_ntp_time || !s->last_rtcp_timestamp)
offsetTime = 0;
else
offsetTime = s->last_rtcp_ntp_time - ((uint64_t)(s->last_rtcp_timestamp) * 65536);
rtp_ntp_time_stamp = ((uint64_t)(timestamp) * 65536) + offsetTime;
pkt->rtp_ntp_time_stamp = rtp_ntp_time_stamp;
Run Code Online (Sandbox Code Playgroud)
libavformat/utils.c:
在 read_frame_internal 方法中将数据包中的 ntp 时间戳复制到帧中:
static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
{
...
uint64_t rtp_ntp_time_stamp = 0;
...
while (!got_packet && !s->internal->parse_queue) {
...
//COPY OVER the RTP time stamp TODO: just create a local copy
rtp_ntp_time_stamp = cur_pkt.rtp_ntp_time_stamp;
...
#if FF_API_LAVF_AVCTX
update_stream_avctx(s);
#endif
if (s->debug & FF_FDEBUG_TS)
av_log(s, AV_LOG_DEBUG,
"read_frame_internal stream=%d, pts=%s, dts=%s, "
"size=%d, duration=%"PRId64", flags=%d\n",
pkt->stream_index,
av_ts2str(pkt->pts),
av_ts2str(pkt->dts),
pkt->size, pkt->duration, pkt->flags);
pkt->rtp_ntp_time_stamp = rtp_ntp_time_stamp; #Just added this line in the if statement.
return ret;
Run Code Online (Sandbox Code Playgroud)
我的 python 代码利用这些更改:
import cv2
uri = 'rtsp://admin:password@192.168.1.67:554'
cap = cv2.VideoCapture(uri)
while True:
frame_exists, curr_frame = cap.read()
# if frame_exists:
k = cap.getRTPTimeStampSeconds()
l = cap.getRTPTimeStampFraction()
time_shift = 0x100000000
#because in the getRTPTimeStampSeconds()
#function, seconds was multiplied by 0x10000000
seconds = time_shift * k
m = (time_shift * k) + l
print("Imagetimestamp: %i" % m)
cap.release()
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
Imagetimestamp: 0
Imagetimestamp: 212041451700224
Imagetimestamp: 212041687629824
Imagetimestamp: 212041923559424
Imagetimestamp: 212042159489024
Imagetimestamp: 212042395418624
Imagetimestamp: 212042631348224
...
Run Code Online (Sandbox Code Playgroud)
最让我惊讶的是,当我关闭网络摄像机并重新打开电源时,时间戳会从 0 开始,然后快速递增。我读到的 NTP 时间格式是相对于 1900 年 1 月 1 日 00:00 的。即使当我尝试计算偏移量并计算从现在到 1900 年 1 月 1 日之间的时间时,我仍然得到了该日期的一个疯狂的高数字。
不知道是不是我计算错了。我有一种感觉,或者我得到的不是时间戳。
正如我所见,您收到一个 uint64 类型的时间戳,其中包含高位和低位的值 uint32。我在您使用的代码的一部分中看到:
seconds = (int64)((timestamp & 0xFFFFFFFF00000000) / 0x100000000);
Run Code Online (Sandbox Code Playgroud)
这基本上删除了较低位并将高位移至较低位。然后将其转换为 int64。在这里我只考虑它应该首先是无符号的,因为它在任何情况下都不应该是负数(自纪元以来的秒数总是正数)并且它应该是 uint32,因为它保证它不会更大(你只需要 32位)。此外,这可以通过如下的位移来实现(可能更快):
auto seconds = static_cast<uint32>(timestamp >> 32);
Run Code Online (Sandbox Code Playgroud)
我发现的另一个错误是在这部分:
time_shift = 0x100000000
seconds = time_shift * k
m = (time_shift * k) + l
Run Code Online (Sandbox Code Playgroud)
在这里,您基本上是在重建 64 位时间戳,而不是创建可在其他上下文中使用的时间戳。这意味着,您将在几秒钟内将较低位移至较高位,并将小数部分添加为较低位...这将以一个非常大的数字结束,该数字可能并不总是有用。您仍然可以使用它进行比较,但是不需要在 C++ 部分中完成的所有转换。我认为可以与 python datetime 一起使用的更正常的时间戳如下所示:
timestamp = float(str(k) + "." + str(l)) # don't know if there is a better way
date = datetime.fromtimestamp(timestamp)
Run Code Online (Sandbox Code Playgroud)
如果你不关心小数部分,你可以直接使用秒。
另一件需要考虑的事情是,RTP 协议的时间戳取决于摄像机/服务器...它们可能使用时钟时间戳或只是一些其他时钟,例如系统启动的流式传输开始。所以它可能来自某个时代,也可能不是。
| 归档时间: |
|
| 查看次数: |
5107 次 |
| 最近记录: |