如何使用cv2获取视频的时长 [英] How to get the duration of video using cv2

查看:1187
本文介绍了如何使用cv2获取视频的时长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能使用CV2获得 CAP_PROP_FRAME_COUNT 的帧数。

I can only get the number of frames CAP_PROP_FRAME_COUNT using CV2.

但是,找不到参数以使用cv2获取视频的时长。

However, I cannot find the parameter to get the duration of the video using cv2.

该怎么做?

非常感谢。

推荐答案

cv2 并非旨在探索视频元数据,因此 VideoCapture 没有API可以直接检索它。

cv2 is not designed to explore video metadata, so VideoCapture doesn't have API to retrieve it directly.

您可以改为测量流的长度:搜索到末尾,然后获取时间戳:

You can instead "measure" the length of the stream: seek to the end, then get the timestamp:

>>> v=cv2.VideoCapture('sample.avi')
>>> v.set(cv2.CAP_PROP_POS_AVI_RATIO,1)
True
>>> v.get(cv2.CAP_PROP_POS_MSEC)
213400.0

检查表明这将设置因此,时间戳确实是流的确切总长度。

Checking shows that this sets the point after the last frame (not before it), so the timestamp is indeed the exact total length of the stream:

>>> v.get(cv2.CAP_PROP_POS_FRAMES)
5335.0
>>>> v.get(cv2.CAP_PROP_FRAME_COUNT)
5335.0

>>> v.set(cv2.CAP_PROP_POS_AVI_RATIO,0)
>>> v.get(cv2.CAP_PROP_POS_FRAMES)
0.0        # the 1st frame is frame 0, not 1, so "5335" means after the last frame

这篇关于如何使用cv2获取视频的时长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆