我正在尝试将视频保存为 .avi
格式,但我一直收到错误消息 “could not demultiplex stream”
。我主要想保存灰度视频。
我需要使用任何特定的编解码器
吗?
现在我尝试使用 XVID、DIVX
import imutils
import cv2
import numpy as np
interval = 30
outfilename = 'output.avi'
threshold=100.
fps = 10
cap = cv2.VideoCapture("video.mp4")
ret, frame = cap.read()
height, width, nchannels = frame.shape
fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter( outfilename,fourcc, fps, (width,height))
ret, frame = cap.read()
frame = imutils.resize(frame, width=500)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
while(True):
frame0 = frame
ret, frame = cap.read()
frame = imutils.resize(frame, width=500)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
if not ret:
deletedcount +=1
break
if np.sum( np.absolute(frame-frame0) )/np.size(frame) > threshold:
out.write(frame)
else:
print "Deleted"
cv2.imshow('Feed - Press "q" to exit',frame)
key = cv2.waitKey(interval) & 0xFF
if key == ord('q'):
print('received key q' )
break
cap.release()
out.release()
print('Successfully completed')
最佳答案
对于 Windows 操作系统,请尝试:
out = cv2.VideoWriter(outfilename, fourcc, fps, (width, height), 0)
关于python - 在 OpenCV 中保存灰度视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50037063/