永久修复Opencv videocapture [英] Permanent fix for Opencv videocapture

查看:425
本文介绍了永久修复Opencv videocapture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经在许多网站上提出过很多次,但不是确定的解决方案.我正在尝试使用功能使用视频运行Opencv:

This question has been posed numerous times on many websites, but not definitive solution. I am trying to run Opencv with a video using function:

import cv2
cap = cv2.VideoCapture('video.mp4')
if(cap.isOpened()==False):
print "Error opening camera"

但是每次都会失败.我已经尝试了从各个站点进行的几乎所有步骤,但是无法正常工作(包括分别重建ffmpeg).

But it fails every time. I have tried almost all steps from various sites, but couldn't get it to work (including rebuilding ffmpeg separately).

任何帮助将不胜感激.

我的平台是Ubuntu17和Python3.

My platform is Ubuntu17 and Python3.

推荐答案

此答案是在考虑Linux和Python的情况下编写的,但是总体思路可以应用于OpenCV支持的任何OS和语言.

This answer is written with Linux and Python in mind, but the general idea can be applied to any OS and language supported by OpenCV.

VideoCapture类无法打开视频文件可能有很多原因,但是以下三种情况适用于大多数情况.

The VideoCapture class not opening the video file can have many causes, but the following three applies to most cases.

默认情况下,OpenCV使用ffmpeg读取视频文件. OpenCV可能不是使用FFMPEG支持构建的.要了解OpenCV是否构建有FFMPEG支持,请在终端中输入:

By default OpenCV uses ffmpeg to read video files. OpenCV may not have been built with FFMPEG support. To find out if OpenCV was built with FFMPEG support, in terminal enter:

python -c "import cv2; print(cv2.getBuildInformation())" | grep -i ffmpeg

输出应类似于:

FFMPEG:是

FFMPEG: YES

如果输出为No,则请遵循在线指南,从具有ffmpeg支持的源代码构建OpenCV.

If the output is No then follow an online guide to build OpenCV from source with ffmpeg support.

FFMPEG可能没有针对您的特定文件的编解码器.我们将以此视频为例,说明FFMPEG是否已解码该文件的功能.

It's possible that FFMPEG does not have codec for your specific file. We are going to use this video as an example, to show if FFMPEG has decoding capability for this file.

首先,我们需要找到此视频文件中使用的编码格式.我们将使用mediainfo程序.在终端中,输入:

First, we need to find the encoding format used in this video file. We will be using the mediainfo program. In terminal, enter:

mediainfo video_file.mp4

在输出中的 video 标题下,找到 format .在这种情况下,使用的视频编码是 AVC ,这是H264的另一个名称.

In the output, under the video heading, look for format. In this case the video encoding used is AVC, which is another name for H264.

现在,我们尝试查找FFMPEG是否支持编解码器来解码 AVC 编码文件.在终端中:

Now, we try to find if FFMPEG supports codec for decoding AVC encoded files. In terminal:

ffmpeg -codecs | grep -i avc

在我的机器上,输出为:

On my machine, the output is:

DEV.LS h264 H.264/AVC/MPEG-4 AVC/MPEG-4第10部分(解码器:h264 h264_crystalhd h264_vdpau)(编码器:libx264 libx264rgb)

DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_crystalhd h264_vdpau ) (encoders: libx264 libx264rgb )

我们对 DEV 感兴趣,它代表解码,编码和视频.这意味着 AVC 是视频编码格式,而FFMPEG同时支持该格式的编码和解码功能.

We are interested in DEV, which stands for Decoding, Encoding and Video. This means that AVC is a video encoding format and FFMPEG supports both encoding and decoding capabilities for this format.

最后,请检查文件路径是否正确或文件是否存在.

Lastly, check if the file path is correct or even if the file exists.

这篇关于永久修复Opencv videocapture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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