【问题标题】:Opening video with openCV使用 openCV 打开视频
【发布时间】:2014-10-02 03:46:37
【问题描述】:

我正在尝试按照此处OpenCV Python TutorialOpenCV Python Tutorial 中看到的教程在 OpenCV 中打开视频文件

但是,在运行代码块时没有任何反应。我的代码如下:

import numpy as np
import cv2

cap = cv2.VideoCapture('768x576.avi')   

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

我的系统和系统路径中安装了 ffmpeg。此外,我的机器上也安装了 gstreamer。此文件可以在 windows 媒体播放器中打开,在 ffmpeg 中进行操作,也可以在 VLC 中打开。

我在这里不知所措,因为这应该是一个简单的教程。


编辑 1:我安装了许多不同的编解码器,包括 K-Lite Codec Pack 和 XVid。如果我用 ffmpeg -i 打开文件,这就是我得到的

C:\sandbox>ffmpeg -i 768x576.avi
ffmpeg version N-64539-gc8b2cf4 Copyright (c) 2000-2014 the FFmpeg developers
  built on Jul  8 2014 22:10:23 with gcc 4.8.3 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetyp
e --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-
libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libope
njpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsox
r --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab -
-enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx
--enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-
libxavs --enable-libxvid --enable-decklink --enable-zlib
  libavutil      52. 91.100 / 52. 91.100
  libavcodec     55. 68.102 / 55. 68.102
  libavformat    55. 45.100 / 55. 45.100
  libavdevice    55. 13.102 / 55. 13.102
  libavfilter     4. 10.100 /  4. 10.100
  libswscale      2.  6.100 /  2.  6.100
  libswresample   0. 19.100 /  0. 19.100
  libpostproc    52.  3.100 / 52.  3.100
Input #0, avi, from '768x576.avi':
  Metadata:
    encoder         : MEncoder 2:1.0~rc2-0ubuntu19
  Duration: 00:01:19.50, start: 0.000000, bitrate: 818 kb/s
    Stream #0:0: Video: msmpeg4v3 (div3 / 0x33766964), yuv420p, 768x576, 816 kb/
s, 10 fps, 10 tbr, 10 tbn, 10 tbc
At least one output file must be specified

编辑 2:当我尝试逐行执行此操作时,会发生以下情况

C:\sandbox>python
Python 2.7.7 |Anaconda 2.0.1 (64-bit)| (default, Jun 11 2014, 10:40:02) [MSC v.1
500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>
>>> import numpy as np
>>> import cv2
>>> print cv2.__version__
2.4.9
>>>
>>> cap = cv2.VideoCapture('768x576.avi')
>>> print cap
<VideoCapture 0000000002DC3810>
>>> print cap.get(5)
0.0
>>>

编辑 3:答案不是 100% 的答案,而是从兔子洞里掉下来的 cmets 把我带到了这个 OpenCV VideoCapture cannot read video in Python but able in VS11,这就是我发现我的问题的方式

【问题讨论】:

  • 如果您在解释器中逐行执行此操作会发生什么? cap 是否使用真实内存位置创建?

标签: python opencv video


【解决方案1】:

如果在 Windows 上,并且您确定您已成功打开文件,most probably you need a codec pack

【讨论】:

  • 我已经下载了 xvideo 以及其他提到的 xpac。我可以通过多种方式打开文件,包括“ffmpeg -i”来获取信息。在 Windows Media Player 中打开并在其他一些视频播放器中打开它。
猜你喜欢
  • 2012-05-08
  • 2015-09-11
  • 2021-03-22
  • 2019-11-22
  • 2013-06-28
  • 2017-09-28
  • 2014-06-29
  • 1970-01-01
  • 2015-12-29
相关资源
最近更新 更多