Opencv 视频捕捉的永久修复 [英] Permanent fix for Opencv videocapture

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

问题描述

这个问题已在许多网站上多次提出,但都不是最终的解决方案.我正在尝试使用函数运行带有视频的 Opencv:

导入 cv2cap = cv2.VideoCapture('video.mp4')如果(cap.isOpened()==假):打印打开相机时出错"

但每次都失败.我已经尝试了来自各个站点的几乎所有步骤,但无法使其正常工作(包括单独重建 ffmpeg).

任何帮助将不胜感激.

我的平台是 Ubuntu17 和 Python3.

解决方案

这个答案是针对 Linux 和 Python 编写的,但总体思路可以应用于 OpenCV 支持的任何操作系统和语言.

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

OpenCV FFMPEG 支持:

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

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

输出应该是这样的:

<块引用>

FFMPEG:是的

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

FFMPEG 编解码器:

FFMPEG 可能没有用于您的特定文件的编解码器.我们将以

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

ffmpeg -codecs |grep -i avc

在我的机器上,输出是:

<块引用>

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

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

文件路径

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

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"

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).

Any help would be much appreciated.

My platform is Ubuntu17 and Python3.

解决方案

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.

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

OpenCV FFMPEG support:

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

The output should be something like:

FFMPEG: YES

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

FFMPEG Codec:

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.

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

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.

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 part 10 (decoders: h264 h264_crystalhd h264_vdpau ) (encoders: libx264 libx264rgb )

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.

File PATH

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

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

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