OpenCV:FFMPEG:标签0xffffffff /'����'找不到(格式' mp4 / MP4(MPEG-4 Part 14)')'

时间:2017-01-24 01:05:56

标签: python opencv video ffmpeg

我正在尝试在python中保存背景扣除视频,以下是我的代码。

import cv2
import numpy as np

capture = cv2.VideoCapture('MAH00119.mp4')
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'X264')
out = cv2.VideoWriter('output.mp4', -1 , 20.0 , size)
fgbg= cv2.createBackgroundSubtractorMOG2()

while True:
    ret, img = capture.read()
    if ret==True:
        fgmask = fgbg.apply(img)
        out.write(fgmask)
        cv2.imshow('img',fgmask)

    if(cv2.waitKey(27)!=-1):
        break

capture.release()
out.release()
cv2.destroyAllWindows()

但是,这会引发以下错误:" OpenCV:FFMPEG:tag 0xffffffff /'����'找不到(格式' mp4 / MP4(MPEG-4 Part 14)')'"

我安装了FFMPEG并将其添加到环境变量中。我的背景减法代码无需保存到文件工作正常,所以我知道openCV安装没有任何问题。我被困在这个地方。我知道我的python似乎并不认识FFMPEG但我不知道除了将FFMPEG添加到环境变量之外还有什么可做的。 我在Windows 10和Python 2.7上使用OpenCV 3.2版。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:9)

稍微修改了一下代码。它适用于我的PC,在Windows 10 64位上使用OpenCV 3.2 for Python 2.7。

import cv2
import numpy as np

capture = cv2.VideoCapture('./videos/001.mp4')
size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'DIVX')  # 'x264' doesn't work
out = cv2.VideoWriter('./videos/001_output.mp4',fourcc, 29.0, size, False)  # 'False' for 1-ch instead of 3-ch for color
fgbg= cv2.createBackgroundSubtractorMOG2()

while (capture.isOpened()):  #while Ture:
    ret, img = capture.read()
    if ret==True:
        fgmask = fgbg.apply(img)
        out.write(fgmask)
        cv2.imshow('img',fgmask)

    #if(cv2.waitKey(27)!=-1):  # observed it will close the imshow window immediately
    #    break                 # so change to below
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture.release()
out.release()
cv2.destroyAllWindows()

检查this cv2.VideoWriter()的参数设置。

希望得到这个帮助。

相关问题