在Python中保存后,OpenCV视频不会播放

2024-05-17 00:25:32 发布

您现在位置: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()

怎么了


Tags: theimportrelease视频is分辨率outcv2
2条回答

在我看来,你没有用“out”做任何事情

您应该添加:

out.write(frame)

在循环内部

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

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

谢谢你关注我的问题

相关问题 更多 >