【发布时间】:2013-09-21 01:41:47
【问题描述】:
我正在使用 mac os x 1.7.5,通过 macports 安装了 python 2.7.5_1 和 opencv 2.4.4_0。我似乎拥有所有最新的依赖端口。
在我的代码中,cv2.Videowriter() 已成功创建并打开,它会生成一个 6kb 的 .avi 文件,但 videoFile.write(img0) 不会向该文件中写入任何内容。我真的无法弄清楚为什么视频流没有写入文件。有什么见解吗?
我的代码如下:
import cv2
import cv
cv2.namedWindow("Original")
cap0 = cv2.VideoCapture(0)
codec = cv.CV_FOURCC('D','I','V','X')
print codec
videoFile = cv2.VideoWriter();
videoFile.open('video.avi', codec, 25, (640, 480),1)
key = -1
while(key < 0):
success0, img0 = cap0.read()
cv2.imshow("Original", img0)
videoFile.write(img0)
key = cv2.waitKey(1)
cv2.destroyAllWindows()
我已经尝试过这些编解码器,但没有一个可以工作:I420、AVC1、YUV1、PIM1、MJPG、MP42、MP4V、DIV3、DIVX、XVID、IUYV、FFV1、FLV1、U263、H264、ZLIB
quick time codecs mentioned here@
使用 ZLIB 编解码器出现错误:
[zlib @ 0x7fb0d130a000] Specified pixel format yuv420p is invalid or not supported
使用 H264 编解码器出现错误:
[libx264 @ 0x7fe423869600] broken ffmpeg default settings detected
[libx264 @ 0x7fe423869600] use an encoding preset (e.g. -vpre medium)
[libx264 @ 0x7fe423869600] preset usage: -vpre <speed> -vpre <profile>
[libx264 @ 0x7fe423869600] speed presets are listed in x264 --help
[libx264 @ 0x7fe423869600] profile is optional; x264 defaults to high
我不明白上述错误是什么意思。我尝试将 ffmpeg 重新安装到最新版本(1.2.2_0+gpl2),但我的脚本仍然无法正常工作。所有其他编解码器都没有给出任何错误。
我什至用上述编解码器尝试了 .mpg 和 .mkv 的文件扩展名。有时我会收到一条错误消息,说编解码器不适合文件扩展名,但是当我没有收到错误消息时,我只会得到一个无法读取的小尺寸视频文件。
非常感谢任何帮助。
ps:我已经解决了以下未解决我的问题的 SO 问题:
- using opencv2 write streaming video in python
- Writing video with OpenCV + Python + Mac
- Create an avi video with opencv and python on a mac
- Python OpenCV, can't write a video (.avi) to file
- Creating AVI files in OpenCV
- read/write avi video on MAC using openCV
- Python OpenCV 2.4 writes half-complete PNG video frames
【问题讨论】: