【问题标题】:how to save video opencv-python on mac如何在mac上保存视频opencv-python
【发布时间】:2016-04-08 05:05:36
【问题描述】:
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, 任何人都可以帮助我吗?谢谢

【问题讨论】:

  • 试试-1作为你的FOURCC,这样你就可以选择使用哪个编解码器了。
  • 对不起,我不工作

标签: python macos opencv numpy


【解决方案1】:

这是我为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()

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • @TylerXie 您确定您的 VideoCapture 设备已成功打开吗?可能你永远不会进入 while 循环。您可以尝试打开一个视频文件,例如:`cv2.VideoCapture('somefile.mp4')`。
    猜你喜欢
    • 2019-12-04
    • 2023-01-19
    • 2020-07-09
    • 2015-06-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    相关资源
    最近更新 更多