【发布时间】: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是否使用真实内存位置创建?