找不到OpenCV:FFMPEG:tag 0xffffff/'''''''''''''(格式为'mp4/mp4(MPEG-4第14部分)')

2024-04-27 00:56:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用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:tag0xffffff/'''''''''''''''''''''''''''''''''(格式'mp4/mp4(MPEG-4第14部分)''”

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

任何帮助都将不胜感激!


Tags: 代码importimgsizegetoutcv2frame
1条回答
网友
1楼 · 发布于 2024-04-27 00:56:05

稍微修改了一下代码。它可以在我的电脑上使用OpenCV 3.2for Python 2.7onwindows1064位。

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()上的参数设置。

希望能帮上忙。

相关问题 更多 >