OpenCV-使用Python保存后无法播放视频

时间:2020-06-02 06:37:13

标签: python opencv

在Python上将分辨率更改为300x300后,我尝试保存视频,但是使用

保存后我的视频无法播放
0xc10100be error: "This file isn't playable. That might be because the file type is unsupported, the file extension is incorrect, or the file is corrupt."

这是我的程序:

import numpy as np
import cv2

cap = cv2.VideoCapture("F:\\mi\\Camera\\2b7d9eccaddffffe3c9ba70b7fe6c12e(0).mp4")
cv2.namedWindow("vid1", 0)
cv2.resizeWindow("vid1", 300,300)

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (300,300))

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

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

怎么了?

2 个答案:

答案 0 :(得分:0)

在我看来,您对“ out”不做任何事情。

您应该添加:

out.write(frame)

在循环内部。

答案 1 :(得分:0)

问了我老师这个问题之后,他修好了我的程序:

import numpy as np
import cv2

cap = cv2.VideoCapture("F:\\mi\\Camera\\2b7d9eccaddffffe3c9ba70b7fe6c12e(0).mp4")
cv2.namedWindow('frame',0)
cv2.resizeWindow('frame',300,300)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (300,300))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret:
        vidout=cv2.resize(frame,(300,300)) #create vidout funct. with res=300x300
        out.write(vidout) #write frames of vidout function
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()
out.release()
cv2.destroyAllWindows()

感谢您对我的问题的关注!