我试图通过Open-CV捕获并保存视频文件,但它没有播放

时间:2017-12-02 12:39:56

标签: python-3.x opencv computer-vision

这是我试过的代码。视频文件在代码运行后创建但视频文件未播放。我很难理解?

import cv2

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'XVID') 

out = cv2.VideoWriter('C:/Users/DarkLord/Downloads/output1.avi',fourcc, 20,(1920,1000))

while(cap.isOpened()):
    ret, frame = cap.read()
    out.write(frame)
    if ret==True:
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break


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

2 个答案:

答案 0 :(得分:0)

试试这个。 这可能会有所帮助

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
#fourcc = cv2.cv.CV_FOURCC(*'DIVX')
#out = cv2.VideoWriter('C:/Users/DarkLord/Downloads/output1.avi',fourcc, 20,(1920,1000))
out = cv2.VideoWriter('C:/Users/DarkLord/Downloads/output1.avi', -1, 20,(1920,1000))

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

        # write the flipped frame
        out.write(frame)

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

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

答案 1 :(得分:0)

视频的大小必须与相机阅读框的大小相同。 修复这样的代码。

import cv2

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
out = cv2.VideoWriter(r'C:/Users/DarkLord/Downloads/output1.avi',fourcc, 20,size)
while(cap.isOpened()):
    ret, frame = cap.read()
    out.write(frame)
    if ret==True:
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()
out.release()
cv2.destroyAllWindows()