【问题标题】:opencv and python 3 -- cannot write video frames to diskopencv 和 python 3 - 无法将视频帧写入磁盘
【发布时间】:2017-07-07 10:53:29
【问题描述】:

Windows 10 上的 Python 3.5.2、anaconda 4.2.0。 从conda 安装的 OpenCV,版本 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'),我会收到类似的错误(以及我的 h.264 库路径的环境变量不正确的警告)。

【问题讨论】:

  • 当你输入“cv2.__version__”时你会得到什么,只是为了确保你加载了正确的opencv。另外,你确定“foo.mp4”的高度和高度是1080 * 720吗?如果不是,它将写入一个空的视频文件。如果这些不起作用,请尝试实现“fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')","video = cv2.VideoWriter()", "video.open(outname, Fourcc,帧速率,大小,False。希望这会有所帮助!

标签: opencv video python-3.5


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 2013-03-05
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    相关资源
    最近更新 更多