Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: FFMPEG: tag 0xffffffff/'����' is not found (format 'mp4 / MP4 (MPEG-4 Part 14)')'

I am trying to save a background subtracted video in python and the following is my code.

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()

However, this keeps throwing the following error: "OpenCV: FFMPEG: tag 0xffffffff/'����' is not found (format 'mp4 / MP4 (MPEG-4 Part 14)')'"

I have FFMPEG installed and have added it to the environment variables. My background subtraction code without having to save to a file works fine, so I know there's nothing wrong with openCV installation. I am stuck at this place. I know that my python doesn't seem to recognize FFMPEG but I don't know what else to do apart from adding FFMPEG to the environment variables. I am using OpenCV version 3.2 on Windows 10 and Python 2.7.

Any help will be much appreciated!

like image 919
Gingerbread Avatar asked Jan 05 '23 16:01

Gingerbread


1 Answers

Modified the code a little bit. It works on my PC with OpenCV 3.2 for Python 2.7 on Windows 10 64-bit.

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()

Check this for the parameters setting on cv2.VideoWriter().

Hope this help.

like image 182
thewaywewere Avatar answered Jan 13 '23 22:01

thewaywewere