繁体   English   中英

如何在Mac上保存视频opencv-python

[英]how to save video opencv-python on mac

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

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

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == cv2.flip(frame,0):

        # Write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) == ord('q'):
            break
    else:
        break;

cap.release()
out.release()
cv2.destroyAllWindows ()

我想使用opencv-python保存视频。 此代码运行后,我确实得到了“ output.avi”文件,但无法打开它。 我正在使用的环境是Mac10.10,opencv2.4.11,python2.7,有人可以帮助我吗? 谢谢

实际上,您的代码将无法运行,因为您正在将Bool类型的retcv2.flip函数的输出进行比较,后者是一个输出数组(翻转的帧)。 您应该将代码更改为:

...
if ret == True:
    fliped = cv2.flip(frame,0)
    out.write(fliped)
    ...

这是为VideoWriter设置正确大小后的代码,我可以获取视频。

cap = cv.VideoCapture(0)
# Define the codec and create VideoWriter object
width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH) + 0.5)
height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT) + 0.5)
size = (width, height)

fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 20.0, size)
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # frame = cv.flip(frame, 0)
    # write the flipped frame
    out.write(frame)
    cv.imshow('frame', frame)
    if cv.waitKey(1) == ord('q'):
        break
# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM