无法播放从 python 2.7 保存的视频

cant play the video saved from python 2.7

我试过很多编解码器,除了最后一个编解码器外,所有编解码器都产生 0 字节视频,最后一个编解码器确实产生了一个视频文件,但后来无法播放。这个问题快把我逼疯了,我使用的代码已经过其他用户的测试。 我已将 opencv_ffmpeg310_64.dll 文件复制到 c:\python2.7 文件夹中。 我在 windows 10 和 opencv 3.1.0 我尝试了所有类型的编解码器 none 都在工作。

import numpy as np
import cv2

    cap = cv2.VideoCapture(0)

    # Define the codec and create VideoWriter object
# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'FFV1')
#fourcc = cv2.VideoWriter_fourcc(*'XVID')
#fourcc = cv2.VideoWriter_fourcc(*'DIVX')
#fourcc = cv2.VideoWriter_fourcc(*'DIV3')
#fourcc = cv2.VideoWriter_fourcc('F','M','P','4')
#fourcc = cv2.VideoWriter_fourcc('D','I','V','X')
#fourcc = cv2.VideoWriter_fourcc('D','I','V','3')
#fourcc = cv2.VideoWriter_fourcc('F','F','V','1')


fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
    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()

我找到了答案,我所做的是通过输入“-1”而不是 "fourcc" 来获取可能的 CODEC 值,然后在 fourcc 编解码器网站上查找该编解码器的 4 位代码 https://www.fourcc.org/codecs.php。 在我的例子中是 Cinemax 编解码器 CDIV

SO first step:  find the code and enter by hand manually and see if the video is made and is playable   

#use -1 below for entering the codec by hand
#out = cv2.VideoWriter('output.avi',-1, 20.0, (640,480))

second step: from the above step find out which codec worked for you and then look up the four digit code on the fourcc site and plug it in 

fourcc = cv2.VideoWriter_fourcc(*'CVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))