使用OpenCV Python阅读视频

时间:2017-04-20 07:57:27

标签: python opencv video

我正在尝试从文件中读取视频 看起来视频已打开,但我无法阅读框架:

Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.0.0'
>>>
>>> video = cv2.VideoCapture('action-recognition-train/0000.avi')
>>> video.isOpened()
True 
>>> nFrames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
>>> print "frame number: %s" %nFrames
frame number: 166
>>> fps = video.get(cv2.CAP_PROP_FPS)
>>> print "FPS value: %s" %fps
FPS value: 25.0
>>> height = video.get(cv2.CAP_PROP_FRAME_HEIGHT)
>>> width = video.get(cv2.CAP_PROP_FRAME_WIDTH)
>>> print "video size:", height, 'x', width
video size: 240.0 x 320.0
>>> video.read()
(False, None)

使用cv2.VideoCapture(0)从网络摄像头读取是正确的

1 个答案:

答案 0 :(得分:0)

解决这个问题的一种方法是提取帧并将其存储在目录中。 可以使用ffmpeg来做到这一点。安装ffmpeg后,从python执行以下命令:

os.system(" ffmpeg -i path_to_video -r 1 path_to store / video_name%4d.jpg")

这里,-r是从视频中提取帧的速率。 -r 1表示以1fps提取帧。

存储视频帧后,我们可以列出它并根据需要使用各个帧。