简体   繁体   English

如何在Python中使用OpenCV存储网络摄像头视频

[英]How to store webcam video with OpenCV in Python

I've got a script in Python which reads out my webcam and shows it in a window. 我有一个Python脚本,可读取网络摄像头并将其显示在窗口中。 I now want to store the results, so following this tutorial I wrote the following code: 现在,我想存储结果,因此在本教程之后,我编写了以下代码:

import cv2
import imutils
camera = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc(*'XVID')
video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

while True:
    try:
        (grabbed, frame) = camera.read()  # grab the current frame
        frame = imutils.resize(frame, width=640, height=480)
        cv2.imshow("Frame", frame)  # show the frame to our screen
        key = cv2.waitKey(1) & 0xFF  # I don't really have an idea what this does, but it works..
        video_writer.write(frame)  # Write the video to the file system
    except KeyboardInterrupt:
        break

# cleanup the camera and close any open windows
camera.release()
video_writer.release()
cv2.destroyAllWindows()
print "\n\nBye bye\n"

This perfectly shows the real time video footage from my webcam in a new window. 这可以在新窗口中完美显示我的摄像头的实时视频素材。 But writing the video file seems to fail. 但是写入视频文件似乎失败。 It does create a file called output.avi , but the file is empty (zero bytes) and on the command line I see the following errors: 它确实创建了一个名为output.avi的文件,但是该文件为空(零字节),并且在命令行上我看到以下错误:

OpenCV: Frame size does not match video size.
OpenCV: Frame size does not match video size.
OpenCV: Frame size does not match video size.
etc.

I clearly resize the frame to the size in which I want to save the video (640x480) so I'm not sure why it wouldn't match. 我清楚地将帧的大小调整为要保存视频的大小(640x480),所以我不确定为什么它不匹配。

When I run the script again (so in this case the empty output.avi already exists), it shows these errors: 当我再次运行脚本时(在这种情况下,空的output.avi已经存在),它显示以下错误:

2017-04-17 10:57:14.147 Python[86358:5848730] AVF: AVAssetWriter status: Cannot Save
2017-04-17 10:57:14.332 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
2017-04-17 10:57:14.366 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
2017-04-17 10:57:14.394 Python[86358:5848730] mMovieWriter.status: 3. Error: Cannot Save
etc.

In the tutorial it says that the Four digit FourCC code is used to specify the video codec which is platform dependent and that the list of available codes can be found in fourcc.org. 在本教程中,它说四位数的FourCC代码用于指定依赖于平台的视频编解码器,并且可用代码列表可以在fourcc.org中找到。 I'm on OSX so I tried a bunch of different codec-codes: DIVX, XVID, MJPG, X264, WMV1, WMV2. 我在OSX上,因此我尝试了一堆不同的编解码器代码:DIVX,XVID,MJPG,X264,WMV1,WMV2。 But unfortunately none of them work for me. 但不幸的是,他们都没有为我工作。 They all give the same errors, except for MJPG , which gives me the following error: 除了MJPG ,它们都给出相同的错误,这给了我以下错误:

OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829
Traceback (most recent call last):
  File "store_video.py", line 15, in <module>
    video_writer.write(frame)  # Write the video to the file system
cv2.error: /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp:829: error: (-215) img.cols == width && img.rows == height && channels == 3 in function write

Does anybody know what could be wrong here? 有人知道这里可能出什么问题吗? All tips are welcome! 欢迎所有提示!

It's probably because you built OpenCV with AVFoundation and it doesn't support XVID or other codec. 可能是因为您使用AVFoundation构建了OpenCV,但它不支持XVID或其他编解码器。 You can try mp4v and m4v extension. 您可以尝试mp4vm4v扩展名。

import cv2
camera = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object to save the video
fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
video_writer = cv2.VideoWriter('output.m4v', fourcc, 30.0, (640, 480))

while True:
        (grabbed, frame) = camera.read()  # grab the current frame
        frame = cv2.resize(frame, (640,480))
        cv2.imshow("Frame", frame)  # show the frame to our screen
        key = cv2.waitKey(33) & 0xFF  # I don't really have an idea what this does, but it works..
        video_writer.write(frame)  # Write the video to the file system
        if key==27:
            break;

# cleanup the camera and close any open windows
camera.release()
video_writer.release()
cv2.destroyAllWindows()
print("\n\nBye bye\n")

On the other note, the error 另一方面,错误

OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /tmp/opencv3-20170216-77040-y1hrk1/opencv-3.2.0/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829

means that you messed up the dimension with 表示您将尺寸弄乱了

frame = imutils.resize(frame, width=640, height=480)

You can try cv2.resize as I used in my code. 您可以尝试使用我在代码中使用的cv2.resize There's no need to use another library when cv2 can do that already. cv2已经可以使用另一个库时,就不需要使用它了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在网络摄像头视频 stream 上显示变量值? (蟒蛇OpenCV) - How to show a variable value on the webcam video stream? (python OpenCV) 在python openCV上的网络摄像头视频上选择静态ROI - Select a static ROI on webcam video on python openCV OpenCV和Python:如果源不是来自网络摄像头,则视频输出为空 - OpenCV and Python: Video output is empty if the source is not coming from the webcam python opencv在网络摄像头视频中使用putText显示时间倒计时 - python opencv display time countdown using putText in webcam video 视频文件与实时网络摄像头馈送 window 大小 OpenCV Python - Video file vs live webcam feed window size in OpenCV Python opencv / python:流网络摄像头视频时模块cv2中的掩码 - opencv/python: mask in module cv2 while streaming webcam video 从 HTML 中的网络摄像头获取视频帧并将其用于 OPENCV python - Get video frames from webcam in HTML and use it for OPENCV python 从 python 中的网络摄像头捕获视频时出现 OPENCV 错误 - OPENCV error while captureing video from webcam in python OpenCV Python - 在网络摄像头视频源中随机绘制圆圈 position - OpenCV Python - Draw circle at random position in webcam video feed 如何在OpenCV Python中将滤镜应用于网络摄像头 - How to apply a filter to webcam in OpenCV Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM