打开视频流或文件时出错?

时间:2017-10-24 14:32:28

标签: opencv video python-3.5

尝试阅读从http://www.sample-videos.com/

下载的以下视频

http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_5mb.mp4

这是我的代码:

import cv2

cap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')

if(cap.isOpened()== False):
  print("Error opening video stream or file")


count = 0

while (cap.isOpened()):
    # capture frame by frame :
    ret, frame = cap.read()
    if ret==True:
        # Display the resulting frame
        cv2.imshow('Frame', frame)
        cv2.imwrite("frame%d.jpg" % count, frame)
        count +=1
        print(count)

但是我在cap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')处获得错误打开视频流或文件ret equals False always

我的OpenCV版本是3.1.0

2 个答案:

答案 0 :(得分:0)

您的计算机可能存在以下问题:

  1. 配置视频路径
  2. 检查访问文件的权限
  3. 安装其他编解码器

答案 1 :(得分:0)

您可能已经安装了opencv,但是在使用open cv读取.mp4视频文件时,需要安装一些先决条件。

您可以通过简单地读取.avi格式文件和.mp4文件来验证

[它可以读取.avi文件,但不能读取.mp4文件]

要读取mp4文件。

  1. 安装使用H.264编解码器编译的ffmpeg软件包:

    H.264/MPEG-4 Part 10 or AVC (Advanced Video Coding) is a standard for video compression, and is currently one of the most commonly used formats for the recording, compression, and distribution of high definition video.
    
    Ref : https://www.debiantutorials.com/how-to-install-ffmpeg-with-h-264mpeg-4-avc/
    

很少有建议确保所有先决条件都可用

    1. check ffmpeg package compiled with H.264 is already installed in the machine using the command below. 
    ffmpeg -version 
    2. Installation of open-cv in anaconda will reduce the stress to install ffmpeg package compiled with H.264 
    3. Make sure that the user created in the machine has got enough privilege to read and write in specific application related directories
        a. Check the read and write permission using the command below
            ls -ld <folder-path>
               or
            namei -mo <folder-path>
        b. Alter the access writes based on the user privilege required (sudo access needed else we need to engage admin to alter the permission)
            eg : sudo chmod -R 740 <folder-path>** [ Recursive rwx for user ,r for group ]