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

时间:2016-11-13 23:13:08

标签: python-2.7 opencv3.1

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

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 个答案:

答案 0 :(得分:1)

我找到了答案,我所做的是通过输入" -1"来获得可能的CODEC值。而不是" fourcc"然后在fourcc编解码器网站https://www.fourcc.org/codecs.php上查找该编解码器的4位数代码。 就我而言,它是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))