首页 > 解决方案 > OpenCV - 在 Python 中保存后视频不播放

问题描述

我尝试在 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()

怎么了?

标签: pythonopencv

解决方案


在向我的老师询问这个问题后,他修复了我的程序:

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()

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


推荐阅读