OpenCV和Python 3-无法将视频帧写入磁盘



python 3.5.2,windows 10上的anaconda 4.2.0。OPENCV从conda,版本3.1.0安装。

我正在尝试通过打开视频文件,转换每个帧并将结果放入新的视频文件中来处理。输出文件是创建的,但大小约为800字节,其空。输入文件约为4,000帧,约为150 mb。

这是我的代码,它非常紧密地遵循OpenCV文档的指南。

import cv2
import progressbar
# preprocess video
# args.input is a valid file name     
    outname = 'foo.mp4'
    cap = cv2.VideoCapture(args.input)    
    codec = int(cap.get(cv2.CAP_PROP_FOURCC)) 
    framerate = app_config.camera.framerate #240
    size = (app_config.camera.width, app_config.camera.height) #1080 x 720
    vw = cv2.VideoWriter(filename=outname, fourcc=codec, fps=framerate, frameSize=size, isColor=False)
    curframe = 0
    with progressbar.ProgressBar(min_value=0, max_value=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))) as pb:
        while cap.isOpened():
            ret, frame = cap.read()
            if ret:
                #update the progress bar
                curframe += 1
                pb.update(curframe)
                # convert to greyscale
                grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                # invert colors
                inverted = cv2.bitwise_not(grey)
                vw.write(inverted)   
                #cv2.imshow('right', right)
                #if cv2.waitKey(1) & 0xFF == ord('q'):
                #    break
            else:
                break
        cap.release()
        vw.release()
        cv2.destroyAllWindows()

我收到以下错误:

OpenCV: FFMPEG: tag 0x7634706d/'mp4v' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???'

如果我尝试设置codec = cv2.VideoWriter_fourcc(*'H264'),我会收到类似的错误(以及警告我的264库路径的不正确环境变量)。

确保inverted的尺寸与videoWriter定义中的size参数的尺寸匹配。

还将'M','P','4','V'编解码器与.mp4容器一起使用。

最新更新