OpenCV 视频未保存

OpenCV video not getting saved

我试图在 OpenCV 中保存视频,但我一直收到错误 "could not demultiplex stream"。然后我检查了大小,发现它在 kB 中。我主要想保存灰度视频,如何实现?

我需要使用什么特定的codec吗?

mplayer gives the following output

MPlayer 1.1-4.8 (C) 2000-2012 MPlayer Team
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing output.avi.
libavformat version 54.20.4 (external)
Mismatching header version 54.20.3
AVI file format detected.
[aviheader] Video stream found, -vid 0
AVI: Missing video stream!? Contact the author, it may be a bug :(
libavformat file format detected.
[lavf] stream 0: video (mpeg4), -vid 0
VIDEO:  [MP4V]  1280x720  24bpp   -nan fps    0.0 kbps ( 0.0 kbyte/s)
Clip info:
 encoder: Lavf54.20.4
Load subtitles in ./
Failed to open VDPAU backend libvdpau_nouveau.so: cannot open shared object file: No such file or directory
[vdpau] Error when calling vdp_device_create_x11: 1
==========================================================================
Opening video decoder: [ffmpeg] FFmpeg's libavcodec codec family
libavcodec version 54.35.1 (external)
Mismatching header version 54.35.0
Unsupported AVPixelFormat 53
Selected video codec: [ffodivx] vfm: ffmpeg (FFmpeg MPEG-4)
==========================================================================
Audio: no sound
Starting playback...
V:   0.0   0/  0 ??% ??% ??,?% 0 0 


Exiting... (End of file)

现在我尝试了多种编解码器格式

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

import numpy as np
import cv2

cap = cv2.VideoCapture(0)


out = cv2.VideoWriter('output.avi',-1, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret:
        gray = cv2.cvtColor(src=frame, code=cv2.COLOR_BGR2GRAY)

        out.write(gray)

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

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

试试这个
Select 英特尔 iyuv 编解码器。 out.avi 不是工作文件。
output.avi 是新的工作文件。

如果视频没有保存,原因可能是它的 捕获大小 被硬编码为 (640,480)

您可以试试下面的代码:

cap = cv2.VideoCapture(0)

fourcc_codec = cv2.VideoWriter_fourcc(*'XVID')
fps = 20.0
capture_size = (int(cap.get(3)), int(cap.get(4)))

out = cv2.VideoWriter("output.avi", fourcc_codec, fps, capture_size)

你也可以检查你传递的形状是否正确,这样做:

  1. h, w, _ = frame.shape

  2. size = (w, h)

  3. out = cv2.VideoWriter('video.avi', cv2.VideoWriter_fourcc(*'XVID'), 30, size)