Python和OpenCV - 无法编写可读的avi视频文件

Hil*_*man 5 python opencv ffmpeg avi windows-10

我有这样的代码:

import numpy as np
import cv2


cap = cv2.VideoCapture('C:/Users/Hilman/haatsu/drive_recorder/sample/3.mov')

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

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

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

但是output.avi无法播放.

尝试也改变了out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))这样的事情(正如一些人所建议的那样)out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480)).但是当我这样做时,我得到了这个消息

OpenCV: FFMPEG: tag 0xffffffff/'    ' is not found (format 'avi / AVI (Audio Video Interleaved)')'.

可能是什么问题呢?我顺便使用Windows 10.

Sar*_*wal 0

我在 Ubuntu 14.04 上使用 OpenCV 2.4.9,以下代码对我来说效果很好:

import cv2
import cv
cap = cv2.VideoCapture(0)
ret,img=cap.read()
height , width , layers =  img.shape     
fps=20
video = cv2.VideoWriter("rec_out.avi", cv.CV_FOURCC(*'DIVX'), fps, (img.shape[1], img.shape[0]))
while True: 

    ret,img=cap.read()
    height , width , layers =  img.shape
        video.write(img)
    cv2.imshow('Video', img)
    #video.write(img)
    if(cv2.waitKey(10) & 0xFF == ord('b')):
            break



cv2.destroyAllWindows()
video.release()
Run Code Online (Sandbox Code Playgroud)