Python 如何使用cv2获取视频的持续时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49048111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:58:16  来源:igfitidea点击:

How to get the duration of video using cv2

pythonvideocv2

提问by Frankie

I can only get the number of frames CAP_PROP_FRAME_COUNTusing CV2.

我只能CAP_PROP_FRAME_COUNT使用 CV2获取帧数。

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

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

How to do that?

怎么做?

Thank you very much.

非常感谢。

回答by ivan_pozdeev

cv2is not designed to explore video metadata, so VideoCapturedoesn't have API to retrieve it directly.

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

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

回答by Ryan Loggerythm

In OpenCV 3, the solution is:

在 OpenCV 3 中,解决方案是:

import cv2

cap = cv2.VideoCapture("./video.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)      # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frame_count/fps

print('fps = ' + str(fps))
print('number of frames = ' + str(frame_count))
print('duration (S) = ' + str(duration))
minutes = int(duration/60)
seconds = duration%60
print('duration (M:S) = ' + str(minutes) + ':' + str(seconds))

cap.release()

回答by Mitiku

First calculate frame per second like this

首先像这样计算每秒帧数

fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)

Then duration can be calculated as (number of frames) / (frames per second)

然后持续时间可以计算为(帧数)/(每秒帧数)

 duration = float(num_frames) / float(fps) # in seconds

回答by lakjeewa Wijebandara

Capture the video and output the duration is seconds

捕获视频并输出持续时间为秒

vidcapture = cv2.VideoCapture('myvideo.mp4')
fps = vidcapture.get(cv2.CAP_PROP_FPS)
totalNoFrames = vidcapture.get(cv2.CAP_PROP_FRAME_COUNT);
durationInSeconds = float(totalNoFrames) / float(fps)

print("durationInSeconds: ",durationInSeconds,"s")

回答by FantasyJXF

I noticed a weird phenomenon that many video DO NOT HAVEas much frames as the vid.get(cv2.CAP_PROP_FRAME_COUNT)gets.

我注意到一个奇怪的现象,许多视频DO NOT HAVE尽可能多帧作为vid.get(cv2.CAP_PROP_FRAME_COUNT)获取。

I suppose that the video duration should be the divided value of TOTAL FRAMES by FPS, but it always mismatch. The video duration would be longer than we calculated. Considering what FFMPEG does, the original video might has some empty frames.

我想视频时长应该是总帧数除以 FPS 的值,但它总是不匹配。视频时长会比我们计算的要长。考虑到 FFMPEG 的作用,原始视频可能有一些空帧。

Hope this help.

希望这有帮助。